最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable Google Anayltics cookie from being sent to cookie less domain - Stack Overflow

programmeradmin0浏览0评论

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 and www.example?

Have already looked through some of the documentation with no luck

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. and www.example.?

Have already looked through some of the documentation with no luck

Share Improve this question asked Jul 12, 2010 at 22:44 Olly HicksOlly Hicks 1,1041 gold badge9 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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

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

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

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
    store.konoro
    video.konoro

You'll see that it gives you a code sample with .konoro. Change that to www.konoro and all cookies will have: .www.konoro (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'], 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'],
    ['_trackPageview']
); 

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

评论列表(0)

  1. 暂无评论