-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #314 from navikt/sette-opp-amplitude
Sette opp amplitude og tracke arbeidslista
- Loading branch information
Showing
11 changed files
with
453 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import * as amplitude from '@amplitude/analytics-browser'; | ||
import { track } from '@amplitude/analytics-browser'; | ||
import { AmplitudeEvent } from './taxonomy-events'; | ||
import { erProd } from '../util/utils'; | ||
|
||
export function initAmplitude() { | ||
const prosjektId = erProd() ? '691963e61d2b11465aa96acfcaa8959b' : 'faf28eb5445abfe75c7ac28ae7a8d050'; | ||
|
||
amplitude.init(prosjektId, undefined, { | ||
serverUrl: 'https://amplitude.nav.no/collect', | ||
ingestionMetadata: { | ||
sourceName: window.location.origin | ||
}, | ||
defaultTracking: false | ||
}); | ||
} | ||
|
||
const maskereFodselsnummer = (data?: Record<string, unknown>) => { | ||
const maskertObjekt = JSON.stringify(data).replace(/\d{11}/g, (_, indexOfMatch, fullString) => | ||
fullString.charAt(indexOfMatch - 1) === '"' ? '***********' : '"***********"' | ||
); | ||
|
||
try { | ||
return JSON.parse(maskertObjekt); | ||
} catch (e) { | ||
// eslint-disable-next-line no-console | ||
console.error('kunne ikke maskere data korrekt før sending til amplitude'); | ||
} | ||
return {}; | ||
}; | ||
|
||
async function logAmplitudeEvent( | ||
eventName: string, | ||
eventData?: Record<string, unknown>, | ||
origin = 'veilarbvisittkortfs' | ||
): Promise<void> { | ||
try { | ||
await track(eventName, { ...eventData, app: origin }); | ||
} catch (e) { | ||
return Promise.reject(`Unexpected Amplitude error: ${e}`); | ||
} | ||
} | ||
|
||
export const trackAmplitude = (event: AmplitudeEvent, ekstraData?: Record<string, unknown>): Promise<void> => { | ||
if (!event || event.constructor !== Object) { | ||
return Promise.reject( | ||
'Argument must be an object of type {eventName: string, eventData?: Record<string, unknown>}' | ||
); | ||
} | ||
|
||
const { name: eventName, data: eventData = {} } = event; | ||
if (!eventName || typeof eventName !== 'string') { | ||
return Promise.reject('Parameter "eventName" must be a string'); | ||
} | ||
if (!eventData || eventData.constructor !== Object) { | ||
return Promise.reject('Parameter "eventData" must be a plain object'); | ||
} | ||
if (!!ekstraData && ekstraData?.constructor !== Object) { | ||
return Promise.reject('Parameter "ekstraData" must be a plain object'); | ||
} | ||
|
||
return logAmplitudeEvent(eventName, maskereFodselsnummer({ ...eventData, ...ekstraData })); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
//Før du lager en ny eventType -> sjekk https://github.com/navikt/analytics-taxonomy/tree/main/events | ||
export type AmplitudeEvent = | ||
| { name: 'skjema fullført'; data: { skjemanavn: string; skjemaId: string } } | ||
| { name: 'knapp klikket'; data: { knapptekst: string; effekt: string } }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { Navspa } from '@navikt/navspa'; | ||
import { isDevelopment } from './util/utils'; | ||
import { isLocalDevelopment } from './util/utils'; | ||
import * as dayjs from 'dayjs'; | ||
import 'dayjs/locale/nb'; | ||
import App from './app'; | ||
import { initAmplitude } from './amplitude/amplitude'; | ||
|
||
dayjs.locale('nb'); | ||
|
||
Navspa.eksporter('veilarbvisittkortfs', App); | ||
|
||
if (isDevelopment()) { | ||
if (isLocalDevelopment()) { | ||
require('./mock'); | ||
|
||
ReactDOM.render( | ||
<App fnr={'10108000398'} enhet={'1234'} tilbakeTilFlate={''} visVeilederVerktoy={true} />, | ||
document.getElementById('veilarbvisittkortfs-root') | ||
); | ||
} else { | ||
initAmplitude(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters