diff --git a/src/app/internarbeidsflatedecorator/Decorator.tsx b/src/app/internarbeidsflatedecorator/Decorator.tsx index 6ce4a451b..7c21d642a 100644 --- a/src/app/internarbeidsflatedecorator/Decorator.tsx +++ b/src/app/internarbeidsflatedecorator/Decorator.tsx @@ -20,6 +20,8 @@ import OppdateringsloggContainer, { import './personsokKnapp.less'; import './decorator.less'; import { useValgtenhet } from '../../context/valgtenhet-state'; +import { trackNavigation, updateUserEnhet } from '../../utils/amplitude'; +import { Enhet } from '../../rest/resources/saksbehandlersEnheterResource'; const bjelleIkon = raw('../../svg/bjelle.svg'); @@ -97,7 +99,8 @@ function lagConfig( function lagConfigV3( enhet: string | undefined | null, history: History, - settEnhet: (enhet: string) => void + settEnhet: (enhet: string, enhetValue?: Enhet) => void, + onLinkClick?: (link: { text: string; url: string }) => void ): DecoratorPropsV3 { const { sokFnr, pathFnr, userKey } = getFnrFraUrl(); const onsketFnr = sokFnr || pathFnr; @@ -116,11 +119,12 @@ function lagConfigV3( } }, enhet: enhet ?? undefined, - onEnhetChanged: (enhet) => { + onEnhetChanged: (enhet, enhetValue) => { if (enhet) { - settEnhet(enhet); + settEnhet(enhet, enhetValue); } }, + onLinkClick: onLinkClick ?? undefined, showHotkeys: true, markup: { etterSokefelt: etterSokefelt @@ -256,20 +260,28 @@ function Decorator() { } }); - const handleSetEnhet = (enhet: string) => { + const handleSetEnhet = (enhet: string, enhetValue?: Enhet) => { + if (enhetValue) { + updateUserEnhet(enhetValue.navn); + } setEnhetId(enhet); }; + const handleLinkClick = (link: { text: string; url: string }) => { + trackNavigation(link.text, link.url); + }; + const configV2 = useCallback(lagConfig, [valgtEnhetId, history, handleSetEnhet])( valgtEnhetId, history, handleSetEnhet ); - const configV3 = useCallback(lagConfigV3, [valgtEnhetId, history, handleSetEnhet])( + const configV3 = useCallback(lagConfigV3, [valgtEnhetId, history, handleSetEnhet, handleLinkClick])( valgtEnhetId, history, - handleSetEnhet + handleSetEnhet, + handleLinkClick ); return ( diff --git a/src/app/internarbeidsflatedecorator/decoratorprops.ts b/src/app/internarbeidsflatedecorator/decoratorprops.ts index 806f0adf0..4f8134f4a 100644 --- a/src/app/internarbeidsflatedecorator/decoratorprops.ts +++ b/src/app/internarbeidsflatedecorator/decoratorprops.ts @@ -1,3 +1,5 @@ +import { Enhet } from '../../rest/resources/saksbehandlersEnheterResource'; + export const RESET_VALUE = '\u0000'; interface TogglesConfig { @@ -94,6 +96,7 @@ export interface DecoratorPropsV3 { fetchActiveEnhetOnMount?: boolean | undefined; fetchActiveUserOnMount?: boolean | undefined; onBeforeRequest?: (headers: HeadersInit) => HeadersInit | undefined; - onEnhetChanged: (enhet?: string | null) => void; + onEnhetChanged: (enhet?: string | null, enhetValue?: Enhet) => void; + onLinkClick?: (link: { text: string; url: string }) => void; onFnrChanged: (fnr?: string | null) => void; } diff --git a/src/utils/amplitude.ts b/src/utils/amplitude.ts index 7f71e1c05..aa7b195ab 100644 --- a/src/utils/amplitude.ts +++ b/src/utils/amplitude.ts @@ -50,6 +50,11 @@ export const initAmplitude = () => { } }); + window.addEventListener('pagehide', () => { + amplitudeInstance?.setTransport('beacon'); + amplitudeInstance?.flush(); + }); + amplitudeInstance.add(maskereFnrPlugin()); window.amplitude = amplitudeInstance; return amplitudeInstance; @@ -103,4 +108,16 @@ export const trackAccordionClosed = (name: string) => { }); }; +export const updateUserEnhet = (enhet: string) => { + if (!amplitudeInstance) { + console.warn('Amplitude is not initialized. Ignoring'); + return; + } + + const identifyEvent = new amplitude.Identify(); + identifyEvent.set('enhet', enhet); + + amplitudeInstance.identify(identifyEvent); +}; + export default initAmplitude;