Once you’ve done this, reload your app. You should begin seeing events in the PostHog events explorer.
3. Create 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
This is the simplest option. PostHog has a variety of survey templates to choose from, and handles all the display logic and response capture for you. You can also customize the questions, branding, and display conditions as needed – see our survey docs for more details on how to do so.
To create a survey with a prebuilt UI, go to the surveys tab in PostHog and click "New survey".
Select any template, or you can create your own by clicking "Create blank survey". Then, configure your survey with the following details:
Ensure Presentation is set to Popover.
Set the display conditions to All users.
Use the default values for everything else.
Then, click "Save as draft" and then "Launch". Your survey is now live and you should see it in your app. After submitting responses, you can view results in PostHog.
Option 2: Implement your own survey UI
If you prefer to have complete control of your survey UI and logic, you can still use PostHog to keep track of and analyze your results.
First, create a survey in PostHog like in option 1 above (for this tutorial, we use a Net Promoter Score survey template). The only difference is you must set Presentation to API.
Then, there are four parts to adding code for our custom survey:
Create the survey UI.
Fetch the survey from PostHog.
Add the logic for displaying and hiding it.
Capture interactions from it.
1. Create the survey UI
We've created a sample survey UI for this tutorial. To use it, create a new folder components in your base directory and then create a new file CustomSurvey.vue in it. Paste the following code in this file:
components/CustomSurvey.vue
<template>
<divclass="survey">
<h2>{{ title }}</h2>
<div>
<buttonv-for="i in 10":key="i"class="button"@click="handleSelect(i)">
This shows a survey popup every time you visit your app's homepage.
2. Fetch the survey from PostHog
PostHog keeps track of all active surveys for a user (this is especially helpful if you set up custom display conditions).
To fetch the active surveys when the app is mounted, we call $posthog().getActiveMatchingSurveys(). This returns a surveys object that looks like this:
JSON
[
{
"id":"018cfcd5-107e-0000-49a1-8e7c6b825947",
"name":"Net promoter score (NPS) API Survey",
"description":"",
"type":"api",
"linked_flag_key":null,
"targeting_flag_key":null,
"questions":[
{
"type":"rating",
"scale":10,
"display":"number",
"question":"How likely are you to recommend us to a friend?",
"description":"",
"lowerBoundLabel":"Unlikely",
"upperBoundLabel":"Very likely"
}
],
"conditions":null,
"start_date":"2024-01-12T08:41:20.614000Z",
"end_date":null
}
]
We can use this survey object to configure our CustomSurvey component:
app.vue
<template>
<!-- ... rest of your template ... -->
</template>
<scriptsetup>
import{ ref, onMounted }from'vue';
import{ useNuxtApp }from'#app';
const showSurvey =ref(true);
const surveyTitle =ref('');
const surveyID =ref('');
onMounted(async()=>{
awaitfetchActiveSurveys();
});
constfetchActiveSurveys=()=>{
const{ $posthog }=useNuxtApp();
$posthog().getActiveMatchingSurveys((surveys)=>{
if(surveys.length>0){
const survey = surveys[0];
surveyID.value= survey.id;
surveyTitle.value= survey.questions[0].question;
}
});
};
// rest of your code
</script>
3. Add the logic for displaying and hiding it.
We want to make sure we don't show the survey again to users who have either submitted or dismissed it. We use localStorage to store this data and use it to check whether to show the survey or not.