-
Notifications
You must be signed in to change notification settings - Fork 7
/
cookies.js
67 lines (55 loc) · 2.06 KB
/
cookies.js
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
function setCookie(name, value='', exdays=365) {
const d = new Date()
d.setTime(d.getTime() + (exdays*24*60*60*1000))
document.cookie = `${name}=${value}; expires=${d.toUTCString()}; path=/`
}
{
// Important key
const id = 'G-HCGGJHJ3DR'
// Read the tracking configuration cookies
let hideBanner = (document.cookie.indexOf('hide-banner') >= 0) || localStorage.getItem('hideBanner')
let disabled = (document.cookie.indexOf('ga-disable=1') >= 0)
// Set up GTagOptIn to allow us to opt in or opt out later
GTagOptIn.register(id)
// If the user has already hidden the banner AND enabled tracking, then go!
if( hideBanner ) {
if( disabled ) GTagOptIn.optOut()
else GTagOptIn.optIn()
}
document.addEventListener('DOMContentLoaded', function() {
// Set up the 'Enable Google Analytics' checkboxes
document.querySelectorAll('input[target=enable-ga]').forEach(e => {
e.checked = hideBanner && !disabled
e.onclick = function() {
// Opt in or out depending
if( this.checked ) GTagOptIn.optOut()
else GTagOptIn.optIn()
// Set the cookie
setCookie('ga-disable', this.checked? '0': '1')
}
})
// Set up the cookies checkboxes
if( hideBanner ) removeCookieBanner()
else showCookieBanner()
})
}
function showCookieBanner() {
Array.from(byClass('cookie-banner')).forEach(e => e.classList.remove('hidden'))
}
function removeCookieBanner() {
Array.from(byClass('cookie-banner')).forEach(e => e.remove())
}
function consentToTracking() {
setCookie('hide-banner')
setCookie('ga-disable', 0)
GTagOptIn.optIn()
removeCookieBanner()
document.querySelectorAll('input[target=enable-ga]').forEach(e => e.checked = true)
}
function denyTracking() {
setCookie('hide-banner')
setCookie('ga-disable', 1)
GTagOptIn.optOut()
removeCookieBanner()
document.querySelectorAll('input[target=enable-ga]').forEach(e => e.checked = false)
}