'use client'; import Script from 'next/script'; import { usePathname, useSearchParams } from 'next/navigation'; import { useEffect } from 'react'; export default function GoogleAnalytics() { const pathname = usePathname(); const searchParams = useSearchParams(); const GA_MEASUREMENT_ID = process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID; useEffect(() => { if (!GA_MEASUREMENT_ID) return; const cookieConsent = localStorage.getItem('cookieConsent'); if (cookieConsent !== 'accepted') return; const url = pathname + (searchParams?.toString() ? `?${searchParams.toString()}` : ''); if (typeof window.gtag !== 'undefined') { window.gtag('config', GA_MEASUREMENT_ID, { page_path: url, }); } }, [pathname, searchParams, GA_MEASUREMENT_ID]); const cookieConsent = typeof window !== 'undefined' ? localStorage.getItem('cookieConsent') : null; if (!GA_MEASUREMENT_ID || cookieConsent !== 'accepted') { return null; } return ( <>