Google Analytics provides a lot of insight into your user base for free. Setting up the service is a breeze, but it can be confusing digging into some of the custom functionality. This article should get you started and show you some tips and tricks on hooking into some of the customized functionality in GA.
Navigate to the Google Analytics website. If you don’t already have a Google account, sign up for one.
Once you are logged in, click Admin in the upper right corner. In the Account Administration area, click on “New Account” and fill out the form
When you have completed setting up your account, you will be given the code to place on your site
Installing on your site
That’s it. For the basic functionality of Google Analytics (which is anything but basic), this is all you need to do. For those of you who need additional functionality such as:
- Tracking across domains/subdomains (shop.foo.com, blog.foo.com, foo-sister-site.com)
- Custom Variables
please read on.
Tracking Across Subdomains
Because you must specify the domain name for your website when you add it in GA (e.g. www.example.com or example.com), GA ties the cookie for your users to that domain; therefore, if you direct your users to shop.example.com and haven’t explicitly told GA to do so, it will see that as a new user. We can remedy this issue using the _setDomainName method:
It’s very important to note that the domain I set was ‘(DOT)example.com’, the leading dot will force the cookie to be valid across all subdomains of your domain.
The order in which you push items onto the _gaq stack is VERY important, please make sure you read the CAVEATs
Tracking Custom Variables
Custom variables are an extremely powerful way of tying useful data to your users and the pages of your site. Want to know what split tests users in Europe are seeing? How about the states in the US that have the most logged in users? Custom variables are the way to gather such insight. As a user of the free GA service, you will have the ability to create 10 custom variables so use them sparingly.
You can find these reports under: Standard Reporting > Audience > Custom > Custom Variables
Tracking Custom Events
Before we get to that and for the sake of your data, PLEASE READ:
The way bounce rate is calculated in GA is the percentage of users who only had one event fire for them during their visit to your website (_trackPageview, to be specific). If you call trackEvent without manually setting the last parameter to true, the event will be seen as an “interaction event”, and therefore will (possibly unintentionally) decrease your bounce rate; by setting it to true, we are instructing GA to not use the event as a factor in our bounce rate calculation.
About Custom Events
Custom Events are interactive and non-interactive events that GA doesn’t track automatically*, such as scrolling, time on site, button clicks that don’t cause the user to navigate away from the page, ajax calls, etc., etc.
We can use the _trackEvent method to cause GA to fire off the tracking pixel when desired and that is what I will provide a few examples of.
Time on Page
In this example I am tracking users who have been on a page for at least 15 seconds in a category called ‘Time’, I could have a number of these fire at different times, or instead use setInterval to track every 15 seconds (just be careful you turn it off after 3 or 4 so you aren’t blowing out your data limits).
jQuery AJAX Requests
Here I’m tracking all jQuery AJAX requests into an “AJAX” category, setting the action to the url that I requested from and giving it a label of the response code so I can see the success rates of urls; I opted not to send a value as I’m not really aggregating anything on the page into this category. The last value I pass (true) declares the event as non-interaction.
Given an HTML setup like the following:
<code><div class="product"> <img src="/products/socks.jpg" /> <select class="quantity"> <option>1</option> <option>2</option> <option>3</option> </select> <button class="addToCart">Socks</button> </div> </code>
Here I am setting up an interactive event of a user adding something to their cart, the event is going into the ‘Cart’ category, I’m giving it an action of ‘AddItem’, I’m setting the label to the item name and the value to the quantity the user added to their cart. I’m using the default value of non-interaction (false) because this is indeed an interaction.
Scrolled to 90%
<code><a href="http://www.example.com/foo" onclick="return track(this);">Example Foo</a></code>
There are a few solid methods for debugging that I’ve found, you can use any or all of them to track down problems:
- Centralize your calls to _gaq.push so you can have a debugging mode that logs your parameters to console
- Use Google Analytics debugging script “u/ga_debug.js”, rather than “/ga.js” (Note: don’t use this script in production as it will slow things down). This will give you so much useful data, including reasons why your data is not being tracked (i.e. GA was expecting a string and you used an integer)
- Watch the network tab of your browser’s console (Firebug in Firefox) and make sure _utm.gif is being requested every time you have an event fire, if you don’t see that gif in the network tab, your data is NOT being tracked
Happy to answer any questions you have in the comments