Analytics tags let you view subsets of your analytics data. This lets you better understand your user’s behavior. Analytics tags are strings that you conditionally assign to specific searches. These strings identify an aspect of the searches that clarify the behavior of a specific group of users.
For instance, suppose you run a social media website where users can authenticate and create a profile. In their profile, they must provide their date of birth. This piece of information can help you determine their age. When they make a search, you can send that information to Algolia.
Later on, when you consult your Algolia analytics data, you can better understand the behavior and needs of your users based on their age group, instead of looking at all your users collectively.
This guide outlines the process step by step. It illustrates two ways of implementing analyticsTags
: with InstantSearch and with the API clients.
Identifying your user’s age group
Sending the specific birth year or age of each of your users wouldn’t be optimal, because you would end up with a lot of unhelpful tags. For example, you likely don’t want to see analytics for all users who are exactly 21, or those who were born in 1975. Instead, you probably want a general overview of how teens, young adults, and seniors behave.
Therefore, you need to fetch your user’s information and transform it, before you can send it to Algolia.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| function getAgeGroup($birthYear)
{
if ($birthYear >= 1996) {
return "Generation Z";
} else if ($birthYear <= 1995 && $birthYear >= 1977) {
return "Generation Y";
} else {
return "Generation X or older";
}
}
$birthYear = 1975; // Fetch your user's birth year from your back end
$ageGroup = getAgeGroup($birthYear);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
| def get_age_group(birth_year)
if birth_year >= 1996
return 'Generation Z'
elsif birth_year <= 1995 && birth_year >= 1977
return 'Generation Y'
else
return 'Generation X or older'
end
end
birth_year = 1975 # Fetch your user's birth year from your back end
age_group = get_age_group(birth_year)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
| const getAgeGroup = birthYear => {
if (birthYear >= 1996) {
return 'Generation Z';
} else if (birthYear <= 1995 && birthYear >= 1977) {
return 'Generation Y';
} else {
return 'Generation X or older';
}
};
const birthYear = 1975; // Fetch your user's birth year from your back end
const ageGroup = getAgeGroup(birthYear);
|
1
2
3
4
5
6
7
8
9
10
11
| def get_age_group(birth_year):
if birth_year >= 1996:
return 'Generation Z'
elif birth_year <= 1995 and birth_year >= 1977:
return 'Generation Y'
else:
return 'Generation X or older'
birth_year = 1975 # Fetch your user's birth year from your back end
age_group = get_age_group(birth_year)
|
1
2
3
4
5
6
7
8
9
10
11
12
| func getAgeGroup(birthyear: Int) -> String {
if 1996 <= birthyear {
return "Generation Z"
} else if 1977 <= birthyear && birthyear <= 1995 {
return "Generation Y"
} else {
return "Generation X or older"
}
}
let birthyear = 1975 // Fetch your user's birth year from your back end
let ageGroup = getAgeGroup(birthyear: birthyear);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
| public String getAgeGroup(int birthyear) {
if (1996 <= birthyear) {
return "Generation Z";
} else if (1977 <= birthyear && birthyear <= 1995) {
return "Generation Y";
} else {
return "Generation X or older";
}
}
int birthyear = 1975; // Fetch your user's birth year from your back end
String ageGroup = getAgeGroup(birthyear);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| Func<int, string> getAgeGroup = (int birthyear) =>
{
if (1996 <= birthyear)
{
return "Generation Z";
}
else if (1977 <= birthyear && birthyear <= 1995)
{
return "Generation Y";
}
else
{
return "Generation X or older";
}
};
const int birthyear = 1975; // Fetch your user's birth year from your back end
string ageGroup = getAgeGroup(birthyear);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
| public String getAgeGroup(int birthyear) {
if (1996 <= birthyear) {
return "Generation Z";
} else if (1977 <= birthyear && birthyear <= 1995) {
return "Generation Y";
} else {
return "Generation X or older";
}
}
int birthyear = 1975; // Fetch your user's birth year from your back end
String ageGroup = getAgeGroup(birthyear);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
| getAgeGroup := func(birthyear int) string {
if 1996 <= birthyear {
return "Generation Z"
} else if 1977 <= birthyear && birthyear <= 1995 {
return "Generation Y"
} else {
return "Generation X or older"
}
}
const birthYear = 1975 // Fetch your user's birth year from your back end
ageGroup := getAgeGroup(birthYear)
|
1
2
3
4
5
6
7
8
9
| def getAgeGroup(birthyear: Int): String = birthyear match {
case x if 1996 <= x => "Generation Z"
case x if 1977 <= x && x <= 1995 => "Generation Y"
case _ => "Generation X or older"
}
val birthyear = 1975 // Fetch your user's birth year from your back end
val ageGroup = getAgeGroup(birthyear)
|
1
2
3
4
5
6
7
8
9
10
| fun getAgeGroup(birthYear: Int): String {
return when {
1996 <= birthYear -> "Generation Z"
birthYear in 1977..1995 -> "Generation Y"
else -> "Generation X or older"
}
}
val birthYear = 1975 // Fetch your user's birth year from your back end
val ageGroup = getAgeGroup(birthYear)
|
Assigning an analytics tag to a search
Whenever a user makes a search, you want to send their age group along with the query. You can do this whether you’re using InstantSearch, or an API client directly.
Using InstantSearch
If you’re using InstantSearch to build your front end, you can pass the analyticsTags
parameter when making the query.
1
2
3
| let query = Query()
query.analyticsTags = [ageGroup]
let searcher = SingleIndexSearcher(index: index, query: query)
|
1
2
3
4
5
6
7
| val query = query {
analyticsTags {
+ageGroup
}
}
val searcher = SearcherSingleIndex(index, query)
|
1
2
3
| instantsearch.widgets.configure({
analyticsTags: [ageGroup]
});
|
1
2
3
| <Configure
analyticsTags={[ageGroup]}
/>
|
1
2
3
| <ais-configure
:analytics-tags.camel="[ageGroup]"
/>
|
1
2
3
| <ais-configure
[searchParameters]="{ analyticsTags: [ageGroup] }"
></ais-configure>
|
Using an API client
You can also pass the analyticsTags
parameter if you’re using one of the API clients.
1
2
3
4
5
| $results = $index->search('query', [
'analyticsTags' => [
$ageGroup
]
]);
|
1
2
3
4
5
| results = index.search('query', {
analyticsTags: [
age_group
]
})
|
1
2
3
4
5
6
7
8
9
10
11
| index.search(
{
query: 'query',
analyticsTags: [ageGroup],
},
(err, { hits } = {}) => {
if (err) throw err;
console.log(hits);
},
);
|
1
2
3
4
5
| results = index.search('query', {
'analyticsTags': [
age_group
]
})
|
1
2
3
4
5
6
7
8
| let query = Query(query: "query")
query.analyticsTags = [
ageGroup
]
index.search(query, completionHandler: { (res, error) in
print(res)
})
|
1
2
3
4
5
6
| index.search(
new Query("query")
.setAnalyticsTags(
ageGroup
)
);
|
1
2
3
4
5
6
7
| index.Search<T>(new Query("")
{
AnalyticsTags = new List<string>
{
ageGroup
},
});
|
1
2
3
4
| index.search(
new Query("query")
.setAnalyticsTags(Arrays.asList(ageGroup))
);
|
1
2
3
4
| res, err := index.Search(
"query",
opt.AnalyticsTags(ageGroup),
)
|
1
2
3
4
5
6
| client.execute {
search into "indexName" query Query(
query = Some("query"),
analyticsTags = Some(Seq(ageGroup)),
)
}
|
1
2
3
4
5
6
7
| val query = query("query") {
analyticsTags {
+ageGroup
}
}
index.search(query)
|
Viewing segmented analytics data
After you’ve implemented, tested, and released your app with age group analyticsTags
, you can start segmenting your new analytics data based on your user’s platform.
You can do this from the Analytics section of your Algolia dashboard. In the header of the Analytics tab you can find the Tags input field.
Once your users start making searches in your new search implementation, the age group analyticsTags
appear in the Tags field. If you select a certain age group, you only see analytics for searches tagged with that age group. This is true for all Analytics subsections.
Depending on your traffic, you may have to wait to get enough tagged data to start drawing meaningful conclusions.