How to Set the Analytics UserToken
By default, our front-end integrations use the user’s IP address as the
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
userTokensearch parameter to tell us which user is searching,
- or set the
X-Forwarded-Forheader to forward us the user’s IP address if you perform the search from your back end.
userToken search parameter
During a search
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);
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' => '126.96.36.199', ]);