-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented the analytics consent banner for Playground
- Loading branch information
1 parent
9e165ac
commit cdacaba
Showing
5 changed files
with
204 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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,57 @@ | ||
const analyticsAcceptBtn = document.getElementById("accept-analytics-btn") | ||
const analyticsDeclineBtn = document.getElementById("decline-analytics-btn") | ||
const analyticsBanner = document.getElementById("analytics-banner") | ||
const manageAnalyticsLink = document.getElementById("consent-link") | ||
|
||
// Show banner if the user has not set any preference | ||
if(localStorage.getItem('consentMode') === null){ | ||
analyticsBanner.classList.remove("hidden") | ||
} | ||
|
||
// Open analytics banner when clicking on the cookie manager link | ||
manageAnalyticsLink.addEventListener("click", () => { | ||
analyticsBanner.classList.remove("hidden") | ||
}) | ||
|
||
//If analytics are rejected, close the banner and update the new user preference | ||
analyticsDeclineBtn.addEventListener("click", () => { | ||
analyticsBanner.classList.add("hidden") | ||
|
||
const consentPreferences = { | ||
'ad_storage': 'denied', | ||
'analytics_storage': 'denied', | ||
'ad_user_data': 'denied', | ||
'ad_personalization': 'denied', | ||
'personalization_storage': 'denied', | ||
'functionality_storage': 'denied', | ||
'security_storage': 'denied', | ||
} | ||
|
||
updateConsentMode(consentPreferences) | ||
}) | ||
|
||
//If analytics are accepted, close the banner and update the new user preference | ||
analyticsAcceptBtn.addEventListener("click", () => { | ||
analyticsBanner.classList.add("hidden") | ||
|
||
const consentPreferences = { | ||
'ad_storage': 'denied', | ||
'analytics_storage': 'granted', | ||
'ad_user_data': 'denied', | ||
'ad_personalization': 'denied', | ||
'personalization_storage': 'denied', | ||
'functionality_storage': 'denied', | ||
'security_storage': 'denied', | ||
} | ||
|
||
updateConsentMode(consentPreferences) | ||
}) | ||
|
||
/** | ||
* Update the user preferences to grant or decline google analytics tracking | ||
* @param { Object } preferences | ||
*/ | ||
function updateConsentMode(preferences) { | ||
gtag('consent', 'update', preferences) | ||
Check failure Code scanning / ESLint disallow the use of undeclared variables unless mentioned in `/*global */` comments Error
'gtag' is not defined.
|
||
localStorage.setItem('consentMode', JSON.stringify(preferences)) | ||
} |
This file contains 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 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 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 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