JavaScript web configuration

Configure the JavaScript web SDK in three places: startup options in init, runtime updates with set_config, and remote toggles in project settings. See PostHogConfig for every option, default, and snapshot date.

Build an init config

Select the options your app needs to generate a startup snippet:

posthog-init.js
posthog.init('<ph_project_token>', {
api_host: 'https://us.i.posthog.com',
defaults: '2026-08-30',
capture_exceptions: true,
})

Where settings apply

Pass initial options at page load, so attribution fields like UTM tags and referrer URLs aren't lost. Use set_config for changes later in the session, such as consent state:

TypeScript
posthog.init('<ph_project_token>', {
api_host: 'https://us.i.posthog.com',
defaults: '2026-08-30',
})
// Update options at runtime without reinitializing
posthog.set_config({ persistence: 'localStorage+cookie' })

PostHog fetches your remote project settings right after startup. Explicit values you pass to init win for heatmaps, web vitals, and exception autocapture.

Session Replay and autocapture run only when both your code and project settings keep them turned on.

Route through a proxy

Standard reverse proxy setups override api_host and ui_host (plus asset_host if your proxy handles only API routes). When your proxy uses custom path prefixes rather than PostHog's default paths, rewrite outbound request paths with rewriteRequestPath:

JavaScript
posthog.init('<ph_project_token>', {
api_host: 'https://e.yourdomain.com',
ui_host: 'https://us.posthog.com',
rewriteRequestPath: (url) => {
if (url.pathname.startsWith('/e/')) {
url.pathname = '/ingest' + url.pathname
}
return url
},
})

Pin HTML snippet versions

The default HTML snippet loads /static/array.js, which stays on the latest v1 release.

Edit that path only for an explicit major pin or exact release rollback. Remove exact pins once debugging ends, so updates resume:

text
# Latest 1.x release (default)
/static/array.js
# Explicit major pin
/static/1/array.js
# Exact pin (minor pins like 1.380 return 404)
/static/1.380.0/array.js

If you serve pinned scripts through a CDN or proxy, set strict_script_versioning, so extension scripts use immutable versioned paths too. See keeping SDKs current and Security.

Attach tracing headers

Pass backend hostnames in tracing_headers (hostnames only, no protocol or path) to attach user and session headers to matching fetch and XHR requests:

JavaScript
posthog.init('<ph_project_token>', {
api_host: 'https://us.i.posthog.com',
tracing_headers: ['api.example.com'],
})

Product and advanced options

Still have questions?

Was this page useful?