Home » Google » Top 3 Speed Tips for Sites using Google Analytics

by 13 comments

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.

  1. Host the Google-Analytics ga.js file locally
  2. Set Correct Cookie Domain
  3. Make Sure Google Analytics Loads

Default Google-Analytics Tracking Code

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "' type='text/javascript'%3E%3C/script%3E"));
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-2452345-5");

1. Hosting the Google ga.js file locally

Instead of calling the 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 file, then I compressed it online and saved to serve!

How to Do it: Serve External Javascript Files locally for Increased Speed

2. Set Correct Domain for Cookie

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 and so it was being sent needlessly with every request to every subdomain, when it only should be sent for requests to

Specify Google-Analytics Cookie Domain

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, ensuring that the cookie will not be sent needlessly for images and other static content.

var pageTracker = _gat._getTracker("UA-732153-7");

3. Make Sure Google Analytics Loads

Look at the source code for this page to see how I do it. Here is what is going on:

  1. Add the google javascript at the bottom of my source code to make sure the HTML and DOM is fully loaded before first calling the script for download.
  2. Check that the google-javascript is loaded by testing that the object called _gat exists, which is the internal object created.
  3. Initiate google-analytics

The Safer Google-Analytics Include Code

<script type="text/javascript">
if (typeof(_gat) == "object") {
  var pageTracker = _gat._getTracker("UA-732153-7");


Comments Welcome

AskApache is an FSF Contributing Member (with ThankGNU)

Information is freedom. Freedom is non-negotiable. So please feel free to modify, copy, republish, sell, or use anything on this site in any way at any time ;)

My Online Tools
Popular Articles

Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two.
-- Richard M. Stallman


It's very simple - you read the protocol and write the code. -Bill Joy

Except 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. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

+Askapache | |

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service | @Htaccess

↑ TOPMain