Skill 76 · Instrument Error Tracking
Subchapter 76.29
references/web.mdMarkdown5 KBView on GitHub
AI agents: this is one page from PostHog’s docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt (opens in a new tab)
1
Required
You can either add the JavaScript snippet directly to your HTML or install the JavaScript SDK via your package manager.
Add this snippet to your website within the <head> tag. This can also be used in services like Google Tag Manager:
HTML
<script>
!function(t,e){var o,n,p,r;e.__SV||(window.posthog && window.posthog.__loaded)||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(
Install the PostHog JavaScript library using your package manager. Then, import and initialize the PostHog library with your project token and host:
npm install posthog-jsyarn add posthog-jspnpm add posthog-jsbun add posthog-jsJavaScript
import posthog from 'posthog-js'
posthog.init('<ph_project_token>', {
api_host: 'https://us.i.posthog.com',
defaults: '2026-05-30'
})2
Recommended
Once installed, PostHog will automatically start capturing events. You can also manually send events to test your integration:
Click around and view a couple pages to generate some events. PostHog automatically captures pageviews, clicks, and other interactions for you.
If you’d like, you can also manually capture custom events:
JavaScript
posthog.capture('my_custom_event', { property: 'value' })3
Recommended
You can enable exception autocapture for the JavaScript Web SDK in the Error tracking section of your project settings (opens in a new tab).
When enabled, this automatically captures $exception events when errors are thrown by wrapping the window.onerror and window.onunhandledrejection listeners.
4
Optional
It is also possible to manually capture exceptions using the captureException method:
JavaScript
posthog.captureException(error, additionalProperties)This is helpful if you’ve built your own error handling logic or want to capture exceptions that are handled by your application code.
Recommended
Confirm events are being sent to PostHog
Before proceeding, let’s make sure exception events are being captured and sent to PostHog. You should see events appear in the activity feed.

5
Required
Great, you’re capturing exceptions! If you serve minified bundles, the next step is to upload source maps to generate accurate stack traces.
Let’s continue to the next section.
Ask PostHog AI
HelpfulCould be better