-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.ts
More file actions
43 lines (35 loc) · 1.1 KB
/
analytics.ts
File metadata and controls
43 lines (35 loc) · 1.1 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
type AnalyticsEventName = 'cta_click' | 'page_view' | 'custom_event';
type CtaName = 'github' | 'npm' | 'docs';
type TrackEventParams = Record<string, string | number | boolean | undefined>;
export class Analytics {
private static isEnabled(): boolean {
return typeof window !== 'undefined' && typeof window.gtag === 'function';
}
static track(eventName: AnalyticsEventName | string, params?: TrackEventParams): void {
if (!this.isEnabled()) return;
window.gtag!('event', eventName, params ?? {});
}
static trackCta(
ctaName: CtaName | string,
params?: {
location?: string;
url?: string;
label?: string;
},
): void {
this.track('cta_click', {
cta_name: ctaName,
location: params?.location,
link_url: params?.url,
label: params?.label,
});
}
static trackPageView(path: string, title?: string): void {
if (!this.isEnabled()) return;
window.gtag!('event', 'page_view', {
page_path: path,
page_title: title,
page_location: typeof window !== 'undefined' ? window.location.href : undefined,
});
}
}