Next, replace the boilerplate code in src/App.jsx with the following:
JSX
// src/App.jsx
import'./App.css'
functionApp(){
return(
<divclassName="App">
<h1>This is our survey tutorial</h1>
</div>
)
}
exportdefaultApp
Finally, run npm run dev and go to http://localhost:5173 to see our new homepage.
Adding PostHog
PostHog will manage our survey and track our results. To use PostHog's React SDK, install posthog-js:
Terminal
npminstall posthog-js
Once installed, import PostHog into src/main.jsx and set up a client using your project API key and host from your project settings. Then, wrap the app with PostHogProvider to access PostHog in any component.
With PostHog set up, our React app is ready for the survey.
Creating a survey
There are two options for displaying a survey using PostHog:
Use PostHog's prebuilt survey UI.
Implement your own survey UI.
This tutorial will cover how to implement both options.
Option 1: Use PostHog's prebuilt survey UI
For fast set up, you can use PostHog's prebuilt surveys. There are variety of survey types to choose from, and PostHog handles all the display logic and event capture for you.
To create a survey with a prebuilt UI, go to the Surveys tab in PostHog and click New survey. Select a template like Open feedback, customize it how you'd like, click Save as draft, and then Launch.
Your survey is now live and you should see it in your app. There are no further code changes needed!
Popover surveys provide a bunch of customization options like colors, text, position, font, targeting, completion conditions, and more.
Option 2: Implement your own survey UI
If you want more customization than what PostHog's prebuilt surveys provide, you can implement your own survey UI.
To do this, go to the surveys tab, click New survey, and select the Net promoter score template (others work too, but we'll use this as an example). On the new survey page, set Presentation to API, click Save as draft, and then Launch.
Once created, there are three parts to implementing it in your app:
Create the survey UI.
Add the logic for displaying it.
Capture interactions from it.
1. Create the survey UI
We've created an example survey UI for this tutorial. To use it, create a new file in the src folder called Survey.jsx and paste the following code:
Then, add the following CSS styles to your index.css file:
css
.survey-popup{
position: fixed;
bottom:20px;
right:20px;
width:300px;
padding:20px;
background-color:#ffffff;
box-shadow:0px0px10pxrgba(0,0,0,0.2);
border-radius:5px;
color:#000000;
z-index:1000;
}
.button{
margin:5px;
}
Finally, integrate the component into App.jsx:
JSX
// src/App.jsx
import'./App.css'
import{ useState }from'react'
importSurveyfrom'./Survey'
functionApp(){
const[showSurvey, setShowSurvey]=useState(true)
consthandleDismiss=()=>{
setShowSurvey(false)
console.log("Survey dismissed!")
}
consthandleSubmit=(value)=>{
setShowSurvey(false)
console.log("User submitted:", value)
}
return(
<divclassName="App">
<h1>This is our survey tutorial</h1>
{showSurvey &&(
<Survey
title="Rate our service"
onDismiss={handleDismiss}
onSubmit={handleSubmit}
/>
)}
</div>
)
}
exportdefaultApp
This shows a survey popup every time you visit your app's homepage.
2. Add the logic for displaying it.
The first part of handling our display logic is fetching the survey from PostHog. PostHog keeps track of all active surveys for a user (this is especially helpful if you have set up custom display conditions).
To fetch the active surveys, we use the usePostHog hook to retrieve our PostHog instance. Then, we call posthog.getActiveMatchingSurveys() using useEffect():
JSX
// src/App.jsx
import'./App.css'
import{ useEffect, useState }from'react'
importSurveyfrom'./Survey'
import{ usePostHog }from'posthog-js/react'
functionApp(){
// ... rest of your code ...
const posthog =usePostHog()
useEffect(()=>{
posthog.getActiveMatchingSurveys((surveys)=>{
// TODO: configure the survey
})
},[posthog])// posthog may be undefined until it's had a chance to initialize. Hence use it as a dependency for useEffect
// ... rest of your code ...
}
exportdefaultApp
posthog.getActiveMatchingSurveys() returns a surveys object that looks like this: