> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Set anonymous user token

> Generate and set an anonymous user token for tracking user interactions.

**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](/doc/guides/sending-events/concepts/usertoken).

## Examples

For more information about initializing the JavaScript Insights client `aa`,
see [Initialize the Insights client](/doc/libraries/search-insights/init).

### Generate and store token in cookie

By default, the method generates a UUID-based anonymous token and stores it in a cookie:

```js JavaScript icon=code theme={"system"}
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.

<Note>
  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.
</Note>

### Generate token in memory only

To generate an anonymous token without storing it in a cookie, set the `inMemory` parameter to `true`:

```js JavaScript icon=code theme={"system"}
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`](/doc/libraries/search-insights/set-user-token) instead.

When the user logs in, synchronize with the authenticated user ID using [`setAuthenticatedUserToken`](/doc/libraries/search-insights/set-authenticated-user-token).

## Parameters

<ParamField body="inMemory" type="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
</ParamField>

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`](/doc/libraries/search-insights/init#param-cookie-duration))
