Top 3 Speed Tips for Sites using Google Analytics
« Serve External Javascript Files locally for Increased SpeedSEO Secrets of AskApache Part 2 »
I’ve written alot about how I speed up websites, but there is much more to cover. Here are the 3 ways I speed up my site by playing with the Google Analytics Tracking Code.
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-2452345-5");
pageTracker._trackPageview();
</script>
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 ga.js with a javascript file already on your site like I do, you reduce HTTP Requests, reduce browser cache lookups, etc.
For this site, I added the ga.js code to my http://z.askapache.com/z/j/apache-999.js file, then I compressed it online and saved to serve!
How to Do it: Serve External Javascript Files locally for Increased Speed
For those websites who employ multiple sub-domains to serve static content, and you are using the default google-analytics javascript code for website statistics, you may be able to get some speed by forcing the google analytics cookie to only be sent for your non-static subdomain.
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 www.askapache.com.
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:
<script type="text/javascript">
if (typeof(_gat) == "object") {
var pageTracker = _gat._getTracker("UA-732153-7");
pageTracker._setDomainName("www.askapache.com");
pageTracker._initData();
pageTracker._trackPageview();
}
</script>
« Serve External Javascript Files locally for Increased Speed
SEO Secrets of AskApache Part 2 »
Tags: cookie, Google, Google Analytics, Javascript
Please consider donating to support active development of the free software and articles here.![]()
The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect. Tim Berners-Lee
Isn’t using defer="defer" in the script tag the same as putting it on the bottom of the page?
Not a bad concept but considering 6 out of ten websites out ther uses GA and given that GA script already sets the appropriate caching wouldn’t a user’s browser uses the cached version of GA script by default and as a result that would already have reduced http request and DNS lookup anyways when someone goes to your site?
And if you were to combine the GA script with your own script in one file (considering if you have do that manually), when Google updates the script, that means you have to update your own script as well. It might be a maintenance nightmare if you have to combine and minify GA script every now and then.
@Marin:
Setting _setDomainName("none") sets all cookies to www.example.com
Great thought about move the script locally. Some pages I visit just waiting for the response from Google site. Although it is just sometimes, but move it locally is great.
Nice tips here. I got some questions remarks:
#1 apparently it’s not strictly forbidden (but not recommanded) to host our own version of ga.js cf: http://www.google.com/support/analytics/bin/answer.py?hl=en&answer=55466
#2 I tried to set the domainName to my domain (“www.domain.com”) but I received 2 sets of cookies:
_umtz with domain:”.www.domain.com”
AND _umtz with domain: “.domain.com”
in that order (+ other _umt..)
Can anyone confirm? is this a bug or something else? on FF3 latest
#3 I would, on top of this, style the DOM container of the ad so that it will have the correct width+height and the whole DOM will not be rerendered when the ad is loaded
Moving external javascript locally is a great idea – thanks for the tip. You only mention Analytics here, do you know if moving or modifying the JS for Adsense violates the terns and conditions?
Interesting idea. Do you have any stats on how much faster the page loads are by doing this?
Excellent tips, though what happens if Google updates the Analytics code? Is there an easy, low-overhead way of ensuring you’re always hosting the most up-to-date code?
It's very simple - you read the protocol and write the code. -Bill Joy
HTML | DCMI | GRDDL | XOXO | XDMP | XFN | DOM | XML | XHTML 1.1 Strict | CSS 2.1 | W3C | TLDP | WAI | DISA | ICSI | GIAC | SANS RR | GHOST | DEFCON | NIST | DHS CYBER | NIST | .:: Phrack Magazine ::.
↑ TOPExcept where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. HTTPD based on NCSA HTTPd
this is very important tip for optimization, thanks