Skip to main content
By default, Algolia uses IP addresses to distinguish users. To get more accurate analytics, set a userToken on search requests. If you send searches from your backend, set a userToken to avoid grouping many users under the same server IP address. To learn what a user token is, how to generate it, and how to handle anonymous and authenticated users, see User token.
A userToken is also required for Personalization.

Before you begin

Use the same userToken for searches and click and conversion events so analytics can associate activity with the same user.

Set the user token in InstantSearch and Autocomplete

Set the insights option to true when initializing InstantSearch or Autocomplete.
import { autocomplete } from "@algolia/autocomplete-js";
import algoliasearch from "algoliasearch/lite";

const appID = "ALGOLIA_APPLICATION_ID";
const apiKey = "ALGOLIA_SEARCH_API_KEY";

const searchClient = algoliasearch(appID, apiKey);

autocomplete({
	container: "#autocomplete",
	placeholder: "Search ...",
	insights: true,
	getSources({ query }) {
		// ...
	},
});

// Set an anonymous user token
window.aa("setUserToken", "ANONYMOUS_USER_TOKEN");

// Set an authenticated user token
window.aa("setAuthenticatedUserToken", "AUTHENTICATED_USER_TOKEN");
For more information, see:

Set the user token in InstantSearch iOS and Android

Add the userToken as an API parameter to your search requests:
val query = query {
    userToken {
        'ANONYMOUS_USER_TOKEN'
    }
}

val searcher = HitsSearcher(client, indexName, query)

Set the user token with API clients

Use the same userToken in search requests and click and conversion events. To set the user token per search request:
var response = await client.SearchSingleIndexAsync<Hit>(
  "INDEX_NAME",
  new SearchParams(new SearchParamsObject { Query = "query", UserToken = "user-1234" })
);
To set the user token when you initialize an API client:
namespace Algolia;

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using Algolia.Search.Clients;
using Algolia.Search.Http;
using Algolia.Search.Models.Search;

class GlobalAlgoliaUserID
{
  async Task Main(string[] args)
  {
    var client = new SearchClient(
      new SearchConfig("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY")
      {
        DefaultHeaders = new Dictionary<string, string>
        {
          { "X-Algolia-UserToken", "test-user-123" },
        },
      }
    );
    Console.WriteLine(client);
  }
}

If you can’t set a user token

If you search from your backend and can’t or don’t want to set a userToken, you can forward user IP addresses to improve analytics accuracy.
Last modified on April 14, 2026