2024-09-08 14:34:54 +02:00
|
|
|
"use client";
|
2024-09-08 15:17:26 +02:00
|
|
|
import { useEffect, Suspense } from "react";
|
2024-09-08 14:34:54 +02:00
|
|
|
import { usePathname, useSearchParams } from "next/navigation";
|
|
|
|
const MATOMO_URL = `${process.env.NEXT_PUBLIC_MATOMO_URL}`;
|
|
|
|
const MATOMO_SITE_ID = `${process.env.NEXT_PUBLIC_MATOMO_SITE_ID}`;
|
|
|
|
let firstUseEffect = true;
|
|
|
|
let firstUseEffect2 = true;
|
|
|
|
|
2024-09-08 15:17:26 +02:00
|
|
|
function AnalyticsInternal(props: { pagesRouter?: boolean }) {
|
2024-09-08 14:34:54 +02:00
|
|
|
const pathname = usePathname();
|
|
|
|
const searchParams = useSearchParams();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (firstUseEffect) {
|
|
|
|
firstUseEffect = false;
|
|
|
|
} else {
|
2024-09-08 15:17:26 +02:00
|
|
|
if (firstUseEffect2) {
|
|
|
|
firstUseEffect2 = false;
|
|
|
|
if (props.pagesRouter) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Track page view
|
|
|
|
const _paq = ((window as any)._paq = (window as any)._paq || []);
|
|
|
|
_paq.push(["setDocumentTitle", document.title]);
|
2024-09-08 18:13:38 +02:00
|
|
|
_paq.push(["setCustomUrl", document.location]);
|
2024-09-08 15:17:26 +02:00
|
|
|
_paq.push(["trackPageView"]);
|
2024-09-08 14:34:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matomo tracking code
|
|
|
|
const _paq = ((window as any)._paq = (window as any)._paq || []);
|
|
|
|
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
|
|
|
|
_paq.push(["disableCookies"]);
|
|
|
|
_paq.push(["trackPageView"]);
|
|
|
|
_paq.push(["enableLinkTracking"]);
|
|
|
|
(function () {
|
|
|
|
const u = MATOMO_URL + "/";
|
|
|
|
_paq.push(["setTrackerUrl", u + "matomo.php"]);
|
|
|
|
_paq.push(["setSiteId", MATOMO_SITE_ID]);
|
|
|
|
const d = document,
|
|
|
|
g = d.createElement("script"),
|
|
|
|
s = d.getElementsByTagName("script")[0];
|
|
|
|
g.async = true;
|
|
|
|
g.src = u + "matomo.js";
|
|
|
|
(s.parentNode as ParentNode).insertBefore(g, s);
|
|
|
|
})();
|
2024-09-08 15:17:26 +02:00
|
|
|
}, [pathname, searchParams]);
|
2024-09-08 14:34:54 +02:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2024-09-08 15:17:26 +02:00
|
|
|
|
|
|
|
export default function Analytics(props: { pagesRouter?: boolean }) {
|
|
|
|
return (
|
|
|
|
<Suspense>
|
|
|
|
<AnalyticsInternal {...props} />
|
|
|
|
</Suspense>
|
|
|
|
);
|
|
|
|
}
|