Analytics tags let you view subsets of your analytics data, which, in turn, allows you to better understand the behavior of your users. Analytics tags are strings which you conditionally assign to specific searches. These strings identify an aspect of a search, which you believe will clarify the behavior of a specific group of users. An example will make this clearer:

Suppose you want to see analytics data for desktop and mobile users separately: by assigning desktop and mobile analytics tag, you can. This guide will take you through this process step by step.

Setup

In this tutorial, we illustrate two ways of implementing desktop and mobile analytics tags: with InstantSearch and with the API clients.

Identifying your user’s device

Before you can assign the desktop or mobile analytics tags to searches, you need to identify your user’s device. We can write a function that detects what device it’s running on, and returns “desktop” or “mobile” depending on what it finds:

1
2
3
4
5
const getPlatform = () => {
  const isMobile = ...
  // your logic to determine whether a user is on mobile or desktop
  return isMobile ? 'mobile' : 'desktop';
};

Fetching your user’s device makes more sense on the front end, which is why there are no back-end snippets. Note that the above script is a naive implementation of how to detect a device, we don’t recommend using it in production.

With InstantSearch

To set the user’s device at the beginning of their search session, you can add it to the searchParameters setting of your InstantSearch instance during initialization.

1
2
3
4
5
const platformTag = getPlatform();

instantsearch.widgets.configure({
  analyticsTags: [platformTag],
});

With an API client

You can also pass the user’s device using the API clients, by providing it as a search parameter whenever you call search on your index.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const index = client.initIndex('your_index_name');

const platformTag = getPlatform();

index.search(
  {
    query: 'query string',
    analyticsTags: platformTag
  },
  (err, { hits } = {}) => {
    if (err) throw err;

    console.log(hits);
  }
);

Viewing segmented analytics data

After you’ve implemented, tested, and released your app with desktop and mobile analytics tags, you can start segmenting your new analytics data based on your user’s platform.

You can do this from the Analytics tab of your Algolia dashboard. In the header of the Analytics tab you’ll find the Tags input field.

Once your users have started making searches to your new search implementation, the desktop and mobile analytics tags will appear in the Tags field. If you select mobile, you’ll only be shown searches made from mobiles and their associated metrics: this is true for any subsection of the Analytics tab.

You may have to wait awhile before you get enough data from mobile and desktop users to start drawing meaningful conclusions.

Did you find this page helpful?