JavaScript web persistence and cookies
Contents
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, we store all this information in both a cookie and localStorage, which means PostHog can identify your users across subdomains. By default, the name of the cookie PostHog sets is ph_<project_api_key>_posthog and it expires after 365 days.
If you want to change how PostHog stores this information, you can do so with the persistence configuration option:
persistence: "localStorage+cookie"(default): Limited things are stored in the cookie such as the distinctID and the sessionID, and everything else in the browser'slocalStorage.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, which means data is only persisted for the duration of the page view.
To change persistence values without reinitializing PostHog, you can use the posthog.set_config() method. This enables you to switch from memory to cookies to better comply with privacy regulations.
Cookie-persisted properties
When using localStorage+cookie persistence (the default), most properties are stored in localStorage while only essential values like distinct_id and session ID go in the cookie. Since localStorage doesn't work across subdomains but cookies do, you can use the cookie_persisted_properties configuration option to specify additional properties that should be stored in the cross-subdomain cookie.
This is useful when you need specific properties to be available across subdomains. For example, you might want to track which products a user has shown interest in on your marketing site and use that data to personalize their onboarding experience on your app subdomain.
You can then set these properties using posthog.register():
Example: tracking user interests across subdomains
Here's an example of tracking which products a user has viewed on a marketing site, then using that data for personalized onboarding on an app subdomain (like we do!):
Warning: Cookie size limits
Cookies have a maximum size of approximately 4KB. If your
cookie_persisted_propertiesstore large arrays or complex objects, you may exceed this limit, which can cause:
- Properties being silently truncated or not stored
431 Request Header Fields Too Largeerrors from your server- Unexpected behavior when reading properties
Keep cookie-persisted values small (short strings, small arrays of IDs). For larger data, consider using
localStoragepersistence and a different cross-subdomain strategy, or store the data server-side.
Persistence caveats
Be aware that
localStorageandsessionStoragecan't be used across subdomains. If you have multiple sites on the same domain, you may want to consider acookieoption or make sure to set all super properties across each subdomain.Due to the size limitation of cookies you may run into
431 Request Header Fields Too Largeerrors (e.g. if you have a lot of feature flags). In that case, uselocalStorage+cookie.If you don't want PostHog to store anything on the user's browser (e.g. if you want to rely on your own identification mechanism only or want completely anonymous users), you can set
disable_persistence: truein PostHog's config. If you do this, remember to callposthog.identifyevery time your app loads. If you don't, every page refresh is treated as a new and different user.For browser extensions, use
localStorage,sessionStorage, ormemory. Each extension context may initialize its own PostHog instance. These contexts don't share storage so the instances don't know about each other. Sincebrowser.storageandchrome.storageAPIs are not supported for data persistence, you'll need to provide your own shareddistinct_idduring each initialization to ensure events are sent under the same identifier. See the browser extension documentation for more details.