How to Set the Analytics User Id

For analytics query aggregation to work properly, the analytics engine relies on a user id.

Many front-end solutions tag every search with the end-user’s HTTP request IP. However, it is important to not rely on this behavior. You should explicitly create a unique user identifier that you maintain in your own application. For example, you could use a unique id you store in a database.

When using a back-end API client, you want to:

  • set the X-Forwarded-For header to forward the user IP to Algolia
  • set the userToken search parameter to tell Algolia which user is querying

Set the user token

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

Did you find this page helpful?