禁止将 Google Analytics cookie 发送到无 cookie 的域

发布于 2024-09-10 06:20:44 字数 145 浏览 8 评论 0原文

为了优化我的网站,我为图像、CSS 和 javascript 等内容设置了一个静态子域。如何阻止 google 分析跟踪 cookie 发送到我的静态子域,但仍然发送到 example.com 和 www.example.com?

已经浏览了一些文档但没有运气

To optimize my site i have set up a static subdomain for content such as images, css and javascript. How do i stop the google analytics tracking cookie from being sent to my static subdomain, but still to both example.com and www.example.com?

Have already looked through some of the documentation with no luck

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蹲墙角沉默 2024-09-17 06:20:44

您不能将 Cookie 发送到 www.example.comexample.com,但不发送到 othersubdomain.example.com

理论上,您可以将 Cookie 发送到 example.com,但不能发送到 subdomain.example.com,但它在 IE 中不起作用。

这就是为什么当您计划使用具有自己的安全上下文的子域时,不应使用无 www example.com 地址。无论如何,最好只使用 www.example.comexample.com 之一作为“正确”URL。因此,为 example.com 设置重定向到 www.example.com,除非您刻意设置 domain=.example,否则 Cookie 不会被共享.com 他们。

You can't have a cookie that gets sent to www.example.com and example.com but not othersubdomain.example.com.

You can in theory have a cookie that gets sent to example.com but not subdomain.example.com, but it doesn't work in IE.

This is why you should not use a no-www example.com address when you are planning on using subdomains with their own security context. It's best to have only one of www.example.com and example.com as the ‘proper’ URL anyway. So set up a redirect for example.com to go to www.example.com, and cookies won't be shared unless you deliberately set domain=.example.com on them.

笑咖 2024-09-17 06:20:44

如果您在使用 Google Analytics 为每个域(包括子域)创建 cookie 时遇到问题。这是我在其他地方发布的关于该主题的解决方案。


天哪,我想我已经找到了谷歌提供的损坏代码的临时解决方案。

如果您进入 GA 并获取非异步代码并选择:

One domain with multiple subdomains
Examples:   apps.konoro.org
    store.konoro.org
    video.konoro.org

您将看到它为您提供了带有 .konoro.org 的代码示例。将其更改为 www.konoro.org,所有 cookie 都将具有:.www.konoro.org(前面有一个点)。

现在,如果您回到异步代码,除了异步之外,只有一处区别。您需要将 ['_setDomainName', 'www.konoro.org'] 作为第二个选项,而不是放在 ['_trackPageview'] 之后。

出于某种原因,作为第二个选项,它工作得很好。

我的最终代码是:

var _gaq = _gaq || [];
_gaq.push(
    ['_setAccount', 'UA-XXXXXXX-1'],
    ['_setDomainName', 'www.konoro.org'],
    ['_trackPageview']
); 

(function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
})();

If you're having issues with Google Analytics creating a cookie for every domain, including your subdomain ones. Here's my solution that I've posted elsewhere on the subject.


By golly I think I've found a temporary solution to the broken code supplied by Google.

If you go into GA and get the non-async code and choose:

One domain with multiple subdomains
Examples:   apps.konoro.org
    store.konoro.org
    video.konoro.org

You'll see that it gives you a code sample with .konoro.org. Change that to www.konoro.org and all cookies will have: .www.konoro.org (with a dot in front of them).

Now if you go back to your Async code, there's only one difference apart from being Async. You need to put ['_setDomainName', 'www.konoro.org'], as the second option rather than after ['_trackPageview'].

For some reason, with this being the second option, it works fine.

My final code is:

var _gaq = _gaq || [];
_gaq.push(
    ['_setAccount', 'UA-XXXXXXX-1'],
    ['_setDomainName', 'www.konoro.org'],
    ['_trackPageview']
); 

(function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
})();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文