mirror of
https://github.com/arthur-pbty/flint.git
synced 2026-08-01 20:29:03 +02:00
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:
@@ -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>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user