"use client"; import { useEffect, useState } from "react"; import { usePathname } from "next/navigation"; import { getConsent, hasCurrentConsentDecision, setAnalyticsConfig, setConsent, track } from "@/lib/analytics/client"; type CategoryText = { label: string; description: string }; type AnalyticsProps = { consentVersion: string; preferencesEnabled: boolean; statisticsEnabled: boolean; necessary: CategoryText; preferences: CategoryText; statistics: CategoryText; }; const buttonBase = "inline-flex items-center justify-center rounded-full px-6 py-3 text-sm font-medium tracking-wide transition-colors duration-300 focus-visible:outline-2 focus-visible:outline-offset-4"; /** * Mounted once in app/(frontend)/layout.tsx. Combines two independent jobs: * (1) the cookie-consent banner, and (2) automatic page_view tracking on * every client-side route change. Both only ever act through track()/ * setAnalyticsConfig() from lib/analytics/client.ts - no network call is * possible from this component before a visitor has actually consented. */ export function Analytics({ consentVersion, preferencesEnabled, statisticsEnabled, necessary, preferences, statistics }: AnalyticsProps) { // Plain assignment during render (not an effect): must be set before the // page_view effect below ever fires, on every render including the first. setAnalyticsConfig({ statisticsEnabled, consentVersion }); const pathname = usePathname(); const [showBanner, setShowBanner] = useState(false); const [expanded, setExpanded] = useState(false); const [prefChoice, setPrefChoice] = useState(false); const [statChoice, setStatChoice] = useState(false); useEffect(() => { if (!hasCurrentConsentDecision()) { // eslint-disable-next-line react-hooks/set-state-in-effect setShowBanner(true); const existing = getConsent(); setPrefChoice(existing?.preferences ?? false); setStatChoice(existing?.statistics ?? false); } }, [consentVersion]); useEffect(() => { track("page_view", { path: pathname }); }, [pathname]); function acceptAll() { setConsent({ preferences: preferencesEnabled, statistics: statisticsEnabled }, consentVersion); setShowBanner(false); } function acceptNecessaryOnly() { setConsent({ preferences: false, statistics: false }, consentVersion); setShowBanner(false); } function saveSelection() { setConsent({ preferences: preferencesEnabled && prefChoice, statistics: statisticsEnabled && statChoice }, consentVersion); setShowBanner(false); } if (!showBanner) return null; return (
Wir nutzen Cookies, um die Website technisch bereitzustellen{statisticsEnabled ? " und, mit deiner Zustimmung, datensparsame Statistiken zu erheben" : ""}. Du kannst deine Auswahl jederzeit ändern.