-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcookie-consent-setup.js
More file actions
160 lines (145 loc) · 5.42 KB
/
cookie-consent-setup.js
File metadata and controls
160 lines (145 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
---
permalink: /assets/js/cookie-consent-setup.js
---
/**
* Cookie Consent Configuration
* Documentation: https://cookieconsent.orestbida.com/
*
* GDPR-Compliant Approach:
* - Analytics scripts use type="text/plain" data-category="analytics"
* - The library blocks all marked scripts until user consents
* - Scripts NEVER run until explicit consent is given
* - Google Consent Mode is used for Google Analytics privacy mode before consent
* - Other analytics (Cronitor, Pirsch, OpenPanel) are blocked until consent given
*
* Supported Analytics Providers:
* - Cronitor RUM
* - Google Analytics (GA4)
* - OpenPanel Analytics
* - Pirsch Analytics
*/
// Initialize Google Consent Mode BEFORE any tracking
// This tells Google services to operate in privacy mode until user consents
window.dataLayer = window.dataLayer || [];
// Reuse existing global gtag if it was already defined (e.g. by other GA scripts)
// to avoid redefining it multiple times when consent is granted.
if (typeof window.gtag !== 'function') {
window.gtag = function() {
window.dataLayer.push(arguments);
};
}
// Local alias for convenience in this file
var gtag = window.gtag;
gtag('consent', 'default', {
'ad_storage': 'denied',
'analytics_storage': 'denied',
'functionality_storage': 'denied',
'personalization_storage': 'denied'
});
// Wait for the library to be available
var cookieConsentRetryCount = 0;
var COOKIE_CONSENT_MAX_RETRIES = 50; // 5 seconds max wait time
function initializeCookieConsent() {
// Check if CookieConsent is available
if (!window.CookieConsent) {
if (cookieConsentRetryCount++ < COOKIE_CONSENT_MAX_RETRIES) {
// Library not yet loaded, try again after a short delay
setTimeout(initializeCookieConsent, 100);
} else {
console.error('CookieConsent library failed to load');
}
return;
}
window.CookieConsent.run({
categories: {
necessary: {
enabled: true,
readOnly: true
},
analytics: {}
},
language: {
default: 'en',
translations: {
en: {
consentModal: {
title: 'We use cookies',
description: 'This website uses cookies to improve your experience and analyze site traffic. By clicking "Accept all", you consent to our use of cookies.',
acceptAllBtn: 'Accept all',
acceptNecessaryBtn: 'Reject all',
showPreferencesBtn: 'Manage Individual preferences'
},
preferencesModal: {
title: 'Manage cookie preferences',
acceptAllBtn: 'Accept all',
acceptNecessaryBtn: 'Reject all',
savePreferencesBtn: 'Accept current selection',
closeIconLabel: 'Close modal',
sections: [
{
title: 'Cookie usage',
description: 'We use cookies to ensure the basic functionalities of the website and to enhance your online experience. You can choose for each category to opt-in/out whenever you want.'
},
{
title: 'Strictly Necessary cookies',
description: 'These cookies are essential for the proper functioning of the website. Without these cookies, the website would not work properly.',
linkedCategory: 'necessary'
},
{
title: 'Analytics cookies',
description: 'These cookies allow us to measure traffic and analyze your behavior to improve our service.',
linkedCategory: 'analytics'
},
{
title: 'More information',
description: 'For any queries in relation to our policy on cookies and your choices, please <a class="cc-link" href="{{ site.url }}{{ site.baseurl }}/#contact">contact us</a>.'
}
]
}
}
}
},
// Callback when user accepts/rejects consent
onFirstConsent: function(consentData) {
updateConsentMode(consentData);
},
// Callback when user changes preferences
onChange: function(consentData) {
updateConsentMode(consentData);
}
});
/**
* Update Google Consent Mode based on user preferences
* This ensures Google services respect user choices
*/
function updateConsentMode(consentData) {
// Handle both callback data structures
var categories = consentData.categories || consentData;
// Ensure categories is an object
if (!categories || typeof categories !== 'object') {
console.warn('Invalid consent data structure:', consentData);
return;
}
gtag('consent', 'update', {
'analytics_storage': categories.analytics ? 'granted' : 'denied',
'ad_storage': 'denied',
'functionality_storage': 'denied',
'personalization_storage': 'denied'
});
if (categories.analytics) {
console.debug('✓ Analytics consent granted - tracking enabled for all providers');
// Analytics scripts with data-category="analytics" will automatically run
// when the library re-evaluates them after this consent update
} else {
console.debug('✗ Analytics consent denied - no tracking data collected');
// Analytics scripts are already blocked by the library (type="text/plain")
// No tracking will occur for:
// - Cronitor RUM
// - Google Analytics (GA4)
// - OpenPanel Analytics
// - Pirsch Analytics
}
}
}
// Initialize when the library is available
initializeCookieConsent();