Repository navigation
Expand file tree
/
Copy pathconsent.js
More file actions
125 lines (114 loc) · 4.09 KB
/
Copy pathconsent.js
File metadata and controls
125 lines (114 loc) · 4.09 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Loads Google Analytics only after the visitor accepts, and never before.
* The choice is kept in localStorage and can be changed from the footer at any time.
*/
(function () {
var STORAGE_KEY = 'philjay-analytics-consent';
var MEASUREMENT_ID = 'G-9EDNTGFEM7';
var banner = null;
var TEXT = document.documentElement.lang.indexOf('de') === 0 ? {
label: 'Cookie-Hinweis',
message: 'Diese Seite würde Besuche gerne mit Google Analytics zählen, das Cookies in deinem Browser speichert. ' +
'Es läuft nur, wenn du zustimmst, und bis dahin wird nichts gemessen. ',
link: 'Was erfasst wird',
privacy: '/de/datenschutz.html',
decline: 'Ablehnen',
allow: 'Erlauben'
} : {
label: 'Cookie notice',
message: 'This site would like to count visits with Google Analytics, which stores cookies in your browser. ' +
'It runs only if you allow it, and nothing is measured until you do. ',
link: 'What is collected',
privacy: '/privacy.html',
decline: 'Decline',
allow: 'Allow'
};
function stored() {
try {
return window.localStorage.getItem(STORAGE_KEY);
} catch (error) {
return null;
}
}
function remember(choice) {
try {
window.localStorage.setItem(STORAGE_KEY, choice);
} catch (error) {
// A browser with storage blocked simply asks again next time.
}
}
function startAnalytics() {
if (window.__analyticsStarted) return;
window.__analyticsStarted = true;
window.dataLayer = window.dataLayer || [];
window.gtag = function () { window.dataLayer.push(arguments); };
window.gtag('consent', 'default', {
ad_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied',
analytics_storage: 'granted'
});
window.gtag('js', new Date());
window.gtag('config', MEASUREMENT_ID);
var script = document.createElement('script');
script.async = true;
script.src = 'https://www.googletagmanager.com/gtag/js?id=' + MEASUREMENT_ID;
document.head.appendChild(script);
}
function clearAnalyticsCookies() {
var names = document.cookie.split(';').map(function (pair) { return pair.split('=')[0].trim(); });
names.forEach(function (name) {
if (name.indexOf('_ga') !== 0) return;
var expiry = '; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
document.cookie = name + '=' + expiry;
document.cookie = name + '=' + expiry + '; domain=.' + window.location.hostname;
});
}
function hide() {
if (!banner) return;
banner.remove();
banner = null;
}
function choose(choice) {
remember(choice);
hide();
if (choice === 'granted') startAnalytics();
else clearAnalyticsCookies();
}
function show() {
if (banner) return;
banner = document.createElement('div');
banner.className = 'consent';
banner.setAttribute('role', 'dialog');
banner.setAttribute('aria-label', TEXT.label);
banner.innerHTML =
'<p>' + TEXT.message + '<a href="' + TEXT.privacy + '">' + TEXT.link + '</a>.</p>' +
'<div class="consent-actions">' +
'<button type="button" class="button" data-choice="denied">' + TEXT.decline + '</button>' +
'<button type="button" class="button primary" data-choice="granted">' + TEXT.allow + '</button>' +
'</div>';
banner.addEventListener('click', function (event) {
var button = event.target.closest('button[data-choice]');
if (button) choose(button.getAttribute('data-choice'));
});
document.body.appendChild(banner);
}
// Changing your mind: any element with data-cookie-settings reopens the question.
document.addEventListener('click', function (event) {
var trigger = event.target.closest('[data-cookie-settings]');
if (!trigger) return;
event.preventDefault();
try {
window.localStorage.removeItem(STORAGE_KEY);
} catch (error) {
// Nothing to remove.
}
show();
});
var choice = stored();
if (choice === 'granted') startAnalytics();
else if (choice !== 'denied') {
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', show);
else show();
}
})();