禁止将 Google Analytics cookie 发送到无 cookie 的域
为了优化我的网站,我为图像、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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能将 Cookie 发送到
www.example.com
和example.com
,但不发送到othersubdomain.example.com
。理论上,您可以将 Cookie 发送到
example.com
,但不能发送到subdomain.example.com
,但它在 IE 中不起作用。这就是为什么当您计划使用具有自己的安全上下文的子域时,不应使用无 www
example.com
地址。无论如何,最好只使用www.example.com
和example.com
之一作为“正确”URL。因此,为example.com
设置重定向到www.example.com
,除非您刻意设置domain=.example,否则 Cookie 不会被共享.com
他们。You can't have a cookie that gets sent to
www.example.com
andexample.com
but notothersubdomain.example.com
.You can in theory have a cookie that gets sent to
example.com
but notsubdomain.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 ofwww.example.com
andexample.com
as the ‘proper’ URL anyway. So set up a redirect forexample.com
to go towww.example.com
, and cookies won't be shared unless you deliberately setdomain=.example.com
on them.如果您在使用 Google Analytics 为每个域(包括子域)创建 cookie 时遇到问题。这是我在其他地方发布的关于该主题的解决方案。
天哪,我想我已经找到了谷歌提供的损坏代码的临时解决方案。
如果您进入 GA 并获取非异步代码并选择:
您将看到它为您提供了带有
.konoro.org
的代码示例。将其更改为www.konoro.org
,所有 cookie 都将具有:.www.konoro.org
(前面有一个点)。现在,如果您回到异步代码,除了异步之外,只有一处区别。您需要将
['_setDomainName', 'www.konoro.org']
作为第二个选项,而不是放在['_trackPageview']
之后。出于某种原因,作为第二个选项,它工作得很好。
我的最终代码是:
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:
You'll see that it gives you a code sample with
.konoro.org
. Change that towww.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: