Skip to content

Commit

Permalink
feat(dsfr): ajout de matomo pour tracker les events (#6157)
Browse files Browse the repository at this point in the history
* fix: matomo

* fix: matomo

---------

Co-authored-by: Martial Maillot <[email protected]>
  • Loading branch information
maxgfr and m-maillot authored Sep 26, 2024
1 parent b250d45 commit 3d0967c
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 13 deletions.
2 changes: 2 additions & 0 deletions packages/code-du-travail-frontend/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { StartDsfr } from "../src/modules/config/StartDsfr";
import { Metadata } from "next/types";
import { SITE_URL } from "../src/config";
import { headers } from "next/headers";
import { MatomoAnalytics } from "../src/modules/config/MatomoAnalytics";

export const metadata: Metadata = {
title: {
Expand Down Expand Up @@ -53,6 +54,7 @@ export default function RootLayout({
</head>
<body>
<DsfrProvider lang={lang}>{children}</DsfrProvider>
<MatomoAnalytics />
</body>
</html>
);
Expand Down
9 changes: 6 additions & 3 deletions packages/code-du-travail-frontend/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import React, { useEffect } from "react";
import { A11y } from "../src/a11y";
import { getSourceUrlFromPath } from "../src/lib";
import { useRouter } from "next/router";
import { PIWIK_SITE_ID, PIWIK_URL, SITE_URL } from "../src/config";
import {
PIWIK_SITE_ID,
PIWIK_URL,
SITE_URL,
WIDGETS_PATH,
} from "../src/config";

if (typeof window !== "undefined") {
import("../src/web-components/tooltip")
Expand All @@ -36,8 +41,6 @@ if (typeof window !== "undefined") {
});
}

const WIDGETS_PATH = /\/widgets\/.*/;

function MyApp({ Component, pageProps }: AppProps) {
const router = useRouter();
useEffect(() => {
Expand Down
1 change: 1 addition & 0 deletions packages/code-du-travail-frontend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ export const DEBOUNCE_TIME_MS = 300;
export const API_GEO_MAX_SEARCH_RESULTS = 10;
export const SUGGEST_DEBOUNCE_DELAY = 200;
export const SUGGEST_MAX_RESULTS = 5;
export const WIDGETS_PATH = /\/widgets\/.*/;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use client";

import { init, push } from "@socialgouv/matomo-next";
import { usePathname } from "next/navigation";
import { useEffect } from "react";
import { PIWIK_SITE_ID, PIWIK_URL, SITE_URL, WIDGETS_PATH } from "../../config";
import { getSourceUrlFromPath } from "../../lib";

export function MatomoAnalytics() {
const pathname = usePathname();

useEffect(() => {
init({
siteId: PIWIK_SITE_ID,
url: PIWIK_URL,
onInitialization: () => {
const referrerUrl =
document?.referrer || getSourceUrlFromPath(SITE_URL + pathname);
if (referrerUrl) {
push(["setReferrerUrl", referrerUrl]);
}
if (pathname && pathname.match(WIDGETS_PATH)) {
push(["setCookieSameSite", "None"]);
}
},
excludeUrlsPatterns: [WIDGETS_PATH],
});
}, []);

return null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import { Input } from "@codegouvfr/react-dsfr/Input";
import Image from "next/image";
import { useEffect, useState } from "react";
import { useState } from "react";
import { useNeedMoreInfoEvents } from "./tracking";
import servicesDeRenseignement from "../../../data/services-de-renseignement.json";
import { Button } from "@codegouvfr/react-dsfr/Button";
import { usePathname } from "next/navigation";
import { fr } from "@codegouvfr/react-dsfr";
import { css } from "../../../../styled-system/css";

Expand All @@ -16,13 +15,12 @@ type ServiceRenseignement = {
};

export function PopupContent() {
const currentPathName = usePathname();
const [department, setDepartment] = useState<string>("");
const [hasSearched, setHasSearched] = useState<boolean>(false);
const [result, setResult] = useState<undefined | ServiceRenseignement>(
undefined
);
const { emitModalIsOpened, emitTrackNumber } = useNeedMoreInfoEvents();
const { emitTrackNumber } = useNeedMoreInfoEvents();

const onSearchInput = () => {
const departmentNum = (department.replace(/^0+/, "") || "").toLowerCase();
Expand All @@ -35,10 +33,6 @@ export function PopupContent() {
emitTrackNumber();
};

useEffect(() => {
if (currentPathName) emitModalIsOpened(currentPathName);
}, [currentPathName, emitModalIsOpened]);

return (
<>
<h2 className={fr.cx("fr-h5")}>Contact téléphonique</h2>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
"use client";

import { fr } from "@codegouvfr/react-dsfr";
import { Button } from "@codegouvfr/react-dsfr/Button";
import { css } from "../../../../styled-system/css";
import { createModal } from "@codegouvfr/react-dsfr/Modal";
import { PopupContent } from "./PopupContent";
import { useIsModalOpen } from "@codegouvfr/react-dsfr/Modal/useIsModalOpen";
import { useNeedMoreInfoEvents } from "./tracking";
import { useEffect } from "react";

export const needMoreInfoModal = createModal({
id: "more-info-modal",
isOpenedByDefault: false,
});

export const NeedMoreInfo = () => {
const isOpen = useIsModalOpen(needMoreInfoModal);
const { emitModalIsOpened } = useNeedMoreInfoEvents();

useEffect(() => {
if (isOpen) emitModalIsOpened();
}, [isOpen, emitModalIsOpened]);

return (
<div className={mainContainer} id="more-info">
<div className={fr.cx("fr-container")}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { push as matopush } from "@socialgouv/matomo-next";
import { MatomoBaseEvent } from "../../../lib";
import { usePathname } from "next/navigation";

export enum MatomoNeedMoreInfoEventSecondary {
CONTACT = "contact",
Expand All @@ -11,6 +12,8 @@ export enum MatomoNeedMoreInfoEventTertiary {
}

export const useNeedMoreInfoEvents = () => {
const currentPathName = usePathname();

const emitTrackNumber = () => {
matopush([
MatomoBaseEvent.TRACK_EVENT,
Expand All @@ -19,12 +22,12 @@ export const useNeedMoreInfoEvents = () => {
]);
};

const emitModalIsOpened = (baseUrl: string) => {
const emitModalIsOpened = () => {
matopush([
MatomoBaseEvent.TRACK_EVENT,
MatomoNeedMoreInfoEventSecondary.CONTACT,
MatomoNeedMoreInfoEventTertiary.CLICK_CONTACT_MODAL,
baseUrl,
currentPathName,
]);
};

Expand Down

0 comments on commit 3d0967c

Please sign in to comment.