-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathanalytics-client.ts
More file actions
56 lines (49 loc) · 1.26 KB
/
Copy pathanalytics-client.ts
File metadata and controls
56 lines (49 loc) · 1.26 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
48
49
50
51
52
53
54
55
56
"use client";
const DISTINCT_ID_STORAGE_KEY = "create_db:distinct_id";
function getDistinctId(): string {
if (typeof window === "undefined") {
return "claim-db-worker";
}
try {
const existing = window.localStorage.getItem(DISTINCT_ID_STORAGE_KEY);
if (existing) {
return existing;
}
const nextId = crypto.randomUUID();
window.localStorage.setItem(DISTINCT_ID_STORAGE_KEY, nextId);
return nextId;
} catch {
return "claim-db-worker";
}
}
export async function sendAnalyticsEvent(
event: string,
properties: Record<string, unknown> = {}
): Promise<void> {
if (!event.startsWith("create_db:")) {
return;
}
try {
const response = await fetch("/api/analytics", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
keepalive: true,
body: JSON.stringify({
event,
properties: {
...properties,
distinct_id: getDistinctId(),
},
}),
});
if (!response.ok && process.env.NODE_ENV === "development") {
console.error("Failed to send analytics event:", response.status);
}
} catch (error) {
if (process.env.NODE_ENV === "development") {
console.error("Failed to send analytics event:", error);
}
}
}