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
1
| $index->search('query', ['userToken' => '123456'])
|
1
| index.search('query', {'userToken': '123456'})
|
1
| index.search('query', {'userToken': '123456'})
|
1
| index.search('query', {'userToken': '123456'})
|
1
2
3
4
5
6
7
8
9
10
11
12
13
| let index = client.index(withName: "your_index_name")
let requestOptions = RequestOptions()
requestOptions.headers["X-Algolia-UserToken"] = "123456"
index.search(
Query(query: "s"),
requestOptions: requestOptions,
completionHandler: { (content, error) -> Void in
if error == nil {
print("Result: \(content!)")
}
}
)
|
1
2
3
4
| index.search(
new Query("query")
.setUserToken("123456")
);
|
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")
)
}
|
1
2
3
4
5
| val query = query("query") {
userToken = UserToken("123456")
}
index.search(query)
|