-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathanalytics-utils.ts
More file actions
47 lines (46 loc) · 1.17 KB
/
analytics-utils.ts
File metadata and controls
47 lines (46 loc) · 1.17 KB
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
type TrackingCustomEvents = {
resource_clicked: {
resource_name: string;
};
resource_compare_add: {
resource_name: string;
};
resource_compare_open: void;
resource_compare_select_all: void;
resource_compare_reset: void;
resource_compare_remove: {
resource_name: string;
};
resource_compare_sort: {
order: 'asc' | 'desc';
};
// Recommended event: https://developers.google.com/analytics/devguides/collection/ga4/reference/events?sjid=9801509268566726236-NA&client_type=gtag#search
search: {
search_term: string;
};
advanced_search_open: void;
advanced_search_run: {
search_term: string;
params: Record<string, unknown>;
};
};
export const track = <T extends keyof TrackingCustomEvents>(
event: T,
...data: TrackingCustomEvents[T] extends void ? [] : [TrackingCustomEvents[T]]
): void => {
if (import.meta.env.MODE === 'production') {
if (typeof window !== 'undefined' && typeof window.gtag === 'function') {
if (data[0] != null) {
window.gtag('event', event, data[0]);
} else {
window.gtag('event', event);
}
}
} else {
if (data[0] != null) {
console.info('track', event, data[0]);
} else {
console.info('track', event);
}
}
};