|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { Suspense, useCallback, useEffect, useState } from 'react'; |
| 4 | +import { usePathname, useSearchParams } from 'next/navigation'; |
| 5 | + |
| 6 | +import { CookieNotice } from '@/components/Matomo/consent'; |
| 7 | +import { init, push } from '@/util/matomo'; |
| 8 | +import { env } from '@/env.mjs'; |
| 9 | + |
| 10 | +const MATOMO_URL = env.NEXT_PUBLIC_MATOMO_URL; |
| 11 | +const MATOMO_CDN_URL = env.NEXT_PUBLIC_MATOMO_CDN_URL; |
| 12 | +const MATOMO_SITE_ID = env.NEXT_PUBLIC_MATOMO_SITE_ID; |
| 13 | +const CONSENT_SID = 'c_sid'; |
| 14 | + |
| 15 | +function Matomo() { |
| 16 | + const searchParams = useSearchParams(); |
| 17 | + const pathname = usePathname(); |
| 18 | + const [isOpen, setIsOpen] = useState(false); |
| 19 | + |
| 20 | + const searchParamsString = searchParams.toString(); |
| 21 | + const openConsent = useCallback(() => setIsOpen(true), []); |
| 22 | + |
| 23 | + const onAccept = () => { |
| 24 | + if (MATOMO_URL && MATOMO_SITE_ID && MATOMO_CDN_URL) { |
| 25 | + init({ |
| 26 | + url: MATOMO_URL, |
| 27 | + cdnUrl: MATOMO_CDN_URL, |
| 28 | + siteId: MATOMO_SITE_ID, |
| 29 | + disableCookies: false, |
| 30 | + }); |
| 31 | + localStorage.setItem( |
| 32 | + CONSENT_SID, |
| 33 | + JSON.stringify({ |
| 34 | + id: crypto.randomUUID(), |
| 35 | + lastUpdated: Math.floor(Date.now() / 1000), |
| 36 | + r: true, |
| 37 | + }) |
| 38 | + ); |
| 39 | + setIsOpen(false); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + const onDecline = () => { |
| 44 | + localStorage.setItem( |
| 45 | + CONSENT_SID, |
| 46 | + JSON.stringify({ |
| 47 | + id: crypto.randomUUID(), |
| 48 | + lastUpdated: Math.floor(Date.now() / 1000), |
| 49 | + r: false, |
| 50 | + }) |
| 51 | + ); |
| 52 | + setIsOpen(false); |
| 53 | + }; |
| 54 | + |
| 55 | + useEffect(() => { |
| 56 | + const savedConsent = localStorage.getItem(CONSENT_SID); |
| 57 | + if (!savedConsent) openConsent(); |
| 58 | + }, [openConsent]); |
| 59 | + |
| 60 | + useEffect(() => { |
| 61 | + if (!pathname) return; |
| 62 | + const url = `${pathname}${searchParamsString ? '?' + decodeURIComponent(searchParamsString) : ''}`; |
| 63 | + push(['setCustomUrl', url]); |
| 64 | + push(['trackPageView']); |
| 65 | + }, [pathname, searchParamsString]); |
| 66 | + |
| 67 | + useEffect(() => { |
| 68 | + try { |
| 69 | + const savedConsent = localStorage.getItem(CONSENT_SID); |
| 70 | + if (savedConsent) { |
| 71 | + const consentParsed = JSON.parse(savedConsent); |
| 72 | + if (consentParsed.r) { |
| 73 | + if (MATOMO_URL && MATOMO_SITE_ID && MATOMO_CDN_URL) { |
| 74 | + init({ |
| 75 | + url: MATOMO_URL, |
| 76 | + cdnUrl: MATOMO_CDN_URL, |
| 77 | + siteId: MATOMO_SITE_ID, |
| 78 | + disableCookies: false, |
| 79 | + }); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } catch (error) { |
| 84 | + // eslint-disable-next-line no-console |
| 85 | + console.error('failed to parse the consent'); |
| 86 | + } |
| 87 | + }, []); |
| 88 | + |
| 89 | + return <CookieNotice isOpen={isOpen} onAccept={onAccept} onDecline={onDecline} />; |
| 90 | +} |
| 91 | + |
| 92 | +export default function MatomoAnalyticsConsent() { |
| 93 | + return ( |
| 94 | + <Suspense> |
| 95 | + <Matomo /> |
| 96 | + </Suspense> |
| 97 | + ); |
| 98 | +} |
0 commit comments