Skip to content

Commit 3037509

Browse files
Implement Google Consent Mode v2:
- Set default 'denied' state in layout - Update CookieConsent component to send 'granted' signals on accept - Resolve 'Consent signals inactive' warnings in GA4
1 parent 53786fc commit 3037509

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

app/layout.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ export default function RootLayout({ children }: RootLayoutProps) {
7979
crossOrigin="anonymous"
8080
strategy="afterInteractive"
8181
/>
82+
<Script id="google-consent-default" strategy="beforeInteractive">
83+
{`
84+
window.dataLayer = window.dataLayer || [];
85+
function gtag(){dataLayer.push(arguments);}
86+
gtag('consent', 'default', {
87+
'ad_storage': 'denied',
88+
'ad_user_data': 'denied',
89+
'ad_personalization': 'denied',
90+
'analytics_storage': 'denied'
91+
});
92+
`}
93+
</Script>
8294
{/* Google Analytics 4 */}
8395
<Script
8496
src="https://www.googletagmanager.com/gtag/js?id=G-1QB54ZRCS5"

components/ui/CookieConsent.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,39 @@ import Link from 'next/link'
66
export default function CookieConsent() {
77
const [visible, setVisible] = useState(false)
88

9+
const updateConsent = (isAccepted: boolean) => {
10+
if (typeof window !== 'undefined' && (window as any).gtag) {
11+
(window as any).gtag('consent', 'update', {
12+
'ad_storage': isAccepted ? 'granted' : 'denied',
13+
'ad_user_data': isAccepted ? 'granted' : 'denied',
14+
'ad_personalization': isAccepted ? 'granted' : 'denied',
15+
'analytics_storage': isAccepted ? 'granted' : 'denied'
16+
})
17+
}
18+
}
19+
920
useEffect(() => {
1021
// Check if consent was already given
1122
const consent = localStorage.getItem('cookie-consent')
1223
if (!consent) {
1324
// Small delay to ensure smooth hydration and visibility
1425
const timer = setTimeout(() => setVisible(true), 500)
1526
return () => clearTimeout(timer)
27+
} else {
28+
// Re-apply consent if already given
29+
updateConsent(consent === 'accepted')
1630
}
1731
}, [])
1832

1933
const accept = () => {
2034
localStorage.setItem('cookie-consent', 'accepted')
35+
updateConsent(true)
2136
setVisible(false)
2237
}
2338

2439
const decline = () => {
2540
localStorage.setItem('cookie-consent', 'declined')
41+
updateConsent(false)
2642
setVisible(false)
2743
}
2844

0 commit comments

Comments
 (0)