🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
Search analytics / Guides

Segment your analytics data

Analytics tags are string labels for categorizing search queries. Searches with the same tag form segments. By comparing analytics data based on segments, you can identify discrepancies that may not be visible in the overall view.

Examples for segments

Examples for segments include:

Segment Description
Platform Compare mobile and desktop searches.
User cohorts Compare different user groups with data from their accounts, such as gender and age.
Search language Compare different search languages.
Region Compare different geographical regions.

Segments by platform

To determine whether a user searches from a mobile device or a desktop, you typically need to check the browser’s user agent. Since these are constantly changing, the following function is just an illustration:

1
2
3
4
5
6
7
function getPlatform() {
  return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
    navigator.userAgent
  )
    ? 'mobile'
    : 'desktop'
}

You can add the result of this function to the analyticsTags parameter.

Segments by first or returning users

To determine if a user is new or returning, it’s best to obtain this information from your backend.

If you don’t store this information, you can use cookies, local storage, or session storage. However, cookies aren’t completely reliable as users can delete or block them. Cookies also don’t work across multiple devices.

To use a cookie to check if a user is returning or not, use the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const decodedCookies = decodeURIComponent(document.cookie).split(';');

const { myCookie: isReturning } = Object.assign(
  {},
  ...decodedCookies.map(cookie => {
    const [key, value] = cookie.split('=');
    return { [key.trim()]: value };
  })
);

const visitingStatus = isReturning ? 'Returning' : 'New';

if (!isReturning) {
  document.cookie = 'myCookie=1';
}

Then, you can add visitingStatus to the analyticsTags parameter.

You may need the user’s consent before storing information on the user’s device, including cookies, local storage, and session storage.

Add analytics tags in InstantSearch

Add your tags to the analyticsTags parameter in a configure widget, or add it to the search parameters of your search query:

1
2
3
instantsearch.widgets.configure({
  analyticsTags: [YourAnalyticsTags],
});

Add analytics tags in API clients

Add your tags to analyticsTags as an extra search parameter:

1
2
3
4
5
$results = $index->search('', [
  'analyticsTags' => [
    YourAnalyticsTags
  ]
]);

View segmented analytics

To view segmented analytics, see Search analytics in the Algolia dashboard.

Did you find this page helpful?