Web surveys installation
Important: When installing PostHog via a package manager, surveys require
posthog-js v1.81.1+. It's recommended to install the latest version.
To get started with surveys, add PostHog to your web app by installing our Javascript web SDK (if you haven't already). There are two methods to do so:
Option 1: Add the JavaScript snippet to your HTML Recommended
Keeping the SDK version up to date
Be careful to avoid things which can cause the SDK version to be cached and fail to update. See: Ways SDK versions fall behind
Using TypeScript with the script tag?
If you're using TypeScript and want type safety for window.posthog, install the @posthog/types package:
Then create a type declaration file:
See the TypeScript types documentation for more details.
Option 2: Install via package manager
And then include it with your project token and host (which you can find in your project settings):
See our framework specific docs for Next.js, React, Vue, Angular, Astro, Remix, and Svelte for more installation details.
Update early, update often
We ship weirdly fast, especially for our JavaScript web SDK. If you choose the npm package instead of the HTML snippet, be sure to update it frequently:
To actually update the package, you need to update the version constraint in your package.json file and then reinstall, or run update instead of install:
Bundle all required extensions (advanced)
By default, the JavaScript Web library only loads the core functionality. It lazy-loads extensions such as surveys or the session replay 'recorder' when needed.
This can cause issues if:
- You have a Content Security Policy (CSP) that blocks inline scripts.
- You want to optimize your bundle at build time to ensure all dependencies are ready immediately.
- Your app is running in environments like the Chrome Extension store or Electron that reject or block remote code loading.
To solve these issues, we have multiple import options available below.
Note: With any of the no-external options, the toolbar will be unavailable as this is only possible as a runtime dependency loaded directly from us.posthog.com.
Note: You should ensure if using this option that you always import posthog-js from the same module, otherwise multiple bundles could get included. At this time @posthog/react does not work with any module import other than the default.
Tree shaking with the slim bundle (advanced)
If you only need a subset of PostHog features, you can use the slim bundle to reduce your bundle size. It gives you the core functionality (event capture, identify, group analytics) and lets you explicitly opt in to additional features via extension bundles. This is currently experimental, but offers the biggest reduction in bundle size.
Note: Always import posthog-js from the same module path (posthog-js/dist/module.slim) throughout your app, otherwise multiple bundles could get included.
Available extension bundles
| Bundle | What's included |
|---|---|
FeatureFlagsExtensions | Feature Flags |
SessionReplayExtensions | Session Replay |
AnalyticsExtensions | Autocapture, pageview tracking, heatmaps, dead click detection, web vitals |
ErrorTrackingExtensions | Error Tracking |
SurveysExtensions | Surveys |
ExperimentsExtensions | Experiments |
ProductToursExtensions | Product Tours |
SiteAppsExtensions | Site apps |
TracingExtensions | Distributed tracing header injection |
ToolbarExtensions | Toolbar |
ConversationsExtensions | Conversations |
LogsExtensions | Log capture |
AllExtensions | Everything (equivalent to the default posthog-js bundle) |
Note: Each extension bundle includes its own dependencies. You don't need to worry about adding them separately.
Don't want to send test data while developing?
If you don't want to send test data while you're developing, you can do the following:
What is the `defaults` option?
The defaults is a date, such as 2026-01-30, for a configuration snapshot used as defaults to initialize PostHog. This default is overridden when you explicitly set a value for any of the options.