forked from offlegacy/event-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateTracker.ts
More file actions
51 lines (45 loc) · 1.14 KB
/
Copy pathcreateTracker.ts
File metadata and controls
51 lines (45 loc) · 1.14 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
import { createTracker } from "@offlegacy/event-tracker";
import * as amplitude from "@amplitude/analytics-browser";
const API_KEY = process.env.NEXT_PUBLIC_AMPLITUDE_API_KEY;
const isProduction = process.env.NODE_ENV === "production";
interface Context {
referrer?: string;
}
interface EventParams {
title?: string;
target?: string;
}
const track = isProduction ? amplitude.track : console.log;
export const [Track] = createTracker({
init: () => {
if (isProduction) {
if (API_KEY === undefined) {
throw new Error("NO API KEY");
}
amplitude.init(API_KEY, {
autocapture: false,
});
}
},
pageView: {
onPageView: (params: EventParams, context: Context) => {
track("pageView", { ...params, referrer: context.referrer });
},
},
DOMEvents: {
onClick: (params: EventParams, context: Context) => {
track("click", {
...params,
referrer: context.referrer,
});
},
},
impression: {
onImpression: (params: EventParams, context: Context) => {
track("impression", {
...params,
referrer: context.referrer,
});
},
},
});