forked from SmartDropLabs/smartdrop-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.ts
More file actions
43 lines (36 loc) · 1.29 KB
/
Copy pathanalytics.ts
File metadata and controls
43 lines (36 loc) · 1.29 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
export type AnalyticsProps = Record<string, unknown>;
type GtagFn = (command: "event", name: string, props?: AnalyticsProps) => void;
type AnalyticsWindow = Window & {
gtag?: GtagFn;
dataLayer?: AnalyticsProps[];
};
function hashPublicKey(publicKey: string): string {
if (typeof window === "undefined" || !publicKey) return "";
const encoder = new TextEncoder();
const data = encoder.encode(publicKey);
return Array.from(new Uint8Array(data.slice(0, 4)))
.map((b) => b.toString(16).padStart(2, "0"))
.join("")
.slice(0, 8);
}
export function trackEvent(name: string, props: AnalyticsProps = {}): void {
if (typeof window === "undefined") return;
const sanitized = { ...props };
if ("publicKey" in sanitized && typeof sanitized.publicKey === "string") {
sanitized.publicKey = hashPublicKey(sanitized.publicKey as string);
}
const payload = { ...sanitized, timestamp: Date.now() };
const w = window as AnalyticsWindow;
try {
if (typeof w.gtag === "function") {
w.gtag("event", name, payload);
} else if (Array.isArray(w.dataLayer)) {
w.dataLayer.push({ event: name, ...payload });
}
if (process.env.NODE_ENV !== "production") {
console.debug("[analytics] " + name, payload);
}
} catch {
// Analytics must never break the app.
}
}