Mobile app version of vmapp.org
Login or Join
Ogunnowo487

: Cookieless subdomain with Google analytics? 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.

@Ogunnowo487

Posted in: #Debian #GoogleAnalytics

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

A call to example.com just gives a redirect to 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 example.com. These two cookies are set for example.com and not example.com. How can I force google analytics to use the domain instead?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Ogunnowo487

2 Comments

Sorted by latest first Latest Oldest Best

 

@Heady270

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: 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');

10% popularity Vote Up Vote Down


 

@Si4351233

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 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 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 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 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 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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme