By default, Algolia uses the IP address to distinguish users.
For more accurate analytics, you can explicitly set a user token.
This is especially important if you’re searching from your backend,
as all searches would have the IP address of your server.
It’s best to create reliable unique identifiers that you store in your app or database.
For example, you could use your own user identifiers once users sign in.
You must send a user token with your search requests
if you use Personalization.
Set the user token in InstantSearch and Autocomplete
If you’re using Autocomplete, InstantSearch.js, React InstantSearch,
or Vue InstantSearch, set the insights option to true when initializing the library.
import{autocomplete}from"@algolia/autocomplete-js";importalgoliasearchfrom"algoliasearch/lite";constappID="YourApplicationID"constapiKey="YourSearchAPIKey"constsearchClient=algoliasearch(appID,apiKey);autocomplete({container:"#autocomplete",placeholder:"Search ...",insights:true,getSources({query}){// ...},});// Set anonymous user tokenwindow.aa("setUserToken","anonymous-user-123");// Set authenticated user tokenwindow.aa("setAuthenticatedUserToken","authenticated-user-123");
1
2
3
4
5
6
7
8
9
10
11
12
13
importinstantsearchfrom'instantsearch.js';constsearch=instantsearch({indexName:"YourIndexName",searchClient,insights:true})// Set anonymous user tokenwindow.aa("setUserToken","anonymous-user-123");// Set authenticated user tokenwindow.aa("setAuthenticatedUserToken","authenticated-user-123");
1
2
3
4
5
6
7
8
9
10
11
12
import{InstantSearch}from"react-instantsearch";<InstantSearchindexName="YourIndexName"searchClient={searchClient}insights={true}>{/* ... */}</InstantSearch>// Set your own user tokenwindow.aa("setUserToken","test-user-123")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<template><ais-instant-search:index-name="<index-name>"
:search-client="searchClient"
:insights="true"
>
<!-- widgets --></ais-instant-search></template><script>// Set your own user tokenwindow.aa("setUserToken","test-user-123")</script>
If you’re using React InstantSearch, Angular InstantSearch, InstantSearch iOS, or InstantSearch Android,
add the userToken as an API parameter to your search requests: