Google Analytics 使我创建无 Cookie 域的努力化为泡影

发布于 2024-12-11 02:45:27 字数 774 浏览 1 评论 0原文

我设置子域 static.map.ninux.org 来加载域 map.ninux.org 的静态文件。目标是拥有一个无 cookie 的域。

我查了一下,发现map.ninux.org上的谷歌分析创建了域名为“.map.ninux.org”的cookie,由于某种原因,该cookie对static.map.ninux.org也有效。

我该如何解决这个问题?

PS:这段代码正确吗?

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-26436344-1']);
_gaq.push(['_trackPageview']);
_gaq.push(['_setDomainName', 'map.ninux.org']);
(function () {
    var ga = document.createElement('script');
    ga.type = 'text/javascript';
    ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(ga, s);
})();

这样我仍然可以得到主机“.map.ninux.org”的cookie

I setup the subdomain static.map.ninux.org to load static files for the domain map.ninux.org. The goal was to have a cookieless domain.

I checked and I found out that google analytics on map.ninux.org creates cookies with the domain ".map.ninux.org" that for some reason are valid also for static.map.ninux.org.

How can I solve this issue?

PS: is this code correct?

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-26436344-1']);
_gaq.push(['_trackPageview']);
_gaq.push(['_setDomainName', 'map.ninux.org']);
(function () {
    var ga = document.createElement('script');
    ga.type = 'text/javascript';
    ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(ga, s);
})();

With this I still get the cookies with host ".map.ninux.org"

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

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

发布评论

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

评论(3

舂唻埖巳落 2024-12-18 02:45:27

编辑:

在为map.ninux.org设置cookie时,不能释放static.map.ninux.org cookie,因为static.map.ninux.org将从map.ninux.org继承所有cookie .org(并且您没有任何机会更改此行为)。

您可能想将 cookiefree 域名更改为 static-map.ninux.org。在这种情况下,_setDomainName 的默认设置“auto”将正常工作。

Edit:

You can't make static.map.ninux.org cookie free when setting a cookie for map.ninux.org, because static.map.ninux.org will inherit all cookies from map.ninux.org (and you don't have any chance to change this behaviour).

You may want to change the cookiefree domain name to static-map.ninux.org instead. In this case the default setting of _setDomainName which is 'auto' will work correct.

晚风撩人 2024-12-18 02:45:27

谷歌跟踪器参数的排序会破坏游戏。将 setDomainName 参数放在 _trackPageview 之前即可工作。

var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXXXX-X'],
    ['_setDomainName', 'www.example.com'],
    ['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

the ordering of google tracker parameters plays the spoilsport. put the setDomainName parameter before _trackPageview and it will work.

var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXXXX-X'],
    ['_setDomainName', 'www.example.com'],
    ['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
三五鸿雁 2024-12-18 02:45:27

促进无 cookie 域的另一种方法是将您的流量推送到 www.map.ninux.org 而不是 map.ninux.org - 这样,static.map.ninux.org 将保持无 cookie。

然后,与之前的响应一样,您可以使用以下指令

['_setDomainName', 'www.map.ninux.org']

或者对于 Google Analytics 的另一种风格,如下所示:

<script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    ga('create', 'UA-XXXX-XX', 'ninux.org');
    ga('send', 'pageview'); 
</script>

您可以相应地手动修改“ga create”部分:

ga('create', 'UA-XXXX-XX', 'www.map.ninux.org')

这将导致 Google Analytics 在 . www.map.ninux.org 域,而不是 .ninux.org 域。

Another way of facilitating cookieless domains is to push your traffic onto www.map.ninux.org rather than map.ninux.org - that way, static.map.ninux.org will remain cookieless.

Then, as in the previous response, you can use the following directive

['_setDomainName', 'www.map.ninux.org']

Or for another flavour of Google Analytics that looks like this:

<script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    ga('create', 'UA-XXXX-XX', 'ninux.org');
    ga('send', 'pageview'); 
</script>

You can manually modify the "ga create" portion accordingly:

ga('create', 'UA-XXXX-XX', 'www.map.ninux.org')

That will cause Google Analytics to create their cookies on the .www.map.ninux.org domain, rather than on .ninux.org domain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文