Skip to content

Commit

Permalink
feat: matomo
Browse files Browse the repository at this point in the history
  • Loading branch information
K4ST0R committed Jan 2, 2025
1 parent 4682347 commit def3ae9
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions ui/app/(accompagnateur)/components/Matomo.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
"use client";

import { useConsent } from "#/app/components/ConsentManagement";
import { init, push } from "@socialgouv/matomo-next";
import { usePathname } from "next/navigation";
import { useEffect } from "react";
import { usePathname, useSearchParams } from "next/navigation";
import { useEffect, useRef } from "react";

function sanitizeUrl(str: string): string {
let strSanitized = str.replace(/address=([^&]*)/, "address=***");
strSanitized = strSanitized.replace(/latitude=([^&]*)/, "latitude=***");
strSanitized = strSanitized.replace(/longitude=([^&]*)/, "longitude=***");
strSanitized = strSanitized.replace(/latitudeA=([^&]*)/, "latitudeA=***");
strSanitized = strSanitized.replace(/longitudeA=([^&]*)/, "longitudeA=***");
return strSanitized;
}

export function MatomoAnalytics() {
const { finalityConsent } = useConsent();
const searchParams = useSearchParams();
const pathname = usePathname();
const isInitialLoad = useRef(true);

const searchParamsString = searchParams.toString();

useEffect(() => {
if (!finalityConsent?.analytics) {
return;
}

init({
siteId: process.env.NEXT_PUBLIC_MATOMO_SITE_ID || "",
url: process.env.NEXT_PUBLIC_MATOMO_URL || "",
disableCookies: true,
onInitialization: () => {
const url = pathname + (searchParamsString ? "?" + sanitizeUrl(searchParamsString) : "");
push(["setCustomUrl", url]);
push(["setReferrerUrl", sanitizeUrl(document?.referrer)]);
},
});
}, []);
}, [finalityConsent, pathname, searchParamsString]);

return null;
}

0 comments on commit def3ae9

Please sign in to comment.