-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathSiteAnalytics.ts
126 lines (117 loc) · 3.33 KB
/
SiteAnalytics.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { GrapherAnalytics, EventCategory } from "@ourworldindata/grapher"
import { SearchCategoryFilter } from "./search/searchTypes.js"
import { DataCatalogState } from "./DataCatalog/DataCatalogState.js"
import { IDataCatalogHit } from "./DataCatalog/DataCatalogUtils.js"
import { set } from "lodash"
export class SiteAnalytics extends GrapherAnalytics {
logCountryProfileSearch(country: string) {
this.logToGA({
event: EventCategory.CountryProfileSearch,
eventContext: country,
})
}
logPageNotFoundError(url: string) {
this.logToGA({
event: EventCategory.SiteError,
eventAction: "not_found",
eventContext: url,
})
}
logCountryPageSearchQuery(query: string) {
this.logToGA({
event: EventCategory.Filter,
eventAction: "country_page_search",
eventContext: query,
})
}
logSearchClick({
query,
position,
url,
positionInSection,
cardPosition,
positionWithinCard,
filter,
}: {
query: string
position: string
positionInSection: string
cardPosition?: string
positionWithinCard?: string
url: string
filter: SearchCategoryFilter
}) {
this.logToGA({
event: EventCategory.SiteSearchClick,
eventAction: "click",
eventContext: JSON.stringify({
query,
position,
positionInSection,
cardPosition,
positionWithinCard,
filter,
}),
eventTarget: url,
})
}
logInstantSearchClick({
query,
url,
position,
}: {
query: string
url: string
position: string
}) {
this.logToGA({
event: EventCategory.SiteInstantSearchClick,
eventAction: "click",
eventContext: JSON.stringify({ query, position }),
eventTarget: url,
})
}
logSearchFilterClick({ key }: { key: string }) {
this.logToGA({
event: EventCategory.SiteSearchFilterClick,
eventAction: "click",
eventContext: key,
})
}
logDodShown(id: string) {
this.logToGA({
event: EventCategory.DetailOnDemand,
eventAction: "show",
eventTarget: id,
})
}
logDataCatalogSearch(state: DataCatalogState) {
this.logToGA({
event: EventCategory.DataCatalogSearch,
eventAction: "search",
eventContext: JSON.stringify({
...state,
topics: Array.from(state.topics),
selectedCountryNames: Array.from(state.selectedCountryNames),
}),
})
}
logDataCatalogResultClick(
hit: IDataCatalogHit,
position: number,
source: "ribbon" | "search",
ribbonTag?: string
) {
const eventContext = {
position,
source,
}
if (ribbonTag) set(eventContext, "ribbonTag", ribbonTag)
this.logToGA({
event: EventCategory.DataCatalogResultClick,
eventAction: "click",
eventContext: JSON.stringify(eventContext),
eventTarget: hit.slug,
})
}
}