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). Storesdistinct_idand session ID in a cross-subdomain cookie, and everything else inlocalStorage.persistence: 'cookie'. Stores all data in a cookie.persistence: 'localStorage'. Stores everything inlocalStorage.persistence: 'sessionStorage'. Stores everything insessionStorage.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():
Analytics storage and consent storage
Analytics state and consent decisions use separate storage settings:
persistence. Stores analytics state such as identity and session information. Defaults tolocalStorage+cookie.opt_out_capturing_persistence_type. Stores consent decisions per origin. Defaults tolocalStorage.cookieWinsOnConflict. Resolves conflicts between shared analytics cookie values and that origin'slocalStorage. 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:
- A tab on
www.example.comstores an anonymous identity in itslocalStorage. - Your app calls
posthog.identify()onapp.example.comand updates the shared cookie. - The first tab remains open, or the visitor returns to
www.example.com. ItslocalStoragestill 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:
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.
Cookie-persisted properties
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:
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_idfrom the parent page and callposthog.identify(), or bootstrap PostHog with a distinct ID. - Subdomain or framed cookies. Setting
cross_subdomain_cookie: falsekeeps cookies scoped to the iframe's host, and cross-site framed apps that rely on first-party cookies requireSameSite=Noneover HTTPS.
Persistence caveats
- Subdomains.
localStorageandsessionStoragedon't work across subdomains. UselocalStorage+cookie(orcookie) to share identity or session state across sibling subdomains. - Large payloads. Store large payloads in
localStorage+cookierather thancookiealone to avoid431 Request Header Fields Too Largeserver errors. - No browser storage. Set
disable_persistence: trueto store nothing in the browser. If you know the user's stable ID at initialization, bootstrap it rather than callingposthog.identify()after load, so reloads don't merge fresh anonymous IDs. - Browser extensions. Use
localStorage,sessionStorage, ormemory, since each extension context is isolated and PostHog doesn't usebrowser.storageorchrome.storage. Supply a shareddistinct_idatinit(), and see browser extension docs.





