Skip to main content
Required ACL: search Use this method to automatically generate an anonymous user token with the anonymous- prefix. This method can either store the token in a cookie (default) or keep it in memory only. The anonymous token is persisted in a cookie by default, so returning users maintain the same token across sessions. For more information, see User token.

Examples

For more information about initializing the JavaScript Insights client aa, see Initialize the Insights client. By default, the method generates a UUID-based anonymous token and stores it in a cookie:
JavaScript
aa("setAnonymousUserToken");
// Generates token like: "anonymous-f8e8a8c7-5b5a-4b8c-9d8e-7f6e5d4c3b2a"
// Stores it in a cookie for 6 months
If a cookie with an anonymous token already exists (with the anonymous- prefix), it will reuse that token instead of generating a new one.
If a user’s browser doesn’t support cookies, this method doesn’t do anything. Use setAnonymousUserToken(true) to generate an in-memory token as a fallback.

Generate token in memory only

To generate an anonymous token without storing it in a cookie, set the inMemory parameter to true:
JavaScript
aa("setAnonymousUserToken", true);
// Generates a new anonymous token in memory only
// Token is lost when the page is refreshed
// A new token is generated each time this method is called

Use cases

Use setAnonymousUserToken when:
  • You want automatic anonymous token generation with the anonymous- prefix
  • You need cookie-based persistence for anonymous users across sessions
  • You’re implementing GDPR-compliant tracking (generate in-memory tokens for users who haven’t consented to cookies)
If you need more control over the token value, use setUserToken instead. When the user logs in, synchronize with the authenticated user ID using setAuthenticatedUserToken.

Parameters

inMemory
boolean
default:false
Whether to generate a new anonymous token and store it in memory only (not in cookies). A new token is generated with each call.If false or omitted:
  • If cookies are supported: generates the token and persists it in the _ALGOLIA cookie (or reuses an existing anonymous token from the cookie)
  • If cookies are not supported: the method doesn’t do anything
Additional usage information:
  • This method triggers the onUserTokenChange callback if one is registered
  • The generated token is in UUID v4 format
  • Cookie name: _ALGOLIA
  • Cookie duration: defaults to 6 months (configurable with cookieDuration)
Last modified on February 10, 2026