> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Segment your analytics data

> Use analytics tags to segment your 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:

```js JavaScript icon=code theme={"system"}
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`](/doc/api-reference/api-parameters/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:

```js JavaScript icon=code theme={"system"}
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`](/doc/api-reference/api-parameters/analyticsTags) parameter.

<Check>
  You may need the user's consent before storing information on the user's device,
  including cookies, local storage, and session storage.
</Check>

## Add analytics tags in InstantSearch

Add your tags to the `analyticsTags` parameter in a [`configure`](/doc/api-reference/widgets/configure/js) widget,
or add it to the search parameters of your search query:

<CodeGroup>
  ```js InstantSearch.js theme={"system"}
  instantsearch.widgets.configure({
  	analyticsTags: [YourAnalyticsTags],
  });
  ```

  ```jsx React theme={"system"}
  import { Configure } from "react-instantsearch";

  <Configure analyticsTags={[YourAnalyticsTags]} />;
  ```

  ```html Vue theme={"system"}
  <ais-configure
    :analytics-tags.camel="[YourAnalyticsTags]"
  />
  ```

  ```kotlin Android theme={"system"}
  val query = query { analyticsTags { +YourAnalyticsTags } }

  val searcher = HitsSearcher(client, indexName, query)
  ```

  ```swift iOS theme={"system"}
  let query = Query()
  query.analyticsTags = [YourAnalyticsTags]
  let searcher = HitsSearcher(index: index, query: query)
  ```
</CodeGroup>

## Add analytics tags in API clients

Add your tags to `analyticsTags` as an extra search parameter:

<CodeGroup>
  ```cs C# theme={"system"}
  var response = await client.SearchSingleIndexAsync<Hit>(
    "INDEX_NAME",
    new SearchParams(
      new SearchParamsObject { AnalyticsTags = new List<string> { "YOUR_ANALYTICS_TAG" } }
    )
  );
  ```

  ```dart Dart theme={"system"}
  final response = await client.searchSingleIndex(
    indexName: "INDEX_NAME",
    searchParams: SearchParamsObject(
      analyticsTags: [
        "YOUR_ANALYTICS_TAG",
      ],
    ),
  );
  ```

  ```go Go theme={"system"}
  response, err := client.SearchSingleIndex(client.NewApiSearchSingleIndexRequest(
    "INDEX_NAME").WithSearchParams(search.SearchParamsObjectAsSearchParams(
    search.NewEmptySearchParamsObject().SetAnalyticsTags(
      []string{"YOUR_ANALYTICS_TAG"}))))
  if err != nil {
    // handle the eventual error
    panic(err)
  }
  ```

  ```java Java theme={"system"}
  SearchResponse response = client.searchSingleIndex(
    "INDEX_NAME",
    new SearchParamsObject().setAnalyticsTags(Arrays.asList("YOUR_ANALYTICS_TAG")),
    Hit.class
  );
  ```

  ```js JavaScript theme={"system"}
  const response = await client.searchSingleIndex({
    indexName: 'indexName',
    searchParams: { analyticsTags: ['YOUR_ANALYTICS_TAG'] },
  });
  ```

  ```kotlin Kotlin theme={"system"}
  var response =
    client.searchSingleIndex(
      indexName = "INDEX_NAME",
      searchParams = SearchParamsObject(analyticsTags = listOf("YOUR_ANALYTICS_TAG")),
    )
  ```

  ```php PHP theme={"system"}
  $response = $client->searchSingleIndex(
      'INDEX_NAME',
      ['analyticsTags' => [
          'YOUR_ANALYTICS_TAG',
      ],
      ],
  );
  ```

  ```python Python theme={"system"}
  response = client.search_single_index(
      index_name="INDEX_NAME",
      search_params={
          "analyticsTags": [
              "YOUR_ANALYTICS_TAG",
          ],
      },
  )
  ```

  ```ruby Ruby theme={"system"}
  response = client.search_single_index(
    "INDEX_NAME",
    Algolia::Search::SearchParamsObject.new(analytics_tags: ["YOUR_ANALYTICS_TAG"])
  )
  ```

  ```scala Scala theme={"system"}
  val response = Await.result(
    client.searchSingleIndex(
      indexName = "INDEX_NAME",
      searchParams = Some(
        SearchParamsObject(
          analyticsTags = Some(Seq("YOUR_ANALYTICS_TAG"))
        )
      )
    ),
    Duration(100, "sec")
  )
  ```

  ```swift Swift theme={"system"}
  let response: SearchResponse<Hit> = try await client.searchSingleIndex(
      indexName: "INDEX_NAME",
      searchParams: SearchSearchParams
          .searchSearchParamsObject(SearchSearchParamsObject(analyticsTags: ["YOUR_ANALYTICS_TAG"]))
  )
  ```
</CodeGroup>

## View segmented analytics

To view segmented analytics,
see [Search analytics in the Algolia dashboard](/doc/guides/search-analytics/overview#search-analytics-in-the-algolia-dashboard).
