-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookieTrackingManager-2.js
239 lines (192 loc) · 6.57 KB
/
cookieTrackingManager-2.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* jshint browser: true, esversion: 6 */
/* global URLSearchParams, console */
const cookieTrackingManager = {
/**
* Consent data
*/
consent: {
},
/**
* Testing function that allows to restore the defaults for consent data
*/
restoreDefault: function () {
this.consent = {
updated: 20200123,
allowAll: true,
denyAll: false,
cats: {
analytics: true,
segmentation: true,
advertisement: false
}
};
},
/**
* Reads the stored cookie preferences
* @returns {object} Returns stored cookie preferences or {}
*/
read: function () {
const consentString = localStorage.cookieSettings;
if (consentString === undefined) {
return {};
}
const consentObject = JSON.parse(consentString);
this.consent = consentObject;
},
/**
* Write the cookie options
*/
write: function () {
this.consent.updated = this.getCurrentDay();
const consentString = JSON.stringify(this.consent);
localStorage.cookieSettings = consentString;
},
/**
* Writes an event to the dataLayer object if it exists.
* The event contains information about the user's consent for different categories of tracking.
*/
writeEvent: function () {
if (typeof dataLayer === "object") {
dataLayer.push({
'event': 'consent_given',
'consent_analytics': this.canItrack('analytics').toString().toUpperCase(),
'consent_segmentation': this.canItrack('segmentation').toString().toUpperCase(),
'consent_marketing': this.canItrack('advertisement').toString().toUpperCase()
});
}
},
/**
* Erases a specific cookie by name and domain
* @param {string} name The name of the cookie to be erased
* @param {string} domain The domain of the cookie to be erased
*/
erraseCookie: function (name, domain) {
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; domain=${domain}`;
},
/**
* Errase all cookies for users that do not want to be tracked, and write their preferences
*/
erraseAll: function () {
const cookies = document.cookie.split(";");
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i];
const eqPos = cookie.indexOf("=");
const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
}
const localKeys = Object.keys(localStorage);
localKeys.forEach(key => {
localStorage.removeItem(key);
});
const sesKeys = Object.keys(sessionStorage);
sesKeys.forEach(key => {
sessionStorage.removeItem(key);
});
this.write();
},
/**
* Checks if a specific tracker can track
* @param {string} category Category of the tracker
* @returns {boolean}
*/
canItrack: function (category) {
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.has("tracking")) {
if (urlParams.get("tracking") == "deny_all") {
return false;
}
if (urlParams.get("tracking") == "allow_all") {
return true;
}
}
if (navigator.doNotTrack == "1") {
return false;
}
if (typeof (this.consent.updated) === "number") {
if (this.daysSinceConsent() > 365 * 2) {
return false;
}
}
if (this.consent.denyAll === true) {
return false;
}
if (this.consent.allowAll === true) {
return true;
}
if (this.consent.cats !== undefined) {
if (this.consent.cats[category] === true) {
return true;
} else if (this.consent.cats[category] === false) {
return false;
} else {
console.error("This cookie category does not exist");
}
}
return false;
},
/**
* Gets current day in the format 20201231
* @returns {number}
*/
getCurrentDay: function () {
const today = new Date();
const year = String(today.getFullYear());
const monthTemp = today.getMonth() + 1;
let month;
if (monthTemp < 10) {
month = "0" + String(monthTemp);
} else {
month = String(monthTemp);
}
const dayTemp = today.getDate();
let day;
if (dayTemp < 10) {
day = "0" + String(dayTemp);
} else {
day = String(dayTemp);
}
return Number(year + month + day);
},
/**
* Calculates the number of days since the user answered to consent
* @returns {number} Days since consent, float
*/
daysSinceConsent: function () {
if (typeof (this.consent.updated) === "number") {
const now = new Date();
const consentUpdatedString = this.consent.updated.toString();
const year = Number(consentUpdatedString.substring(0, 4));
const month = Number(consentUpdatedString.substring(4, 6)) - 1;
const day = Number(consentUpdatedString.substring(6, 8));
const updatedDate = new Date(year, month, day); // Values from this.consent.updated
const milisecondsSinceConsent = now - updatedDate;
const daysSinceConsent = milisecondsSinceConsent / 1000 / 60 / 60 / 24;
return daysSinceConsent;
} else {
console.error("Consent date was not defined");
}
},
/**
* Checks if the UI has to ask consent
* @returns {boolean} Returns true if it has to ask consent
*/
needToAskConsent: function () {
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.has("tracking")) {
if (urlParams.get("tracking") == "deny_all" || urlParams.get("tracking") == "allow_all" ) {
return false;
}
}
if (typeof (this.consent.updated) !== "number") {
return true;
} else {
if (this.daysSinceConsent() > 365 * 2) {
return true;
} else {
return false;
}
}
console.error("Can't determine if I need to ask for consent");
return true;
}
};