Skip to content

Commit 1f9f6a1

Browse files
committed
Properly use consent mode
1 parent 2aec0c4 commit 1f9f6a1

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

pages/_app.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import type { AppProps } from "next/app"
22
import Head from "next/head"
3-
import { useEffect, useRef, useState } from "react"
3+
import { useEffect, useState } from "react"
44
import CookieConsent, { getCookieConsentValue } from "react-cookie-consent"
55
import "tailwindcss/tailwind.css"
66
import Footer from "../components/Footer"
77
import NavBar from "../components/NavBar"
88
import "../public/global.css"
99
import * as gtag from "../utils/gtag"
10-
import Script from "next/script"
1110

1211
export default function MyApp({ Component, pageProps, router }: AppProps) {
1312
const [consented, setConsented] = useState(null as boolean | undefined | null)
@@ -23,27 +22,25 @@ export default function MyApp({ Component, pageProps, router }: AppProps) {
2322
}, [])
2423

2524
useEffect(() => {
26-
if (!consented)
27-
return
25+
if (consented)
26+
gtag.allowCookies()
27+
}, [consented])
2828

29+
useEffect(() => {
2930
const handleRouteChange = (url: string) => {
3031
gtag.pageview(url)
3132
}
3233
router.events.on("routeChangeComplete", handleRouteChange)
3334
return () => {
3435
router.events.off("routeChangeComplete", handleRouteChange)
3536
}
36-
}, [router.events, consented])
37+
}, [router.events])
3738

3839
return <div className="bg-slate-50 dark:bg-slate-700 min-h-screen flex flex-col items-center justify-between text-slate-900 dark:text-slate-100">
3940
<Head>
4041
<title>{router.pathname.substring(1).replace(/^\w/, w => w.toUpperCase())} | Hu Tao</title>
4142
<link rel="icon" href="/favicon.ico" />
4243
<meta httpEquiv="content-language" content="en-us"></meta>
43-
44-
{consented && <Script async
45-
src={`https://www.googletagmanager.com/gtag/js?id=${gtag.GA_TRACKING_ID}`}
46-
/>}
4744
</Head>
4845

4946
<div className="w-full">

pages/_document.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@ export default class MyDocument extends Document {
1212
__html: `
1313
window.dataLayer = window.dataLayer || [];
1414
function gtag(){dataLayer.push(arguments);}
15-
gtag('js', new Date());
16-
gtag('config', '${GA_TRACKING_ID}', {
15+
gtag("consent", "default", {
16+
"ad_storage": "denied",
17+
"analytics_storage": "denied"
18+
});
19+
gtag('set', 'anonymizeIp', true);
20+
gtag("js", new Date());
21+
gtag("config", "${GA_TRACKING_ID}", {
1722
page_path: window.location.pathname,
1823
});
1924
`,
2025
}}
2126
/>
27+
<script async src={`https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}`} />
2228
</Head>
2329
<body>
2430
<Main />

utils/gtag.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
export const GA_TRACKING_ID = "G-0SJQ3GQCGE"
22

33
// https://developers.google.com/analytics/devguides/collection/gtagjs/pages
4-
export const pageview = (url) => {
5-
window.gtag("config", GA_TRACKING_ID, {
6-
page_path: url,
7-
})
4+
export function pageview(url) {
5+
window.gtag("config", GA_TRACKING_ID, {
6+
page_path: url,
7+
})
88
}
99

1010
// https://developers.google.com/analytics/devguides/collection/gtagjs/events
11-
export const event = ({ action, category, label, value }) => {
12-
window.gtag("event", action, {
13-
event_category: category,
14-
event_label: label,
15-
value: value,
16-
})
11+
export function event({ action, category, label, value }) {
12+
window.gtag("event", action, {
13+
event_category: category,
14+
event_label: label,
15+
value: value,
16+
})
17+
}
18+
19+
export function allowCookies() {
20+
window.gtag("consent", "update", {
21+
"ad_storage": "granted",
22+
"analytics_storage": "granted"
23+
})
1724
}

0 commit comments

Comments
 (0)