feat: ajouter le composant MatomoScript pour l'analyse des données et mettre à jour les variables d'environnement dans .env.example

This commit is contained in:
Puechberty Arthur
2026-05-04 23:33:06 +02:00
parent ae60c50415
commit 19568822fe
3 changed files with 62 additions and 5 deletions
+2
View File
@@ -4,6 +4,7 @@ import { hasLocale, NextIntlClientProvider } from "next-intl";
import { getMessages, getTranslations } from "next-intl/server";
import { notFound } from "next/navigation";
import { MatomoScript } from "../../components/MatomoScript";
import { RTL_LOCALES, routing } from "../../i18n/routing";
import "../globals.css";
@@ -130,6 +131,7 @@ export default async function LocaleLayout({
<body
className={`${headingFont.variable} ${monoFont.variable} antialiased`}
>
<MatomoScript />
<NextIntlClientProvider locale={locale} messages={messages}>
{children}
</NextIntlClientProvider>
+50
View File
@@ -0,0 +1,50 @@
'use client';
import { useEffect } from 'react';
/**
* Matomo Analytics Script Component
* Injects the Matomo tracking script only if MATOMO_URL and MATOMO_SITE_ID are configured
*
* Environment variables:
* - NEXT_PUBLIC_MATOMO_URL: Base URL of Matomo instance (e.g., https://analytics.arthurp.fr/)
* - NEXT_PUBLIC_MATOMO_SITE_ID: Site ID in Matomo (e.g., 9)
*/
export function MatomoScript() {
useEffect(() => {
// Check if Matomo is properly configured
const matomoUrl = process.env.NEXT_PUBLIC_MATOMO_URL?.trim();
const matomoSiteId = process.env.NEXT_PUBLIC_MATOMO_SITE_ID?.trim();
// Skip if not configured
if (!matomoUrl || !matomoSiteId) {
if (process.env.NODE_ENV === 'development') {
console.debug('[Matomo] Not configured. Skipping analytics.');
}
return;
}
// Initialize Matomo tracking array
(window as any)._paq = (window as any)._paq || [];
const _paq = (window as any)._paq;
// Set up tracking
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
_paq.push(['setTrackerUrl', `${matomoUrl}matomo.php`]);
_paq.push(['setSiteId', matomoSiteId]);
// Inject the Matomo script
const script = document.createElement('script');
script.async = true;
script.src = `${matomoUrl}matomo.js`;
script.defer = true;
const firstScript = document.getElementsByTagName('script')[0];
if (firstScript && firstScript.parentNode) {
firstScript.parentNode.insertBefore(script, firstScript);
}
}, []);
return null;
}