blob: 5c3e1faad03fff32caae684d82ee41a5230ec7c2 (
plain)
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
|
// 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');
}
} else {
// display a message with a question
toggleGoogleAnalyticsMessage('show')
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'
toggleGoogleAnalyticsMessage('hide')
})
// register a click on the "Decline" button
googleAnalyticsMessageDecline.forEach(el => {
el.addEventListener('click', function () {
document.cookie = 'isGoogleAnalyticsAllowed=no'
toggleGoogleAnalyticsMessage('hide')
})
})
}
|