There are two ways to set up surveys:
- Get started quickly with a pre-built survey
- Create your own UI components
Option 1 (recommended): Pre-built survey
Make sure you've enabled the surveys app in your project's apps.
You must have the
opt_in_site_appsoption enabled in your PostHog initialization code andposthog-jsversion 1.67.1+ or the JavaScript snippet that includesgetActiveMatchingSurveysandgetSurveys.
posthog.init("<ph_project_api_key>", {"api_host": "<ph_instance_address>","opt_in_site_apps": true})
- That's it! Now you can go create a survey in PostHog.
Option 2: Custom UI components
Make sure you're up to date on PostHog's JavaScript web library. Surveys requires version 1.67.1, or later.
After creating your survey on PostHog app, call
posthog.getSurveys()and get a list of all surveys back if you wish to do your own matching, orposthog.getActiveMatchingSurveys()to get a list of active matching surveys that should display for the current user client.
2.5. Make sure you filter for surveys that are of the type "api".
posthog.getActiveMatchingSurveys((surveys) => {// do something with surveys// handleSurveys(surveys.filter(survey => survey.type === 'api'))})
Note: You'll need to handle whether a user has seen a survey already or not manually if you're using this option. The survey app uses
localStorageto keep track of this, which you can implement similarly in your code. Note: If you're using a URL or selector targeting option, you'll need to poll withposthog.getActiveMatchingSurveys()to get the most updated survey results.
- In order for survey responses to be recorded, you'll need to send in a "survey sent" capture call event with the survey's name, ID, and any properties you'd want.
posthog.capture("survey sent", {$survey_id: {survey.id} // required$survey_response: {survey_response} // required$survey_name: {survey.name} // optional})
Surveys that are shown should be captured with a "survey shown" event and those that are dismissed, with a "survey dismissed" event similar to the above.
PostHog currently supports surveys with either a single text input, or a link to another page (such as a form, or scheduling platform), so be sure to use the syntax above in order to track results. We'll be adding more survey types soon!