This is an old revision of the document!
Google Analytics is the best free traffic analysis software available. It can handle pretty much any sized website (at least within reason), and has a number of fairly advanced capabilities.
Basic Install is pretty well covered by Google's Docs. Be sure to use the new
ga.js code, not the old
urchin.js code. Make sure that the tracking code appears once, and only once, on all of your pages. Preferably at the very bottom, right above the closing
Once you get that done, login and start looking at your reports. Note that (annoyingly) any configuration changes you make to your reports (e.g. apply a new filter, change your default homepage filename, etc.) only affect data from that day onward. You can't go back and reprocess old data.
Google Analytics has five tracking areas: Dashboard, Visitors, Traffic Sources, Content and Goals.
In most reports, Google Analytics tracks pretty much everything via Visits, which is one person being active on the site. They can be active as long as they want, but as soon as they are inactive for 30min or more, their visit ends. If they come back, they are counted as a new visit. They also offer Visitors; these are unique across the entire time period you're looking at. So if somebody visited on Monday, and then visited again on Wednesday, they'd have two visits but be one visitor.
Pageviews counts the number of individual pages that has been viewed; a reload of a page means that page is counted again. Unique Pageviews filters out reloads (so if you reload a page ten times, that's ten pageviews but one unique pageview). Unique Pageviews only count across visits, meaning if someone views a page, waits 30 minutes (and thus their 'visit' ends), and then views the same page again, that is two unique pageviews (one from the first visit and one from the second visit).
Each GA profile gives you up to four Goals that you can track. You don't have to insert any special code into your pages for these; GA matches your goals based on the URL of the page. In my opinion, the best thing to do is to write regular expression matches against the URL; this allows you a lot of flexibility you wouldn't have otherwise.
For example, WHMCS 3.4.1 has a checkout process that can end on either
order.php?step=6. So, you'd make your Goal URL
(order.php\?step\=6)|(creditcard\.php\?invoiceid\=[\d]*), which would match either page. If you make your Goal URL a regular expression, you need to make your goal steps the same, e.g.
order\.php$, which matches any URL that ends in order.php.
Filters are one of the most powerful features of GA. They let you include or exclude certain pages/subdirectories, and do search and replace operations on URLs (or other variables).
The most important (and most annoying) thing about filters is that they run when the original data is collected, and ONLY when the original data is collected. If you make a mistake, or you just want to go retroactively apply a new filter to your data, you can't.
GA lets you set a 'default page' in the profile settings (e.g.
index.php). It then converts any root level (
/) requests to the equivalent default page URL (e.g.
/index.php), and displays those in its reports. The problem is that sometimes you might have those files redirected, or they otherwise can't be accessed at
/index.php. You also can't set more than one page to be your 'default page'.
A better solution is to setup a filter, which renames
home.php, and whatever else you want to
/. Then all of your URLS are listed in GA as
/, which is more useful. To do this, setup a 'Search & Replace' filter on the 'Request URI'. Search for
index\.php and replace with
You can use the GA campaign feature to track marketing campaigns, including email.
If a user loads your page out of the Google Search cache, your Google Analytics code WILL run, and you'll record a hit for that page. Generally, this is probably OK… I think a cached hit is still a 'hit' on your site. If you don't want this to happen, you can setup a filter to filter out the cached traffic.
GA has a 'user defined' segment, and you can pass your own variable into it and then segment your reports on that variable. To do this, call
_setVar() in your code:
// any user with $isLoggedIn set to true will have the user defined GA variable // set to 'logged-in' pageTracker._setVar(<?= $isLoggedIn ? "'logged-in'": "'logged-out'"; ?>); pageTracker._trackPageview();
There are a few things to keep in mind:
_setVar()again, it will overwrite the existing variable. There is a way to set more than one, but it is a bit cumbersome.
_setVar('logged-in');) on his visit today, and he comes back tomorrow, tomorrow's visit will still be tagged with 'logged-in', even though you didn't explicitly set it again. As before, this can be good or bad, depending on what you want to do.
_setVar()on every page load, it will break the bounce rate calculation in GA. This is because
_setVar()records a secret second pageview every time it is called. I wrote up some code to fix this 'zero bounce rate' problem, so
_setVar()is only called once and then the value is read out of the cookie.
If you want, you can record data for the same hit in more than one profile (using codes
UA-YYYYYY-1 in this example):
If you want, you can inject a pageview into the Google Analytics stats. This is done by calling
_trackPageview() again, but with an argument. Generally, you'll want to do this in an
onclick, or similar:
<a href="#" onclick="pageTracker._trackPageview('/pages/foobar');">click to track the foobar page</a>
Needless to say, the main google code should have loaded before you try to call this function, but if you're putting it in an
onclick, odds are that it has.
GA doesn't seem to like 301/302 redirects when validating tracking codes. It's a fairly common technique to redirect
www.startupcto.com (or vice versa). If you tell GA that your tracking code is on
startupcto.com, and that redirects to
www.startupcto.com, GA will never validate that you have properly installed your tracking code. You must set your Website URL to the non-redirected domain, e.g.
There are numerous ways to track sub-domains in Google Analytics, but I think this one is the best. The basic idea is using one profile for all sub-domains, and then creatign sub-profiles (with filters applied) for each sub-domain.
The full GA API is available.