Skip to content

Commit 847d699

Browse files
authored
docs(misc): add cookies prompt (#32197)
Add cookie consent for website using [Cookiebot](https://www.cookiebot.com). Moves all tracking logic into `window.addEventListener('CookiebotOnAccept', ...)` callback.
1 parent bc78f06 commit 847d699

6 files changed

Lines changed: 942 additions & 101 deletions

File tree

nx-dev/feature-analytics/src/lib/google-analytics.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,33 @@ import { Gtag } from './gtag';
77

88
declare const gtag: Gtag;
99

10+
declare global {
11+
interface Window {
12+
Cookiebot?: any;
13+
}
14+
}
15+
1016
export function sendPageViewEvent(data: {
1117
gaId: string;
1218
path?: string;
1319
title?: string;
1420
}): void {
1521
if (process.env.NODE_ENV !== 'production') return;
22+
23+
// Check if user has consented to statistics cookies
24+
if (
25+
typeof window !== 'undefined' &&
26+
window.Cookiebot &&
27+
!window.Cookiebot.consent.statistics
28+
) {
29+
return;
30+
}
31+
32+
// Check if gtag is available (might not be loaded yet if consent was just given)
33+
if (typeof gtag === 'undefined') {
34+
return;
35+
}
36+
1637
try {
1738
gtag('config', data.gaId, {
1839
...(!!data.path && { page_path: data.path }),
@@ -31,6 +52,21 @@ export function sendCustomEvent(
3152
customObject?: Record<string, unknown>
3253
): void {
3354
if (process.env.NODE_ENV !== 'production') return;
55+
56+
// Check if user has consented to statistics cookies
57+
if (
58+
typeof window !== 'undefined' &&
59+
window.Cookiebot &&
60+
!window.Cookiebot.consent.statistics
61+
) {
62+
return;
63+
}
64+
65+
// Check if gtag is available (might not be loaded yet if consent was just given)
66+
if (typeof gtag === 'undefined') {
67+
return;
68+
}
69+
3470
try {
3571
gtag('event', action, {
3672
event_category: category,

0 commit comments

Comments
 (0)