Skip to content

Commit

Permalink
Forenkle ia metrikkene (#185)
Browse files Browse the repository at this point in the history
* Legger på kall til IA-metrikker etter fem sekunder på alle undersider
* Fjerner alle andre kall til Ia-metrikkene
*  Skriver tester for metrikkutsendelser
  • Loading branch information
runska authored Jan 25, 2024
1 parent 489def2 commit f874edd
Show file tree
Hide file tree
Showing 20 changed files with 135 additions and 475 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches:
- main
- amplitude-browser
- forenkle-ia-metrikkene
paths-ignore:
- "**.md"
- "**/**.md"
Expand Down
10 changes: 0 additions & 10 deletions src/Aktiviteter/context/aktivitetStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import React from "react";
import { StatusType } from "../AktivitetData";
import { AktivitetBrukerStatus } from "../../hooks/useHentAktiviteter";
import { oppdaterStatus } from "../status-klient";
import { sendIaMetrikkInteraksjonstjeneste } from "../../integrasjoner/ia-tjenestemetrikker-api";
import {
AggregertStatistikkDto,
tomtDataobjekt,
} from "../../integrasjoner/aggregert-statistikk-api";
import {MetrikkKilde} from "@navikt/ia-metrikker-client";

const AktivitetContext = React.createContext<{
aktivitetStatuser: AktivitetBrukerStatus[];
Expand Down Expand Up @@ -73,12 +71,6 @@ export const AktivitetProvider = ({
);
};

export const useAktivitetStatuser = () => {
const { aktivitetStatuser } = React.useContext(AktivitetContext);

return { aktivitetStatuser };
};

export const useStatusForAktivitet = (id: string) => {
const { aktivitetStatuser } = React.useContext(AktivitetContext);

Expand Down Expand Up @@ -110,8 +102,6 @@ export const useOppdaterStatus = (
(status: StatusType) => {
if (orgnr) {
oppdaterStatus(aktivitetId, orgnr, status);
sendIaMetrikkInteraksjonstjeneste(MetrikkKilde.FOREBYGGINGSPLAN, orgnr);

setLokaleEndringer((tidligereEndringer) => {
const aktivitetIndex = tidligereEndringer.findIndex(
(endring) => endring.aktivitetId === aktivitetId
Expand Down
14 changes: 14 additions & 0 deletions src/hooks/useSendIaTjenesteMetrikkEtterFemSekunder.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useEffect } from "react";
import { sendDigitalIaTjenesteMetrikk } from "../integrasjoner/ia-tjenestemetrikker-api";
import { useOrgnr } from "./useOrgnr";

export function useSendIaMetrikkEtterFemSekunder() {
const orgnr = useOrgnr();

useEffect(() => {
const timer = setTimeout(() => {
sendDigitalIaTjenesteMetrikk(orgnr);
}, 5000);
return () => clearTimeout(timer);
}, [orgnr]);
}
55 changes: 0 additions & 55 deletions src/hooks/useSendIaTjenesteMetrikkOnEvent.test.tsx

This file was deleted.

29 changes: 0 additions & 29 deletions src/hooks/useSendIaTjenesteMetrikkOnEvent.ts

This file was deleted.

52 changes: 25 additions & 27 deletions src/integrasjoner/ia-tjenestemetrikker-api.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
import { METRIKKER_URL } from "../utils/konstanter";
import {
MetrikkKilde,
MetrikkType,
sendIaMetrikk,
} from "@navikt/ia-metrikker-client";
import { AUTHENTICATED_BASE_PATH } from "../utils/konstanter";

export const sendDigitalIaTjenesteMetrikk = async (
tjeneste: MetrikkKilde,
orgnr?: string
) => {
if (!orgnr) {
return Promise.reject("orgnr er udefinert");
}
return sendIaMetrikk(
orgnr,
MetrikkType.DIGITAL_IA_TJENESTE,
tjeneste,
METRIKKER_URL
);
};
interface Metrikk {
orgnr: string;
type: string;
kilde: string;
}

export const sendIaMetrikkInteraksjonstjeneste = async (tjeneste: MetrikkKilde, orgnr?: string) => {
const METRIKKER_URL = AUTHENTICATED_BASE_PATH + "/metrikker";

export const sendDigitalIaTjenesteMetrikk = async (orgnr?: string) => {
if (!orgnr) {
return Promise.reject("orgnr er udefinert");
}
return sendIaMetrikk(
orgnr,
MetrikkType.INTERAKSJONSTJENESTE,
tjeneste,
METRIKKER_URL
);

const metrikk: Metrikk = {
orgnr: orgnr,
type: "DIGITAL_IA_TJENESTE",
kilde: "FOREBYGGE_FRAVÆR",
};

return await fetch(METRIKKER_URL, {
method: "POST",
credentials: "include",
body: JSON.stringify(metrikk),
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
});
};
47 changes: 1 addition & 46 deletions src/komponenter/Kalkulator/Kalkulator.test.tsx
Original file line number Diff line number Diff line change
@@ -1,51 +1,6 @@
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { render } from "@testing-library/react";
import { Fraværskalulator } from "./Kalkulator";
import { axe } from "jest-axe";
import { sendDigitalIaTjenesteMetrikk } from "../../integrasjoner/ia-tjenestemetrikker-api";
jest.mock("next/router", () => ({
useRouter() {
return {
route: "/",
pathname: "",
query: "",
asPath: "",
push: jest.fn(),
events: {
on: jest.fn(),
off: jest.fn(),
},
beforePopState: jest.fn(() => null),
prefetch: jest.fn(() => null),
};
},
}));
jest.mock("../../integrasjoner/ia-tjenestemetrikker-api", () => ({
__esModule: true,
...jest.requireActual("../../integrasjoner/ia-tjenestemetrikker-api"),
sendDigitalIaTjenesteMetrikk: jest.fn(),
}));
jest.mock("../../hooks/useOrgnr", () => ({
useOrgnr: () => "999999999",
}));

afterEach(() => {
jest.resetAllMocks();
jest.fn().mockClear();
});

it("Kaller sendIaTjenesteMetrikk ved endring av modus", async () => {
render(<Fraværskalulator {...dummyData} nedlastingPågår={false} />);
const user = userEvent.setup();

const dagsverkLenke = screen.getByText("Dagsverk");

expect(sendDigitalIaTjenesteMetrikk).toHaveBeenCalledTimes(0);

await user.click(dagsverkLenke);

expect(sendDigitalIaTjenesteMetrikk).toHaveBeenCalledTimes(1);
});

it("Ingen uu-feil fra axe", async () => {
const { container: myContainer } = render(
Expand Down
12 changes: 1 addition & 11 deletions src/komponenter/Kalkulator/Kalkulator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import styles from "./Kalkulator.module.scss";
import { KalkulatorMedDagsverk } from "./KalkulatorMedDagsverk";
import { KalkulatorMedProsent } from "./KalkulatorMedProsent";
import { Heading, Ingress, Link, ToggleGroup } from "@navikt/ds-react";
import { useSendIaTjenesteMetrikkOnEvent } from "../../hooks/useSendIaTjenesteMetrikkOnEvent";
import TestVersjonBanner from "../Banner/TestVersjonBanner";
import { MetrikkKilde } from "@navikt/ia-metrikker-client";
import {sendToggleEvent} from "../../amplitude/amplitude";
import { sendToggleEvent } from "../../amplitude/amplitude";

export interface KalkulatorData {
tapteDagsverk?: string;
Expand All @@ -21,11 +19,6 @@ export const Fraværskalulator: FunctionComponent<
prodUrl?: string;
}
> = (props) => {
useSendIaTjenesteMetrikkOnEvent(
MetrikkKilde.KALKULATOR,
"inputfeltEndretAvBruker"
);

const [kalkulatorvariant, setKalkulatorvariant] = useState("prosent");

return (
Expand Down Expand Up @@ -53,9 +46,6 @@ export const Fraværskalulator: FunctionComponent<
onChange={(valgtVariant) => {
setKalkulatorvariant(valgtVariant);
sendToggleEvent("kalkulatorvariant", valgtVariant);
document.dispatchEvent(
new CustomEvent("inputfeltEndretAvBruker")
);
}}
value={kalkulatorvariant}
size="medium"
Expand Down
1 change: 0 additions & 1 deletion src/komponenter/Kalkulator/Kalkulatorrad/Kalkulatorrad.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export const Kalkulatorrad: FunctionComponent<Props> = (props) => {
onChange={(event) => {
props.onChange(event);
sendInputfeltUtfyltEvent(props.label, props.name);
document.dispatchEvent(new CustomEvent("inputfeltEndretAvBruker"));
}}
type={"text"}
inputMode={"numeric"}
Expand Down
Loading

0 comments on commit f874edd

Please sign in to comment.