-
Notifications
You must be signed in to change notification settings - Fork 1
/
amplitude.ts
186 lines (166 loc) · 5.18 KB
/
amplitude.ts
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { Auth } from "decorator-shared/auth";
import { ClientParams, Context } from "decorator-shared/params";
import { param } from "../params";
// Dynamic import for lazy loading
const importAmplitude = () =>
import("amplitude-js").then((module) => module.default);
type EventData = Record<string, any>;
declare global {
interface Window {
dekoratorenAmplitude: typeof logEventFromApp;
}
}
const buildPlatformField = () => {
const { origin, pathname, hash } = window.location;
return `${origin}${pathname}${hash}`;
};
export const initAmplitude = async () => {
const amplitude = await importAmplitude();
const userProps = {
skjermbredde: window.screen.width,
skjermhoyde: window.screen.height,
vindusbredde: window.innerWidth,
vindushoyde: window.innerHeight,
};
amplitude.getInstance().init("default", "", {
apiEndpoint: "amplitude.nav.no/collect-auto",
saveEvents: false,
includeUtm: true,
includeReferrer: true,
platform: buildPlatformField(),
});
amplitude.getInstance().setUserProperties(userProps);
// This function is exposed for use from consuming applications
window.dekoratorenAmplitude = logEventFromApp;
};
type AnalyticsCategory =
| "dekorator-header"
| "dekorator-footer"
| "dekorator-meny"
| "varsler";
type AnalyticsActions =
| "søk-dynamisk"
| "navlogo"
| "lenke"
| "lenkegruppe"
| "hovedmeny/forsidelenke"
| "[redacted]"
| "nav.no"
| "arbeidsflate-valg"
| `${string}/${string}`
| string;
type AnalyticsEventArgs = {
eventName?: string;
category?: AnalyticsCategory;
action?: AnalyticsActions;
context?: Context;
destination?: string;
label?: string;
komponent?: string;
lenkegruppe?: "innlogget meny";
};
export const amplitudeEvent = (props: AnalyticsEventArgs) => {
const {
context,
eventName,
destination,
category,
action,
label,
komponent,
lenkegruppe,
} = props;
const actionFinal = `${context ? context + "/" : ""}${action}`;
logAmplitudeEvent(
eventName || "navigere",
{
destinasjon: destination || label,
søkeord: eventName === "søk" ? "[redacted]" : undefined,
lenketekst: actionFinal,
kategori: category,
komponent: komponent || action,
lenkegruppe,
},
"decorator_next",
);
};
const logEventFromApp = (params?: {
origin: unknown | string;
eventName: unknown | string;
eventData?: unknown | EventData;
}): Promise<any> => {
try {
if (!params || params.constructor !== Object) {
return Promise.reject(
"Argument must be an object of type {origin: string, eventName: string, eventData?: Record<string, any>}",
);
}
const { origin, eventName, eventData = {} } = params;
if (!eventName || typeof eventName !== "string") {
return Promise.reject('Parameter "eventName" must be a string');
}
if (!origin || typeof origin !== "string") {
return Promise.reject('Parameter "origin" must be a string');
}
if (!eventData || eventData.constructor !== Object) {
return Promise.reject(
'Parameter "eventData" must be a plain object',
);
}
return logAmplitudeEvent(eventName, eventData, origin);
} catch (e) {
return Promise.reject(`Unexpected Amplitude error: ${e}`);
}
};
export const logPageView = (params: ClientParams, authState: Auth) => {
return logAmplitudeEvent("besøk", {
sidetittel: document.title,
innlogging: authState.authenticated ? authState.securityLevel : false,
parametre: {
...params,
BREADCRUMBS: params.breadcrumbs && params.breadcrumbs.length > 0,
...(params.availableLanguages && {
availableLanguages: params.availableLanguages.map(
(lang) => lang.locale,
),
}),
},
});
};
export const logAmplitudeEvent = async (
eventName: string,
eventData: EventData = {},
origin = "decorator-next",
) => {
const amplitude = await importAmplitude();
return new Promise((resolve) => {
amplitude.getInstance().logEvent(
eventName,
{
...eventData,
platform: buildPlatformField(),
origin,
originVersion: eventData.originVersion || "unknown",
viaDekoratoren: true,
fromNext: true,
},
resolve,
);
});
};
export const amplitudeClickListener =
(fn: (el: HTMLAnchorElement) => AnalyticsEventArgs | null) =>
(e: MouseEvent) => {
const anchor =
e.target instanceof Element ? e.target.closest("a") : null;
if (anchor) {
const args = fn(anchor);
if (args) {
amplitudeEvent({
context: param("context"),
label: anchor.href,
...args,
});
}
}
};