Skip to content

Commit 7c2bd04

Browse files
committed
IS-1940: Add amplitude
1 parent 15288b2 commit 7c2bd04

File tree

4 files changed

+246
-0
lines changed

4 files changed

+246
-0
lines changed

package-lock.json

Lines changed: 189 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"author": "DigiSyfo <[email protected]>",
2323
"license": "ISC",
2424
"dependencies": {
25+
"@amplitude/analytics-browser": "^2.3.8",
2526
"@navikt/aksel-icons": "5.11.2",
2627
"@navikt/ds-css": "5.11.1",
2728
"@navikt/ds-react": "5.11.1",

src/utils/amplitude.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import * as amplitude from '@amplitude/analytics-browser';
2+
import { isProd } from '@/utils/miljoUtil';
3+
4+
/**
5+
See documentation for naming guidelines: https://github.com/navikt/analytics-taxonomy
6+
Other documentation on Aksel: https://aksel.nav.no/god-praksis/artikler/mal-brukeratferd-med-amplitude
7+
*/
8+
export enum EventType {
9+
PageView = 'besøk',
10+
Navigation = 'navigere',
11+
OptionSelected = 'alternativ valgt',
12+
}
13+
14+
type EventPageView = {
15+
type: EventType.PageView;
16+
data: {
17+
url: string;
18+
sidetittel: string;
19+
};
20+
};
21+
22+
type Navigation = {
23+
type: EventType.Navigation;
24+
data: {
25+
lenketekst: string;
26+
destinasjon: string;
27+
};
28+
};
29+
30+
type OptionSelected = {
31+
type: EventType.OptionSelected;
32+
data: {
33+
url: string;
34+
tekst: string;
35+
option: string;
36+
};
37+
};
38+
39+
type Event = EventPageView | Navigation | OptionSelected;
40+
41+
export const logEvent = (event: Event) =>
42+
amplitude.track(event.type, { ...event.data });
43+
44+
const getApiKey = () => {
45+
return isProd()
46+
? 'e4b68538f8d185f0ee2d913d8e51bd39'
47+
: 'c7bcaaf5d0fddda592412234dd3da1ba';
48+
};
49+
50+
amplitude.init(getApiKey(), undefined, {
51+
serverUrl: 'https://amplitude.nav.no/collect',
52+
});

src/utils/miljoUtil.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export const erLokal = (): boolean => {
99
return window.location.host.indexOf('localhost') > -1;
1010
};
1111

12+
export function isProd() {
13+
return window.location.href.indexOf('syfomodiaperson.intern.nav.no') > -1;
14+
}
15+
1216
export const finnNaisUrlDefault = (): string => {
1317
return erPreProd() ? '.intern.dev.nav.no' : '.intern.nav.no';
1418
};

0 commit comments

Comments
 (0)