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 session identifiers once users start their session.
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}){// ...},});// You can also use insights to set an anonymous user tokenwindow.aa("setUserToken","anonymous-user-123");// You can also use insights to set an authenticated user tokenwindow.aa("setAuthenticatedUserToken","authenticated-user-123");
importinstantsearchfrom'instantsearch.js';constsearch=instantsearch({indexName:"YourIndexName",searchClient,insights:{insightsInitParams:{// Set anonymous user tokenuserToken:'anonymous-user-123',// Set authenticated user tokenauthenticatedUserToken:'authenticated-user-123',},}})// You can also use insights to set an anonymous user tokenwindow.aa("setUserToken","anonymous-user-123");// You can also use insights to set an authenticated user tokenwindow.aa("setAuthenticatedUserToken","authenticated-user-123");
import{InstantSearch}from"react-instantsearch";<InstantSearchindexName="YourIndexName"searchClient={searchClient}insights={{insightsInitParams:{// Set your own user tokenuserToken:'anonymous-user-123',// Set an authenticated user tokenauthenticatedUserToken:'authenticated-user-123',},}}>{/* ... */}</InstantSearch>// You can also use insights to set an anonymous user tokenwindow.aa("setUserToken","test-user-123");// You can also use insights to set an authenticated user tokenwindow.aa("setAuthenticatedUserToken","authenticated-user-123");
<template><ais-instant-search:index-name="<index-name>"
:search-client="searchClient"
:insights="insights"
>
<!-- widgets --></ais-instant-search></template><script>// You can also use insights to set an anonymous user tokenwindow.aa("setUserToken","test-user-123");// You can also use insights to set an authenticated user tokenwindow.aa("setAuthenticatedUserToken","authenticated-user-123");exportdefault{data(){return{insights:{insightsInitParams:{// Set your own user tokenuserToken:'anonymous-user-123',// Set an authenticated user tokenauthenticatedUserToken:'authenticated-user-123',}}}}}</script>