JavaScript web persistence and cookies

For PostHog to work optimally, we store a small amount of information about the user on the user's browser. This ensures we identify users properly if they navigate away from your site and come back later.

The information we store includes:

  • Their distinct_id
  • Session ID & Device ID
  • Active & enabled feature flags
  • Any super properties you have defined
  • Some PostHog configuration options (e.g. whether session recording is enabled)

By default, PostHog uses localStorage+cookie persistence. It stores the full state in localStorage and a smaller identity and session subset in a first-party cookie. This enables PostHog to identify visitors across sibling subdomains that can access the cookie. The cookie name is ph_<project_token>_posthog, and it expires after 365 days.

To change how PostHog stores this information, set persistence in posthog.init(). See PostHogConfig in the SDK reference for all storage options:

  • persistence: 'localStorage+cookie' (default). Stores distinct_id and session ID in a cross-subdomain cookie, and everything else in localStorage.
  • persistence: 'cookie'. Stores all data in a cookie.
  • persistence: 'localStorage'. Stores everything in localStorage.
  • persistence: 'sessionStorage'. Stores everything in sessionStorage.
  • persistence: 'memory'. Stores everything in page memory for the current page view only.

Update persistence at runtime without reinitializing PostHog by calling posthog.set_config():

Web
const handleCookieConsent = (consent) => {
const mode =
consent === 'yes' ? 'localStorage+cookie' : 'memory'
posthog.set_config({ persistence: mode })
localStorage.setItem('cookie_consent', consent)
}

Analytics state and consent decisions use separate storage settings:

  • persistence. Stores analytics state such as identity and session information. Defaults to localStorage+cookie.
  • opt_out_capturing_persistence_type. Stores consent decisions per origin. Defaults to localStorage.
  • cookieWinsOnConflict. Resolves conflicts between shared analytics cookie values and that origin's localStorage. It doesn't share consent decisions.

Both localStorage and PostHog cookies are first-party storage. Their scopes differ because localStorage belongs to one origin, while cookies work across sibling subdomains.

Sharing an analytics identity doesn't share consent. Neither persistence: 'cookie' nor cookieWinsOnConflict: true changes where consent lives.

To share one consent decision across subdomains, set opt_out_capturing_persistence_type: 'cookie' on each. See share consent across subdomains and Privacy and consent.

Synchronize identity and sessions across subdomains

With localStorage+cookie, localStorage belongs to one origin while sibling subdomains share the first-party PostHog cookie under cross_subdomain_cookie. Both stores can hold the same key and conflict, for example:

  1. A tab on www.example.com stores an anonymous identity in its localStorage.
  2. Your app calls posthog.identify() on app.example.com and updates the shared cookie.
  3. The first tab remains open, or the visitor returns to www.example.com. Its localStorage still contains the anonymous identity.

When cookieWinsOnConflict is disabled (the default when defaults is unset), the stale localStorage value wins. The tab can then capture events with the old identity or session and write that old state back to the shared cookie.

Set cookieWinsOnConflict: true to make the shared cookie authoritative for the keys it contains:

JavaScript
posthog.init("<ph_project_token>", {
api_host: "https://us.i.posthog.com",
persistence: "localStorage+cookie",
cross_subdomain_cookie: true,
cookieWinsOnConflict: true,
})

This option requires posthog-js 1.418.0 or later and applies only to localStorage+cookie persistence. The SDK enables it by default with defaults: '2026-08-29' or later, and leaves it off when defaults is unset or earlier.

An explicit cookieWinsOnConflict value overrides the snapshot default.

When enabled, PostHog synchronizes these cookie-backed values:

  • Identity state. Distinct ID, device ID, and anonymous or identified state.
  • Session state. Session ID, last activity time, and session start time.
  • Replay and person state. Session Replay sampling state, person-processing state, and initial-person information.
  • Custom super properties. Properties you add with cookie_persisted_properties.

At initialization, cookie values override matching values in that subdomain's localStorage. PostHog then updates localStorage with the synchronized state.

In an open tab, PostHog checks for shared-cookie changes before captures and persistence writes rather than immediately when another subdomain changes the cookie.

PostHog can adopt a shared identity during any of these checks. A persistence write can record the adoption without reloading feature flags right away.

When PostHog next processes the adopted identity, such as before a capture, it clears identity-bound flag state and reloads flags. onFeatureFlags callbacks run after the reload finishes.

If the session ID changes, posthog.onSessionId() callbacks run when the tab next checks its session, such as during the next capture. A synchronized reset() also clears event and session properties that belonged to the previous identity.

This setting synchronizes state only between sibling subdomains that share the first-party cookie. It doesn't enable cross-site or third-party cookie tracking.

With localStorage+cookie (default), most properties stay in localStorage while essential values like distinct_id and session ID go in the cookie. Pass keys to cookie_persisted_properties when you also need specific super properties readable across subdomains:

JavaScript
posthog.init('<ph_project_token>', {
api_host: 'https://us.i.posthog.com',
persistence: 'localStorage+cookie',
cookie_persisted_properties: [
'product_interests',
'signup_source',
],
})

cookie_persisted_properties controls which extra keys live in the cookie, while cookieWinsOnConflict resolves conflicts with localStorage. Register values with posthog.register() and read them back with posthog.get_property().

Keep cookie values compact. Cookies have about a 4 KB total limit, so oversized cookies can truncate or cause 431 Request Header Fields Too Large errors.

Cookies in iframes

If you embed an app inside an iframe across sibling subdomains or separate domains:

  • Cross-site iframes. Browsers treat cookies inside a cross-site iframe as third-party cookies and often block or partition them. Pass the visitor's distinct_id from the parent page and call posthog.identify(), or bootstrap PostHog with a distinct ID.
  • Subdomain or framed cookies. Setting cross_subdomain_cookie: false keeps cookies scoped to the iframe's host, and cross-site framed apps that rely on first-party cookies require SameSite=None over HTTPS.

Persistence caveats

  • Subdomains. localStorage and sessionStorage don't work across subdomains. Use localStorage+cookie (or cookie) to share identity or session state across sibling subdomains.
  • Large payloads. Store large payloads in localStorage+cookie rather than cookie alone to avoid 431 Request Header Fields Too Large server errors.
  • No browser storage. Set disable_persistence: true to store nothing in the browser. If you know the user's stable ID at initialization, bootstrap it rather than calling posthog.identify() after load, so reloads don't merge fresh anonymous IDs.
  • Browser extensions. Use localStorage, sessionStorage, or memory, since each extension context is isolated and PostHog doesn't use browser.storage or chrome.storage. Supply a shared distinct_id at init(), and see browser extension docs.

Still have questions?

Was this page useful?