How does the Search Insights JavaScript library handle user tokens?
The Search Insights library (version 2 and later) generates anonymous user tokens on every page load. By default, user tokens don’t persist. This helps with data protection regulations, but means you can’t distinguish new from returning users, since a new user token is generated on every page visit.See how the user token changes when a user refreshes the page
Version 1 of the Search Insights library generates anonymous user tokens and stores them in a cookie by default.
Persistent user token
For more accurate analytics and better personalization experiences, you should persist user tokens across sessions.Authenticated user token
If you’re using an authentication system for your users, use thesetAuthenticatedUserToken
method to set the authenticated user token as the authenticatedUserToken
parameter for the Insights client.
JavaScript
getAuthenticatedUserTokenAfterLogin()
function.
Always provide a userToken
since this is Algolia’s primary user identifier.
- If the user is authenticated, pass a
userToken
and anauthenticatedUserToken
. - If the user isn’t authenticated, only pass an anonymous
userToken
. Later, if they sign in, send the sameuserToken
and the newauthenticatedUserToken
so that Algolia can link the two session histories.
userToken
parameter.
Algolia cookie
To store theuserToken
in a first-party cookie with the name _ALGOLIA
on the user’s device,
set the useCookie
parameter in the init
method to true
.
This lets you identify users across sessions with the persistent, anonymous user token.
JavaScript
useCookie
to true
, you should get user consent.
For example, you can dynamically update the useCookie
parameter when the user accepts non-essential cookies.
Use the partial
parameter in the init
method to only update the useCookie
option without changing the others.
JavaScript
useCookie
parameter in the init
method as false
and only update its value dynamically once the user grants or withdraws consent.
For example, your code could look like this:
JavaScript
If you’re using a cookie consent service like OneTrust or TrustArc,
refer to their documentation to properly react to cookie consent updates.
Adjust the cookie expiration time
By default, the_ALGOLIA
cookie expires after 6 months.
To adjust the cookie’s lifespan, set the cookieDuration
parameter in the init
method.
JavaScript
Third-party cookies
Third-party cookies are stored on the user’s device and belong to domains other than the website the user is visiting. They enable advertisers, analytics platforms, and other third-party services to collect data about a user’s browsing behavior across multiple websites. To maintain the reliability of the collected data, providers of third-party cookies, such as Google Analytics or Segment often use a stable user token that remains consistent across multiple websites. You can use one of these third-party cookies as a user token in the Search Insights library.Google Analytics
Google Analytics stores its persistent, anonymous user token in a cookie called_ga
.
If you’re already using Google Analytics on your website, you can use that user token as the Search Insights library’s userToken
parameter.
The value of the _ga
cookie follows the pattern GA1.1.1900000000.1684510679
and has three components:
GA1.1.
. Indicates the version of the Google Analytics tracking library used to generate the cookie.1900000000
. A unique identifier for distinguishing individual users.1684510679
. The timestamp when the cookie was created, in Unix epoch time.
JavaScript
userToken
for Search Insights with setUserToken
.
JavaScript
Segment
Segment stores its persistent, anonymous user token in a cookie calledajs_anonymous_id
.
If you’re already using Segment on your website,
you can use that user token as the Search Insights library’s userToken
parameter.
Using Segment’s Analytics.js 2.0 library,
you can get the user token from the function analytics.user().anonymousId()
.
To learn more, see:
Get the user token from Search Insights
With the Search Insights library, you can retrieve the anonymous user token by callinggetUserToken
.
JavaScript
getAuthenticatedUserToken
.
JavaScript
Send user token with click and conversion events
The Search Insights library sets a default anonymoususerToken
.
You can also manually set the userToken
or authenticatedUserToken
parameter in two ways:
either globally or for each event.
To set userToken
or authenticatedUserToken
globally, you can pass them on init
or use the setUserToken
or
setAuthenticatedUserToken
method.
This way, you don’t need to provide your user token every time you send an event.
JavaScript
userToken
and authenticatedUserToken
parameters to the event object.
JavaScript
Exclude users who want to opt out of Analytics, Recommend, and Personalization
To allow users to opt out of Analytics, Recommend, and Personalization features:- Don’t instantiate the Insights API client for users who have opted out.
- Set the parameters
analytics
andenablePersonalization
tofalse
to turn off these features.