- Host the Google-Analytics ga.js file locally
- Set Correct Cookie Domain
- Make Sure Google Analytics Loads
Instead of calling the http://www.google-analytics.com/ga.js file in your site's html, you can instead save the
ga.js source code and serve it from on your own server! This reduces DNS lookups, gives you control over caching, and if you combine the
For this site, I added the ga.js code to my https://www.askapache.com/s/s.askapache.net/z/j/apache-999.js file, then I compressed it online and saved to serve!
I just noticed this on my site the other day while watching the wire:
The Google Analytics cookie was being set for the domain
.askapache.com and so it was being sent needlessly with every request to every subdomain, when it only should be sent for requests to
So I googled for an answer for awhile and finally found the solution in the ga.js code itself. There is a command called
_setDomainName that lets you set the domain for the cookie. The following code sets the GA cookie for
.www.askapache.com, ensuring that the cookie will not be sent needlessly for images and other static content.
var pageTracker = _gat._getTracker("UA-732153-7"); pageTracker._setDomainName("www.askapache.com"); pageTracker._trackPageview();
Look at the source code for this page to see how I do it. Here is what is going on:
- Initiate google-analytics