-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathanalytics.ts
More file actions
79 lines (72 loc) · 1.8 KB
/
analytics.ts
File metadata and controls
79 lines (72 loc) · 1.8 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { generateUUID } from '../ids';
import { retryFetch } from './retry';
const generateEventId = (now: Date) => {
const randomStr = (Math.random() + 1).toString(36).substring(8);
const timePart = (now.getTime() / 1000).toFixed(0);
return `${timePart}${randomStr}`;
};
export type AnalyticsEvent = {
event_name: string;
event_timestamp: Date;
user_id: string;
};
export enum AnalyticsEventName {
ConfirmAddingWorkspace = 'confirm adding workspace',
// Plus
ChangeBillingCycle = 'change billing cycle',
CancelSubscription = 'cancel subscription',
ReceivePayment = 'receive payment',
ClaimSubscription = 'claim subscription',
}
export async function sendAnalyticsEvent<T extends AnalyticsEvent>(
events: T[],
): Promise<void> {
const now = new Date();
const [visit_id, session_id] = await Promise.all([
generateUUID(),
generateUUID(),
]);
const transformed = events.map((event) => ({
event_id: generateEventId(now),
visit_id,
session_id,
...event,
}));
await retryFetch(
`${process.env.ANALYTICS_URL}/e`,
{
method: 'POST',
body: JSON.stringify({ events: transformed }),
headers: {
'content-type': 'application/json',
},
},
{ retries: 3 },
);
}
export type ExperimentAllocationEvent = {
event_timestamp: Date;
user_id: string;
experiment_id: string;
variation_id: string;
};
export async function sendExperimentAllocationEvent<
T extends ExperimentAllocationEvent,
>(event: T): Promise<void> {
await retryFetch(
`${process.env.ANALYTICS_URL}/e/x`,
{
method: 'POST',
body: JSON.stringify(event),
headers: {
'content-type': 'application/json',
},
},
{ retries: 3 },
);
}
export enum TargetType {
Plus = 'plus',
Credits = 'credits',
Recruiter = 'recruiter',
}