Question

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"

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top