JavaScript web
Browser only.
posthog-jsruns in the browser. For server-side JavaScript, use the Node.js SDK.
Installation
Option 1: Install with the AI wizard
Run the AI wizard in your project directory to detect your framework, install PostHog, and configure your app:
Option 2: 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 JavaScript SDK types reference for all exported interfaces and options.
Option 3: Install via package manager
If your site sets a Content-Security-Policy, it needs to allow PostHog. This applies to the snippet and to package installs alike: the SDK lazy-loads extra bundles (session replay, surveys) from PostHog's CDN, and sends events to the ingestion host. PostHog serves from subdomains of
posthog.comthat change over time, so allow the wildcard:
script-srccovers the snippet and the lazy-loaded bundles,connect-srccovers event ingestion and feature flags, andworker-srccovers session replay. The toolbar needs a few more, or use a reverse proxy so everything is first-party. Failing to do so causes silent failures wherecaptureandidentifycalls never send, so the integration looks complete while zero events arrive. Rememberconnect-srcfalls back todefault-src, sodefault-src 'self'blocks event delivery even when the script itself is bundled.
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 |
SiteAppsExtensions | JS snippets |
TracingExtensions | Distributed tracing header injection |
ToolbarExtensions | Toolbar |
LogsExtensions | Log capture |
ConversationsExtensions | Support |
WebMCPExtensions | WebMCP tool call capture for MCP Analytics. Add ErrorTrackingExtensions to also send failed calls to Error Tracking. |
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 option is a date that selects a configuration snapshot used as defaults to initialize PostHog. The examples use the latest snapshot. This default is overridden when you explicitly set a value for any of the options. The configuration reference lists each snapshot and what it changes.
Using Google Tag Manager? Use the Google Tag Manager template rather than a custom HTML tag.
Pass this to your agent
Give your agent the markdown version of the JavaScript SDK reference.
What the SDK does
After setup, the SDK captures pageviews, clicks, form interactions, and sessions automatically:
- Anonymous-to-identified profiles. Events start under a generated anonymous ID with no person profile (
person_profiles: 'identified_only'by default). Calling identify links that history to the user's profile. If you already know who signed in on page load, see identifying users. - **Cross-subdomain tracking.** Default storage pairs `localStorage` with a shared cookie, so PostHog keeps identity and sessions across subdomains like `example.com` and `app.example.com`. See [Persistence](/docs/libraries/js/persistence) for storage modes or [cross-domain tracking](/tutorials/cross-domain-tracking) for separate root domains.
- **Consent and opt-out.** You can start opted out or run cookieless by default, then update consent at runtime with [opt_in_capturing](/docs/libraries/js/1.x.x#posthogopt_in_capturing) and [opt_out_capturing](/docs/libraries/js/1.x.x#posthogopt_out_capturing). See [Privacy and consent](/docs/libraries/js/1.x.x#privacy-and-consent) for every consent method.
- Named instances and debugging. Pass a name string as the third argument to init to create an isolated secondary instance. Disable autocapture on it, so you don't capture duplicate events.
Next steps
| Guide | Covers |
|---|---|
| Usage | SDK lifecycle across events, identity, feature flags, surveys, replay, and filtering |
| Configuration | Interactive config builder, where settings apply, proxy paths, and snippet pins |
| Security | Version pinning, Subresource Integrity (SRI), CSP, nonces, and no-external bundles |
| Persistence | Browser storage modes, subdomain cookies, and consent storage separation |
| Version 1.x.x | Full generated API signatures, config options, and TypeScript types |





