API client / Methods / Insights

Set authenticated user token

This method is only available for JavaScript.

Required API Key: any key with the search ACL
Method signature
Only available for JavaScript

We released a new version of the PHP API client in public beta. Read the beta documentation for more information.

We released a new version of the Java API client in public beta. Read the beta documentation for more information.

You’re currently reading the Ruby API client v2 documentation. Check the migration guide to learn how to upgrade from v1 to v2. You can still access the v1 documentation.

About this method

Set the authenticated user token for all subsequent events sent to the Algolia Insights API.

Use this method to automatically send the authenticatedUserToken with every event.


Read the Algolia CLI documentation for more information.

The following example initializes the Insights API client aa. If the user isn’t identified initially, you can let the Insights API client generate an anonymous user token, and persist it in the cookies with the useCookie option.

import aa from "search-insights";

aa('init', {
  appId: 'YourApplicationID',
  apiKey: 'YourSearchOnlyAPIKey',
  useCookie: true,

When user logs in, set the authenticated user token to the user ID coming from your system.

aa('setAuthenticatedUserToken', 'YourUserID');

Now, both the anonymous and authenticated tokens will be sent with each event to ensure that data collected when users weren’t authenticated isn’t lost.

When users log out, you can unset the authenticated token by passing undefined.

aa('setAuthenticatedUserToken', undefined);


Parameter Description
type: string | undefined
pattern: [A-Za-z0-9_=+/-]{1,129}

Pseudonymous identifier for authenticated users.

Never include personally identifiable information in user tokens.

See also: User Token


This method doesn't return a response.

Did you find this page helpful?