Guides / Getting insights and analytics / Search analytics / Out-of-the-box analytics

How to Set the Analytics UserToken

For query aggregation, search analytics, and Personalization to work properly, the analytics engine needs to uniquely identify each of your users. You can do this with the userToken parameter.

By default, our front-end integrations use the user’s IP address as the userToken. However, we strongly recommend against relying on this default behavior. Relying on the IP address could cause people on the same Wi-Fi network to be treated as one person (since they would share the same IP address), even though there might be numerous people on your search simultaneously. Conversely, someone who uses your application on different networks (at home, on the commute, at work, etc.) would be identified as several different users.

We recommend to explicitly create a reliable unique identifier that you store in your application or database. For example, you can use your own user identifiers once users log in.

When using one of our API clients, you need to:

  • set the userToken search parameter to tell us which user is searching,
  • or set the X-Forwarded-For header to forward us the user’s IP address if you perform the search from your back end.

Set the userToken search parameter

1
$index->search('query', ['userToken' => '123456'])

During client initialization

1
2
3
4
5
6
$config = \Algolia\AlgoliaSearch\Config\SearchConfig::create('YourApplicationID', 'YourSearchOnlyAPIKey');
$config->setDefaultHeaders([
    'X-Algolia-UserToken' => '123456',
]);

$client = \Algolia\AlgoliaSearch\SearchClient::createWithConfig($config);

Set the X-Forwarded-For header

You only have to set this header if you perform your search from your back end. Please make sure to replace the IP address in the snippet with the actual IP address of your end user.

1
2
3
4
5
$index = $client->initIndex('your_index_name');

$res = $index->search('query string', [
    'X-Forwarded-For' => '94.228.178.246',
]);

Did you find this page helpful?