Skip to contentSkip to navigationSkip to topbar
On this page
Looking for more inspiration?Visit the
(information)
You're in the right place! Segment documentation is now part of Twilio Docs. The content you are used to is still here—just in a new home with a refreshed look.

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.


Segment ID persistence

segment-id-persistence page anchor

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.

Device-mode destination cookies

device-mode-destination-cookies page anchor

Segment doesn't control cookie management for device-mode destinations. As a result, the way cookies are used and managed is solely determined by each individual SDK. For example, if you have concerns about destinations setting third-party cookies, Segment recommends that you consult directly with the destination providers for clarification. For instance, Amplitude, one of the destinations in the Segment catalog, provides an informative article on this topic(link takes you to an external 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:

OptionDescriptionDefault value
persistThis toggles storing user information locally.true
cookie.keyName of the cookie used to store the user ID.ajs_user_id
cookie.oldKeyName of a cookie previously used to store the user ID. Will be read if cookie.key can't be found.ajs_user
localStorage.keyName of the key used to store user traits in localStorage.ajs_user_traits

Example:

1
analytics.load('writeKey', {
2
user: {
3
persist: true,
4
cookie: {
5
key: 'ajs_user_id'
6
},
7
localStorage: {
8
key: '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:

FieldDescriptionDefault value
persistToggles storing group information locally.true
cookie.keyName of the cookie used to store the group id.ajs_group_id
localStorage.keyName of the key used to store user traits in localStorage.ajs_group_properties

Example:

1
analytics.load('writeKey', {
2
group: {
3
persist: true,
4
cookie: {
5
key: 'ajs_group_id'
6
},
7
localStorage: {
8
key: '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.


Disable all client-side persistence

disable-all-client-side-persistence page anchor

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.

1
analytics.load('writeKey', {
2
// Global Storage Priority: Both User and Group data
3
storage: {
4
stores: ['cookie', 'localStorage', 'memory']
5
},
6
// Specific Storage Priority
7
user: {
8
storage: {
9
stores: ['cookie', 'localStorage', 'memory']
10
}
11
},
12
group: {
13
storage: {
14
stores: ['cookie', 'localStorage', 'memory']
15
}
16
},
17
}