How can I tell Google Analytics to not use cookies for my sub domain?

5,569

Solution 1

You can not do this. Any cookie issue for domain example.com is valid for all subdomains of example.com.

If you wish to have static.example.com be a cookie free domain, you should serve up the main site at www.example.com (you can provide a 301 redirect from example.com to www.example.com).

As static.example.com is not a subdomain of www.example.com (rather a sibling), it will not share its cookies.

This doesn't really have anything to do with Google Analytics, this is simply how the WWW works. Even if you could tell GA to not send the cookies to static.example.com that wouldn't help your other users who would not have access to this information.

Solution 2

Like this (with the new GA code)

_gaq.push(
    ['_setAccount', 'UA-xxxxx-x'],
    ['_setDomainName', 'www.example.com'],
    ['_trackPageview']
);

It still won't block other 3rd party apps you may be using from setting cookies to your static subdomain but at least you are getting rid of the GA cookies which are quite large.

Solution 3

Like Kris said, any cookies set on example.com will be available on static.example.com and other subdomains. If you want to keep the site without the www. then I'd suggest using a different domain name (for example, Stack Overflow uses http://sstatic.net).

If you have multiple domains already, for example yourbusiness.net redirecting to yourbusiness.com then you can simply use the .net one to host all the static content.

Solution 4

The correct(est) answer is

_gaq.push(['_setDomainName', 'none']);  

Reference.

In my tests, explicitly setting a domain WITHOUT a sub-domain (e.g. example.com instead of www.example.com) still resulting in cookies being sent to sub.example.com. Setting the domain name to 'none' solved this.

Share:
5,569
Magnus
Author by

Magnus

My blog http://InsomniacGeek.com

Updated on September 17, 2022

Comments

  • Magnus
    Magnus over 1 year

    I have my web site at example.com, and i have also setup a sub domain for serving static content at static.example.com.
    On my web pages I'm using Google Analytics, so it will attach its cookies to all page request, even on static.example.com.

    I went into the GA settings and added a filter to exclude static.example.com, but I still see cookies (with the __utma.. name) on all web requests to my static domain.

    How can i prohibit GA to use cookies on my sub domain?

    • John Conde
      John Conde over 13 years
      Why do you not want the cookies to be there? Does it matter as long as the content isn't being tracked?
    • Thomas
      Thomas over 13 years
      Google's speed test gives you a ++ if you are serving static content from a cookieless domain. Google gives, google takes.
    • John Conde
      John Conde over 13 years
      Interesting....
    • Talvi Watia
      Talvi Watia over 13 years
      @thomas +1 for seeing the reason this question was asked.
    • Lèse majesté
      Lèse majesté over 13 years
      Why does it matter what Google's speed test gives you? Isn't that just a performance testing tool for webmasters? Unless you want to delude yourself with a non-existent performance improvement, there's no point in removing cookies just to game a speed test that is only meant for you.
    • Magnus
      Magnus over 13 years
      @Lèse. You have to ask yourself; why does Google Webmasters have a utility to measure your site's response time? Why does Google release a browser plugin to measure your site's response time and this utility give you plus points if you put your static content on a cookieless domain? Do you care that Google have discussed giving plus points for openly about slow response time hurting your ranking? Is it important to you to have a high Google ranking for your web sites? To me, the last question is of highest importance, and I I'm putting my static content on a cookieless domain.
    • Magnus
      Magnus over 13 years
      @Thomas. You've got it.
    • Lèse majesté
      Lèse majesté over 13 years
      Google offers a lot of useful tools/services that have nothing to do with SEO. I would think that the utility of speed optimization is enough reason to release a speed test. As a matter of fact, google employees have stated that you shouldn't worry about speed affecting SEO: mattcutts.com/blog/site-speed As long as your site performance is decent/reasonable, it's unlikely to be a factor. Optimize performance for UX, not for SEO.
  • Magnus
    Magnus over 13 years
    Thanks, I was afraid of that. I will be using a dedicated domain.net for my static content.
  • user3055004
    user3055004 over 10 years
    You don't need another domain. donaldthe just suggested a valid method.
  • user3055004
    user3055004 over 10 years
    StackOverflow uses sstatic.net because it delivers the static content to multiple websites. On a single website there is no need in getting a new domain. I'm using the same setup (with subdomain) and never had problems with cookies. They are not sent to subdomains.
  • DisgruntledGoat
    DisgruntledGoat over 10 years
    @machineaddict not true: developer.yahoo.com/performance/rules.html#cookie_free you probably set cookies on the www subdomain and not the root domain.
  • ArendE
    ArendE about 10 years
    Thanks! This variable showed me the light. The current code is _gaq.push(['_setDomainName', 'yoursub.domain.com']);, put it after the _gaq.push(['_setAccount', 'UA-12345-1']); directive in the js code. See also developers.google.com/analytics/devguides/collection/gajs/…
  • BGBRUNO
    BGBRUNO over 4 years
    it helped to "remove that dot" but not solve issue with subdomains - cookies are everywhere still
  • BGBRUNO
    BGBRUNO over 4 years
    gtag('config', 'GA_MEASUREMENT_ID', { 'cookie_domain': 'none' }); -- developers.google.com/analytics/devguides/collection/gtagjs/‌​…