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 clientaa,
see Initialize the Insights client.
Generate and store token in cookie
By default, the method generates a UUID-based anonymous token and stores it in a cookie:JavaScript
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 theinMemory parameter to true:
JavaScript
Use cases
UsesetAnonymousUserToken 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)
setUserToken instead.
When the user logs in, synchronize with the authenticated user ID using setAuthenticatedUserToken.
Parameters
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
_ALGOLIAcookie (or reuses an existing anonymous token from the cookie) - If cookies are not supported: the method doesn’t do anything
- This method triggers the
onUserTokenChangecallback if one is registered - The generated token is in UUID v4 format
- Cookie name:
_ALGOLIA - Cookie duration: defaults to 6 months (configurable with
cookieDuration)