如何从JavaScript正确控制Google Analytics Cookie?

发布于 2025-01-22 13:06:31 字数 502 浏览 4 评论 0原文

目前,我已经设置了cookie提示到覆盖document.cookie.cookie set方法,以便它存储非要求的cookie并等待用户输入(用户允许在网页中使用的cookie)决定其他饼干。

代码看起来像这样:

const originalSetter = document.__lookupSetter__('cookie');

function newSetter(cookie) {
    if (isRequired(cookie)) {
        originalSetter(cookie);
    } 
    else {
        //store the cookie and decide later.
    }
}
document.__defineSetter__('cookie', newSetter);

由于Google Analytics(至少在我的页面中)不是必需的cookie,这是正确的方法还是有其他方法可以做到这一点?

Currently I have set up the cookie prompt to override document.cookie set method, so that it stores non-required cookies and waits for user input (which cookies the user allows to use in the web page) to decide about other cookies.

Code looks something like this:

const originalSetter = document.__lookupSetter__('cookie');

function newSetter(cookie) {
    if (isRequired(cookie)) {
        originalSetter(cookie);
    } 
    else {
        //store the cookie and decide later.
    }
}
document.__defineSetter__('cookie', newSetter);

Since the google analytics cookie is not a required one (at least in my page), is this the correct approach or is there another way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文