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
| <script>
// This script is copied from
// https://developers.cloudflare.com/zaraz/consent-management/api/
// With some modifications
function getCookie(name) {
const value = `; ${document.cookie}`;
return value?.split(`; ${name}=`)[1]?.split(";")[0];
}
function handleZarazConsentAPIReady() {
const consent_cookie = getCookie("cf_consent");
const isEUCountry = "{{system.device.location.isEUCountry}}" === "1";
const isUKCountry = "{{ system.device.location.country }}" === "GB";
const isCalfornia =
"{{ system.device.location.region }}" === "California";
const shouldConsent = isEUCountry || isUKCountry || isCalfornia;
console.log(
`isEUCountry: ${isEUCountry}, isUKCountry: ${isUKCountry}, isCalfornia: ${isCalfornia}, consent_cookie: ${consent_cookie}`,
);
if (!consent_cookie) {
if (shouldConsent) {
console.log("consent needed");
zaraz.consent.modal = true;
zaraz.consent.setAllCheckboxes(true);
} else {
console.log("consent not needed");
zaraz.consent.setAll(true);
zaraz.consent.sendQueuedEvents();
}
} else {
// Check for updates
const currentConsents = JSON.parse(consent_cookie);
const allConsents = zaraz.consent.getAll();
// Every key name in allConsents should be in currentConsents,
// else we need to update
var updateFlag = false;
for (const key in allConsents) {
if (currentConsents[key] == undefined) {
updateFlag = true;
break;
}
}
// If updateFlag is true, we need to update
// Consent again
if (updateFlag) {
console.log("consent updated");
if (shouldConsent) {
console.log("consent needed");
zaraz.consent.modal = true;
zaraz.consent.setAllCheckboxes(true);
} else {
console.log("consent not needed");
zaraz.consent.setAll(true);
zaraz.consent.sendQueuedEvents();
}
}
}
}
if (zaraz.consent?.APIReady) {
handleZarazConsentAPIReady();
} else {
document.addEventListener(
"zarazConsentAPIReady",
handleZarazConsentAPIReady,
);
}
</script>
|