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
 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
<script>
    // This script is copied from
    // https://developers.cloudflare.com/zaraz/consent-management/api/
    // With a lot of modifications
    function getCookie(name) {
        const value = `; ${document.cookie}`;
        return value?.split(`; ${name}=`)[1]?.split(";")[0];
    }

    function handleZarazConsentAPIReady() {
        const logPrefix = "[handleZarazConsentAPIReady]";
        const cookieName = "cf_consent";
        // Consent Cookie
        const consentCookie = getCookie(cookieName);
        // Get the current country, the variable will be injected by Zaraz
        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;
        // Get current consents
        let currentConsents = {};
        if (consentCookie) {
            try {
                currentConsents = JSON.parse(consentCookie);
            } catch (error) {
                // Broken JSON
                console.log(logPrefix, error);
                currentConsents = false;
            }
        }
        // Get all available consents
        const allConsents = zaraz.consent.getAll();
        // Check for should update
        // Every key name in allConsents should be in currentConsents, else we need to update
        let shouldUpdate = false;
        if (consentCookie) {
            for (const key in allConsents) {
                if (currentConsents[key] == undefined) {
                    shouldUpdate = true;
                    break;
                }
            }
        }
        // Or JSON is broken
        if (currentConsents === false) {
            shouldUpdate = true;
        }
        // Build config
        const configDict = {
            logPrefix: logPrefix,
            cookieName: cookieName,
            consentCookie: consentCookie,
            isEUCountry: isEUCountry,
            isUKCountry: isUKCountry,
            isCalfornia: isCalfornia,
            shouldConsent: shouldConsent,
            currentConsents: currentConsents,
            allConsents: allConsents,
            shouldUpdate: shouldUpdate,
        };

        console.log(logPrefix, configDict);

        // Pop up consent modal
        function popUpConsent() {
            console.log(logPrefix, "Consent needed");
            zaraz.consent.modal = true;
            zaraz.consent.setAllCheckboxes(true);
        }

        // Allow all and go
        function silentlyAllowAllAndGo() {
            console.log(logPrefix, "Consent not needed");
            zaraz.consent.setAll(true);
            zaraz.consent.sendQueuedEvents();
        }

        if (!consentCookie) {
            // If consent cookie is not set
            if (shouldConsent) {
                popUpConsent();
            } else {
                silentlyAllowAllAndGo();
            }
        } else {
            // If consent cookie is set
            if (shouldUpdate) {
                console.log(logPrefix, "Upstream consent config changed");
                if (shouldConsent) {
                    popUpConsent();
                } else {
                    silentlyAllowAllAndGo();
                }
            }
        }
    }

    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>
除非另有说明,此内容使用 CC BY-SA 4.0 许可。
最后更新于 2025-06-13 20:26:38
此处获取此页源码或编辑此页面。
提交: eeb7d648 环境: production Hugo: 0.147.9 主题: 3.30.0-modified 时间: 1751280154084274
本博客内容仅供参考,作者不对其准确性、完整性或适用性作出任何明示或暗示的保证。因使用、引用或解读本博客内容所引发的任何直接或间接后果,作者概不承担任何责任。
本博客可能包含第三方转载内容,相关版权归原作者所有。转载内容仅为分享信息之目的,不代表作者观点。如涉及侵权,请联系删除。
使用 Hugo 构建
主题 StackJimmy 设计