开发者

Disable Google Anayltics cookie from being sent to cookie less domain

开发者 https://www.devze.com 2023-01-07 02:13 出处:网络
To optimize my site i have set up a static subdomain for content such as images, css and javascript. How d开发者_如何学Pythono i stop the google analytics tracking cookie from being sent to my static

To optimize my site i have set up a static subdomain for content such as images, css and javascript. How d开发者_如何学Pythono 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


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.


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);
})();
0

精彩评论

暂无评论...
验证码 换一张
取 消