Web error tracking installation
Contents
- 1
Install PostHog web SDK
RequiredOption 1: Add the JavaScript snippet to your HTML Recommended
HTMLOption 2: Install via package manager
Start by installing PostHog with the package manager of your choice:
And then include it with your project API key and host (which you can find in your project settings):
WebSee our framework specific docs for Next.js, React, Vue, Angular, Astro, Remix, and Svelte for more installation details.
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 fromapp.posthog.com
.WebNote: 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 timeposthog-js/react
does not work with any module import other than the default.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:
WebWhat is the `defaults` option?
The
defaults
is a date, such as2025-05-24
, 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. - 2
Set up exception autocapture
RequiredNote: A minimum SDK version of v1.207.8 is required, but we recommend keeping up to date with the latest version to ensure you have all of error tracking's features.
You can enable exception autocapture for the JavaScript Web SDK in the Error tracking section of your project settings.
When enabled, this automatically captures
$exception
events when errors are thrown by wrapping thewindow.onerror
andwindow.onunhandledrejection
listeners. - 3
Manually capture exceptions
OptionalIt is also possible to manually capture exceptions using the
captureException
method:JavaScriptThis is helpful if you've built your own error handling logic or want to capture exceptions that are handled by your application code.
- 5