diff options
-rw-r--r-- | site/js/google-analytics-message.js | 87 |
1 files changed, 48 insertions, 39 deletions
diff --git a/site/js/google-analytics-message.js b/site/js/google-analytics-message.js index 5c3e1fa..c776a8a 100644 --- a/site/js/google-analytics-message.js +++ b/site/js/google-analytics-message.js @@ -1,48 +1,57 @@ -// function to toggle Google Analytics message -const toggleGoogleAnalyticsMessage = action => { - const message = document.getElementById('google-analytics-message') - action === 'show' && message.classList.add('open') - action === 'hide' && message.classList.remove('open') -} - -// check if cookies with user choice exist -const isGoogleAnalyticsAllowedValueExist = - document.cookie.split(';') - .find(row => row.includes('isGoogleAnalyticsAllowed')) - -// if the user has already made a choice -if (isGoogleAnalyticsAllowedValueExist) { - - const isGoogleAnalyticsAllowedValue = isGoogleAnalyticsAllowedValueExist.split('=')[1] - - // and if the user chose "Accept" - connect Google Analytics - if (isGoogleAnalyticsAllowedValue === 'yes') { - window.dataLayer = window.dataLayer || []; - function gtag() { dataLayer.push(arguments); } - gtag('js', new Date()); - gtag('config', 'G-J3WHFQG00P'); +document.addEventListener('DOMContentLoaded', function (event) { + + function determineDataOfExpiration() { + const date = new Date(); + date.setMonth(date.getMonth() + 1); // Add one month to the current date + const expiresString = "expires=" + date.toUTCString() + return expiresString + } + // function to toggle Google Analytics message + const toggleGoogleAnalyticsMessage = action => { + const message = document.getElementById('google-analytics-message') + action === 'show' && message.classList.add('open') + action === 'hide' && message.classList.remove('open') } -} else { + // check if cookies with user choice exist + const isGoogleAnalyticsAllowedValueExist = + document.cookie.split(';') + .find(row => row.includes('isGoogleAnalyticsAllowed')) + + // if the user has already made a choice + if (isGoogleAnalyticsAllowedValueExist) { + + const isGoogleAnalyticsAllowedValue = isGoogleAnalyticsAllowedValueExist.split('=')[1] - // display a message with a question - toggleGoogleAnalyticsMessage('show') + // and if the user chose "Accept" - connect Google Analytics + if (isGoogleAnalyticsAllowedValue === 'yes') { + window.dataLayer = window.dataLayer || []; + function gtag() { dataLayer.push(arguments); } + gtag('js', new Date()); + gtag('config', 'G-J3WHFQG00P'); + } - const googleAnalyticsMessageAccept = document.querySelector('#google-analytics-accept') - const googleAnalyticsMessageDecline = document.querySelectorAll('#google-analytics-decline') + } else { - // register a click on the "Accept" button - googleAnalyticsMessageAccept.addEventListener('click', function () { - document.cookie = 'isGoogleAnalyticsAllowed=yes' - toggleGoogleAnalyticsMessage('hide') - }) + // display a message with a question + toggleGoogleAnalyticsMessage('show') - // register a click on the "Decline" button - googleAnalyticsMessageDecline.forEach(el => { - el.addEventListener('click', function () { - document.cookie = 'isGoogleAnalyticsAllowed=no' + const googleAnalyticsMessageAccept = document.querySelector('#google-analytics-accept') + const googleAnalyticsMessageDecline = document.querySelectorAll('#google-analytics-decline') + + // register a click on the "Accept" button + googleAnalyticsMessageAccept.addEventListener('click', function () { + document.cookie = `isGoogleAnalyticsAllowed=yes;${determineDataOfExpiration()};path=/` toggleGoogleAnalyticsMessage('hide') }) - }) -}
\ No newline at end of file + // register a click on the "Decline" button + googleAnalyticsMessageDecline.forEach(el => { + el.addEventListener('click', function () { + document.cookie = `isGoogleAnalyticsAllowed=no;${determineDataOfExpiration()};path=/` + toggleGoogleAnalyticsMessage('hide') + }) + }) + + } +})
\ No newline at end of file |