Bind search analytics events to a user
By default, Meilisearch uses IP addresses to identify users and calculate the total user metrics. This guide shows you how to use the X-MS-USER-ID
HTTP header to manually link analytics events to specific users.
This is useful if you're searching from your back end, as all searches would otherwise appear to come from your server's IP address, making it difficult to accurately track the number of individual users.
Requirements
- A Meilisearch Cloud project with analytics and monitoring enabled
- A working pipeline for submitting analytics events
Add X-MS-USER-ID
to your search query
Include the X-MS-USER-ID
header in your search requests:
curl \
-X POST 'http://localhost:7700/indexes/INDEX_NAME/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer DEFAULT_SEARCH_API_KEY' \
-H 'X-MS-USER-ID: MEILISEARCH_USER_ID' \
--data-binary '{}'
Replace MEILISEARCH_USER_ID
with any value that uniquely identifies that user. This may be an authenticated user's ID when running searches from your own back end, or a hash of the user's IP address.
Add X-MS-USER-ID
to the analytics event
Next, submit your analytics event to the analytics endpoint. Send the same header and value in your API call:
curl \
-X POST 'https://edge.meilisearch.com/events' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer DEFAULT_SEARCH_API_KEY' \
-H 'X-MS-USER-ID: MEILISEARCH_USER_ID' \
--data-binary '{
"eventType": "click",
"eventName": "Search Result Clicked",
"indexUid": "products",
"objectId": "0",
"position": 0
}'
Conclusion
In this guide you have seen how to bind analytics events to specific users by specifying the same HTTP header for both the search request and the analytics event.