noscript varianten eingebaut, matomo integration erweitert und als konfiguration aufgenommen.

This commit is contained in:
Codex Agent
2025-12-05 17:02:03 +01:00
parent 946d09764a
commit c1bd4c1eb3
15 changed files with 331 additions and 95 deletions

View File

@@ -21,10 +21,29 @@ interface MatomoTrackerProps {
}
const MatomoTracker: React.FC<MatomoTrackerProps> = ({ config }) => {
const page = usePage<{ security?: { csp?: { scriptNonce?: string } } }>();
const { hasConsent } = useConsent();
const scriptNonce = (page.props.security as { csp?: { scriptNonce?: string } } | undefined)?.csp?.scriptNonce;
const analyticsConsent = hasConsent('analytics');
let scriptNonce: string | undefined;
let analyticsConsent = false;
let pageUrl = typeof window !== 'undefined' ? window.location.pathname + window.location.search : '';
try {
const page = usePage<{ security?: { csp?: { scriptNonce?: string } } }>();
scriptNonce = (page.props.security as { csp?: { scriptNonce?: string } } | undefined)?.csp?.scriptNonce;
pageUrl = page.url || pageUrl;
} catch {
// Not in Inertia context; fall back to window values/meta tags
const metaNonce = typeof document !== 'undefined'
? document.querySelector('meta[name="csp-nonce"]')?.getAttribute('content')
: undefined;
scriptNonce = metaNonce ?? undefined;
}
try {
const consent = useConsent();
analyticsConsent = consent.hasConsent('analytics');
} catch {
// No consent provider available; default to no analytics
analyticsConsent = false;
}
useEffect(() => {
if (!config?.enabled || !config.url || !config.siteId || typeof window === 'undefined') {
@@ -92,14 +111,14 @@ const MatomoTracker: React.FC<MatomoTrackerProps> = ({ config }) => {
window._paq = window._paq || [];
const { _paq } = window;
const currentUrl =
typeof window !== 'undefined' ? `${window.location.origin}${page.url}` : page.url;
typeof window !== 'undefined' ? `${window.location.origin}${pageUrl}` : pageUrl;
_paq.push(['setCustomUrl', currentUrl]);
if (typeof document !== 'undefined') {
_paq.push(['setDocumentTitle', document.title]);
}
_paq.push(['trackPageView']);
}, [config, analyticsConsent, page.url]);
}, [config, analyticsConsent, pageUrl]);
if (!config?.enabled || !config.url || !config.siteId || !analyticsConsent) {
return null;