-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Add GDPR-compliant cookie consent with analytics blocking #3492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
11
commits into
main
Choose a base branch
from
copilot/add-gdpr-cookie-consent-dialog
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+292
−12
Open
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0c497c8
Initial plan
Copilot 31d936b
Add GDPR cookie consent dialog with analytics integration
Copilot 3728004
Fixed library version
george-gca 86f34c7
Fixed issue with library usage
george-gca d695535
Added function to clear cookies
george-gca 08a8090
Added information about other analytics
george-gca 1784a36
Adding theme change to cookie consent dialog
george-gca 970d8d6
Fixed issue when clicking on Reject all button
george-gca b433dee
Fixed prettier complaints
george-gca 0e5bc9b
Disable cookie consent by default
george-gca 8e7a60d
Moved vanilla-cookieconsent item in _config
george-gca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| --- | ||
| 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 || []; | ||
| function gtag() { | ||
| window.dataLayer.push(arguments); | ||
| } | ||
| gtag('consent', 'default', { | ||
| 'ad_storage': 'denied', | ||
| 'analytics_storage': 'denied', | ||
| 'functionality_storage': 'denied', | ||
| 'personalization_storage': 'denied' | ||
| }); | ||
|
|
||
| // Wait for the library to be available | ||
| function initializeCookieConsent() { | ||
| // Check if CookieConsent is available | ||
| if (!window.CookieConsent) { | ||
| // Library not yet loaded, try again after a short delay | ||
| setTimeout(initializeCookieConsent, 100); | ||
| 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(); | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.