Client-side persistence in Analytics.js
This page explains what data Analytics.js stores on the client and shows you how to modify or override that storage behavior.
To ensure high-fidelity, first-party customer data, Segment writes the user's IDs to the user's local storage and uses them as the Segment ID in the cookie whenever possible. Local storage is intended for storing first-party customer information.
If a user returns to your site after the cookie expires, Analytics.js looks for an old ID in the user's localStorage, and if one is found, it sets it as the user's ID again in the new cookie. If a user clears their cookies and localStorage, all of the IDs are removed, and the user gets a completely new anonymousID when they next visit the page.
Analytics.js automatically persists the user's ID and traits locally. You can override how and where the user ID and traits are stored when loading Analytics.js by passing in a user object to the load method.
The user object has the following fields and default values:
| Option | Description | Default value |
|---|---|---|
persist | This toggles storing user information locally. | true |
cookie.key | Name of the cookie used to store the user ID. | ajs_user_id |
cookie.oldKey | Name of a cookie previously used to store the user ID. Will be read if cookie.key can't be found. | ajs_user |
localStorage.key | Name of the key used to store user traits in localStorage. | ajs_user_traits |
Example:
1analytics.load('writeKey', {2user: {3persist: true,4cookie: {5key: 'ajs_user_id'6},7localStorage: {8key: 'ajs_user_traits'9}10}11})
Analytics.js automatically persists the user's group ID and group properties locally. You can override how and where the group ID and properties are stored when loading Analytics.js by passing in a group object to the load method.
The group object has the following fields and default values:
| Field | Description | Default value |
|---|---|---|
persist | Toggles storing group information locally. | true |
cookie.key | Name of the cookie used to store the group id. | ajs_group_id |
localStorage.key | Name of the key used to store user traits in localStorage. | ajs_group_properties |
Example:
1analytics.load('writeKey', {2group: {3persist: true,4cookie: {5key: 'ajs_group_id'6},7localStorage: {8key: 'ajs_group_properties'9}10}11})
When enabled, Analytics.js automatically retries network and server errors. When the client is offline or your application can't connect to Segment's API, Analytics.js stores events in localStorage and falls back to in-memory storage when localStorage is unavailable.
Analytics.js supports disabling persisting any data locally. This will force analytics.js to store data in-memory only and disables automatic identity tracking across pages.
You can completely disable client-side persistence when loading Analytics.js by setting disableClientPersistence to true.
analytics.load('writeKey', { disableClientPersistence: true })
When disableClientPersistence is set to true, Analytics.js won't be able to automatically keep track of a user's identity when navigating to different pages. This can cause increased MTU usage if the anonymous usage can't be associated with a userId.
You can still manually track identity by calling analytics.identify() with the known identity on each page load, or you can pass in identity information to each page using the querystring API.
Analytics.js tries to detect when a page is about to be closed and saves pending events to localStorage. When the user navigates to another page within the same domain, Analytics.js attempts to send any events it finds in localStorage.
When disableClientPersistence is set to true, Analytics.js won't store any pending events into localStorage.
By default, Analytics.js uses localStorage as its preferred storage location, with Cookies as a fallback when localStorage is not available or not populated. An in-memory storage is used as a last fallback if all the previous ones are disabled.
Default storage priority:
LocalStorage -> Cookie -> InMemory
Some scenarios might require a switch in the storage systems priority:
- Apps that move the user across different subdomains
- Apps where the server needs control over the user data
- User Consent
- Availability
You can configure the storage priority in the Analytics.js client using the storage property, either globally or only for user or group data.
The storage property accepts an array of supported storage names (localStorage, cookie, memory) to be used in the priority order of the array.
1analytics.load('writeKey', {2// Global Storage Priority: Both User and Group data3storage: {4stores: ['cookie', 'localStorage', 'memory']5},6// Specific Storage Priority7user: {8storage: {9stores: ['cookie', 'localStorage', 'memory']10}11},12group: {13storage: {14stores: ['cookie', 'localStorage', 'memory']15}16},17}