Cloudflare Zaraz 同意管理脚本

文档

Cloudflare Zaraz

控制同意提示的 Custom HTML 工具

开启“启用同意管理”,关闭“显示同意模式”。

 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>

同意模式文本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<div>
    <p>
        Cookies created by Cloudflare and its tag manager are necessary for the
        website to work and cannot be switched off.
        <a href="https://www.cloudflare.com/privacypolicy/" target="_blank">
            Learn more about Cloudflare privacy policies.
        </a>
    </p>
    <p>
        This website does not target any specific audience, especially not EU,
        UK or Californian audiences.
    </p>
</div>
提交: 7b0d6113 环境: production Hugo: 0.145.0 主题: 3.30.0-modified 时间: 1743994200947172
本博客内容仅供参考,作者不对其准确性、完整性或适用性作出任何明示或暗示的保证。因使用、引用或解读本博客内容所引发的任何直接或间接后果,作者概不承担任何责任。
本博客可能包含第三方转载内容,相关版权归原作者所有。转载内容仅为分享信息之目的,不代表作者观点。如涉及侵权,请联系删除。
使用 Hugo 构建
主题 StackJimmy 设计