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.
It’s best 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 the API clients, you need to:
- set the
userToken
search parameter to tell Algolia which user is searching,
- or set the
X-Forwarded-For
header to forward Algolia the user’s IP address if you perform the search from your back end.
- for better analytics aggregation performance, configure the unique user identifier (instead of relying on an IP address). For a back end search, configure either
userToken
or X-Forwarded-For
not both.
Set the userToken
search parameter
During a search
1
| $index->search('query', ['userToken' => '123456'])
|
1
| index.search('query', { userToken: '123456' })
|
1
2
3
4
5
| index.search('query', {
userToken: '123456'
}).then(({ hits }) => {
console.log(hits);
});
|
1
| index.search('query', {'userToken': '123456'})
|
1
2
3
4
5
6
7
8
| let query = Query("query")
.set(\.userToken, to: "123456")
index.search(query: query) { result in
if case .success(let response) = result {
print("Response: \(response)")
}
}
|
1
2
3
4
5
| val query = query("query") {
userToken = UserToken("123456")
}
index.search(query)
|
1
2
| Query query = new Query("query");
query.UserToken = "123456";
|
1
| Query query = new Query("query").setUserToken("123456")
|
1
2
3
4
| res, err := index.Search(
"query",
opt.UserToken("123456"),
)
|
1
2
3
4
5
6
| client.execute {
search into "myIndex" query Query(
query = Some("query"),
userToken = Some("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);
|
1
2
3
4
| config = Algolia::Search::Config.create('YourApplicationID', 'YourSearchOnlyAPIKey')
config.default_headers['X-Algolia-UserToken'] = '123456'
client = Algolia::Search::Client.create_with_config(config)
|
1
2
3
4
5
| const client = algoliasearch('YourApplicationID', 'YourSearchOnlyAPIKey', {
headers: {
'X-Algolia-UserToken': '123456'
}
});
|
1
2
3
4
5
6
| from algoliasearch.configs import SearchConfig
config = SearchConfig('YourApplicationID', 'YourSearchOnlyAPIKey')
config.headers['X-Algolia-UserToken'] = '123456'
client = SearchClient.create_with_config(config)
|
1
| client.setHeader(withName: "X-Algolia-UserToken", to: "123456")
|
1
2
3
4
5
6
| val configuration = ConfigurationSearch(
applicationID = ApplicationID("YourApplicationID"),
apiKey = APIKey("YourSearchOnlyAPIKey"),
defaultHeaders = mapOf("X-Algolia-UserToken" to "123456")
)
ClientSearch(configuration)
|
1
2
3
4
5
6
| var configuration = new SearchConfig("YourApplicationID", "YourSearchOnlyAPIKey")
{
DefaultHeaders = new Dictionary<string, string> { { "X-Algolia-UserToken"", "123456" } }
};
var client = new SearchClient(configuration);
|
1
2
3
4
5
6
| SearchConfig configuration =
new SearchConfig.Builder("YourApplicationID", "YourSearchOnlyAPIKey")
.addExtraHeaders("X-Algolia-UserToken", "123456")
.build();
SearchClient client = DefaultSearchClient.create(configuration);
|
1
2
3
4
5
6
7
8
9
| config := search.Configuration{
AppID: "YourApplicationID",
APIKey: "YourSearchOnlyAPIKey",
Headers: map[string]string{
"X-Algolia-UserToken": "123456",
},
}
client := search.NewClientWithConfig(config)
|
1
2
3
4
5
6
7
| val client = new AlgoliaClient(
"YourApplicationID",
"YourSearchOnlyAPIKey",
customHeader = Map(
"X-Algolia-UserToken" -> "123456"
)
)
|
Using InstantSearch
1
2
3
4
5
6
7
8
9
| const insightsMiddleware = instantsearch.middlewares.createInsightsMiddleware({
insightsClient,
onEvent,
});
search.use(insightsMiddleware);
// See https://www.algolia.com/doc/api-reference/widgets/insights/js/
// for more information about `insights` middleware.
|
1
2
3
4
5
| import { Configure } from 'react-instantsearch-dom';
<Configure
userToken="123456"
/>
|
1
2
3
| <ais-configure
:user-token="123456"
/>
|
1
2
3
| <ais-configure
[searchParameters]="{ userToken: '1234561'}"
</ais-configure>
|
1
2
3
4
5
6
7
| val query = query {
userToken {
'123456'
}
}
val searcher = HitsSearcher(client, indexName, query)
|
1
2
3
| let query = Query()
query.userToken = '123456'
let searcher = HitsSearcher(index: index, query: query)
|
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',
]);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
| # '94.228.178.246' should be replaced with the actual IP you would like
# to search around. Depending on your stack there are multiple ways to get this
# information.
# This is only needed when you're making a request from the backend. If you make
# your request in the browser, then this is added automatically.
index = client.init_index('your_index_name')
res = index.search('query string', {
headers: {
'X-Forwarded-For': '94.228.178.246'
}
})
|
1
2
3
4
5
6
7
8
9
| const index = client.initIndex('your_index_name');
index.search('query string', {
headers: {
'X-Forwarded-For': '94.228.178.246',
},
}).then(({ hits }) => {
console.log(hits);
});
|
1
2
3
4
5
| index = client.init_index('your_index_name')
res = index.search('query string', {
'X-Forwarded-For': '94.228.178.246',
})
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| let requestOptions = RequestOptions()
let index = client.index(withName: "your_index_name")
requestOptions.headers["X-Forwarded-For"] = "94.228.178.246"
index.search(
Query(query: "query string"),
requestOptions: requestOptions,
completionHandler: { (content, error) -> Void in
if error == nil {
print("Result: \(content!)")
}
}
)
|
1
2
3
4
5
6
7
8
| val indexName = IndexName("your_index_name")
val index = client.initIndex(indexName)
val query = Query("query string")
val requestOptions = requestOptions {
header("X-Forwarded-For", "94.228.178.246")
}
index.search(query, requestOptions)
|
1
2
3
4
5
6
7
8
| Index index = client.InitIndex("your_index_name");
RequestOptions requestOptions = new RequestOptions
{
Headers = new Dictionary<string,string>{ { "X-Forwarded-For", "94.228.178.246" } }
};
var result = index.Search<T>(new Query("query string"), requestOptions);
|
1
2
3
4
5
6
7
8
9
10
11
| //Sync version
index.search(
new Query("query string"),
new RequestOptions().addExtraHeader("X-Forwarded-For", "94.228.178.246")
);
//Async version
index.searchAsync(
new Query("query string"),
new RequestOptions().addExtraHeader("X-Forwarded-For", "94.228.178.246")
);
|
1
2
3
4
5
| extraHeaders := opt.ExtraHeaders(map[string]string{
"X-Forwarded-For": "94.228.178.246",
})
res, err = index.Search("query string", extraHeaders)
|
1
2
3
4
5
6
7
| client.execute {
search into "your_index_name" query Query(
query = Some("query string")
) options RequestOptions(
extraHeaders = Some(Map("X-Forwarded-For" => "94.228.178.246"))
)
}
|