I have a domain where I exclusively serve to www.example.com .

A call to example.com just gives a redirect to www.example.com directly from apache2.

Now I want to serve my pictures from media.example.com cookieless. In theory that should be possible, but my chorme plugin says there are _ga and _gat cookies that always come back whenever I open www.example.com. These two cookies are set for example.com and not www.example.com. How can I force google analytics to use the www. domain instead?

  • I think you'll end up having to buy a new domain name. Feb 20, 2015 at 3:44
  • Why do I have to do that?
    – JasonTS
    Feb 20, 2015 at 3:51
  • Because that is the way Google Analytics has been implemented. Feb 20, 2015 at 8:19

2 Answers 2


Google Analytics embeds the cookies automatically at the highest level for tracking purposes. What this means is that if the cookie is set for example.com it will also be accessible from static.example.com www.example.com something.else.example.com etc.

The way most sites combat this to have a cookie free domain for static content is to register a new domain name specifically for cookie free authentication.

As a side note this is how the entire Stack Exchange network has a cookie free domain. Static content is hosted on http://cdn.sstatic.net which never has any cookies anywhere within the sstatic.net domain and that provides the cookie free experience. In fact Stack Exchange actually provides the reason for it on their explanatory page...

When the browser makes a request for a static image and sends cookies together with the request, the server doesn't have any use for those cookies. So they only create network traffic for no good reason. You should make sure static components are requested with cookie-free requests. Create a subdomain and host all your static components there. If your domain is www.example.org, you can host your static components on static.example.org. However, if you've already set cookies on the top-level domain example.org as opposed to www.example.org, then all the requests to static.example.org will include those cookies. In this case, you can buy a whole new domain, host your static components there, and keep this domain cookie-free. Yahoo! uses yimg.com, YouTube uses ytimg.com, Amazon uses images-amazon.com and so on. Another benefit of hosting static components on a cookie-free domain is that some proxies might refuse to cache the components that are requested with cookies. On a related note, if you wonder if you should use example.org or www.example.org for your home page, consider the cookie impact. Omitting www leaves you no choice but to write cookies to *.example.org, so for performance reasons it's best to use the www subdomain and write the cookies to that subdomain. - @Stack Exchange


Google Analytics allows you to set the cookie domain. By default it uses the highest domain name possible (example.com), but you can have it use your subdomain (www.example.com) by passing that as a parameter when you initialize your tracker. The documentation for it: https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id

Example code for trackers that set the domain name:

ga('create', 'UA-XXXXX-Y', {
  'cookieDomain': 'www.example.com',

As the documentation states, the most common cookie field to set is cookieDomain, as such, the create command accepts the cookieDomain field as an optional third parameter for convenience:

ga('create', 'UA-XXXXX-Y', 'www.example.com');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.