mirror of
https://github.com/BillyOutlast/posthog.com.git
synced 2026-02-04 03:11:21 +01:00
* split ios sdk pages * Update contents/docs/libraries/ios/index.mdx Co-authored-by: Ian Vanagas <34755028+ivanagas@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Ian Vanagas <34755028+ivanagas@users.noreply.github.com> * review comments * Autocapture, config table * Apply suggestion from @ivanagas Co-authored-by: Ian Vanagas <34755028+ivanagas@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Ian Vanagas <34755028+ivanagas@users.noreply.github.com> * Unique import names * Address comments --------- Co-authored-by: Ian Vanagas <34755028+ivanagas@users.noreply.github.com> Co-authored-by: Eli Kinsey <eli@ekinsey.dev>
41 lines
2.7 KiB
Plaintext
41 lines
2.7 KiB
Plaintext
---
|
|
title: JavaScript web persistence and cookies
|
|
sidebar: Docs
|
|
showTitle: true
|
|
---
|
|
|
|
import PersistenceInfo from "./_snippets/persistence-info.mdx"
|
|
|
|
<PersistenceInfo />
|
|
|
|
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's <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage" target="_blank">`localStorage`</a>.
|
|
|
|
- `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, 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.
|
|
|
|
```js-web
|
|
const handleCookieConsent = (consent) => {
|
|
posthog.set_config({ persistence: consent === 'yes' ? 'localStorage+cookie' : 'memory' });
|
|
localStorage.setItem('cookie_consent', consent);
|
|
};
|
|
```
|
|
|
|
## Persistence caveats
|
|
|
|
- Be aware that `localStorage` and `sessionStorage` can't be used across subdomains. If you have multiple sites on the same domain, you may want to consider a `cookie` option 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 Large` errors (e.g. if you have a lot of feature flags). In that case, use `localStorage+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: true` in PostHog's config. If you do this, remember to call [`posthog.identify`](/docs/libraries/js/features#identifying-users) **every 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`, or `memory`. Each extension context may initialize its own PostHog instance. These contexts don't share storage so the instances don't know about each other. Since `browser.storage` and `chrome.storage` APIs are not supported for data persistence, you'll need to provide your own shared `distinct_id` during each initialization to ensure events are sent under the same identifier. See the [browser extension documentation](/docs/advanced/browser-extension) for more details.
|