svrjs-nextjs-website/components/shared/providers/Analytics.tsx

59 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-09-08 14:34:54 +02:00
"use client";
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;
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 {
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]);
_paq.push(["setCustomUrl", document.location]);
_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);
})();
}, [pathname, searchParams, props.pagesRouter]);
2024-09-08 14:34:54 +02:00
return null;
}
export default function Analytics(props: { pagesRouter?: boolean }) {
return (
<Suspense>
<AnalyticsInternal {...props} />
</Suspense>
);
}