Skip to content

Commit

Permalink
Legge til enhet i amplitude & tracke navigering
Browse files Browse the repository at this point in the history
  • Loading branch information
LudvigHz committed Apr 10, 2024
1 parent 60cf11c commit f3e052c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
24 changes: 18 additions & 6 deletions src/app/internarbeidsflatedecorator/Decorator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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 (
Expand Down
5 changes: 4 additions & 1 deletion src/app/internarbeidsflatedecorator/decoratorprops.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Enhet } from '../../rest/resources/saksbehandlersEnheterResource';

export const RESET_VALUE = '\u0000';

interface TogglesConfig {
Expand Down Expand Up @@ -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;
}
17 changes: 17 additions & 0 deletions src/utils/amplitude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export const initAmplitude = () => {
}
});

window.addEventListener('pagehide', () => {
amplitudeInstance?.setTransport('beacon');
amplitudeInstance?.flush();
});

amplitudeInstance.add(maskereFnrPlugin());
window.amplitude = amplitudeInstance;
return amplitudeInstance;
Expand Down Expand Up @@ -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;

0 comments on commit f3e052c

Please sign in to comment.