Event Tracking with Google Analytics
August 6, 2010
It’s been quite a while since the Google Analytics team introduced event tracking, but many people I talk to don’t understand it. In some cases, they don’t even know it’s there. Nevertheless, event tracking is one of the most useful tools for getting data into Google Analytics and it opens up analysis on many things we just couldn’t see before.
What’s Event Tracking?
Traditionally, web metrics were entirely based on page views. We look at what pages visitors see, and in what order they see them. But that’s not how the web works anymore… things are not just page-by-page. You can search a Google map and then move around or drag your location pins without reloading the page. You can post your Facebook status updates and see new tweets come in on Twitter without reloading. You can watch, pause and rewind videos, without reloading. JavaScript, which enables this interaction, is more important than ever to using the web.
Event tracking allows you to gather data when these things happen. Older techniques for tracking things like video plays involved forging URLs — using a snippet of code that would “pretend” that a page was loaded so that we could track the event. With proper event tracking, we no longer need to view things as URLs. We can track events as what they are — non-pageview events.
Getting Started
Here’s what you need to know to get started using events in Google Analytics:
First, Google has set up several fields for each event, for the purpose of categorizing them. Events are grouped by Category, Action, Label and Value. Let’s look at how they play together.

The above diagram shows four events that are grouped into two categories. The events are represented by the coloured boxes.
Let’s consider the first category of events, “Videos”. This event category will relate to actions that are taken on videos throughout the website. In other words, all events that take place with videos on the site, including videos played, paused, downloaded or shared, will be tracked in this one category.
The next level down is the Action. As the name suggests, actions are the specific thing that was done by the user to trigger this event. In the case of these videos, we have an action for “Play” and an action for “Pause”. We can find out pure metrics for actions, such as how many videos were played by a specific segment within a time frame, or we can use them to group specific events and drill down by event type.
On the next level is the label. For our videos, we’re using the label to indicate which video an action was performed on. For instance, the first event in the diagram is a “Play” action labeled “Training Video”. Now I know specifically which video was played. The next action has the same label, but is of the “Pause” type — meaning that the training video was paused, triggering this event.
The last number is “value”. Value is an integer field which, in the case of my videos, is left blank. Values are used to provide specific counts that are useful to the event. For instance, if the event is the removal of an item from a shopping cart, I may use the “Value” field to indicate the dollar amount of the item removed. Then, I can know the total value of all products that were removed from carts prior to purchases.
Our next event category does make use of the values. This category is called “Contact Form”, and is used on a website for events related to that form. Our first event’s action is a form error, which means a failure to send the form because something went wrong. The label is used to indicate the type of form error, in this case, an “Invalid Email”. We’ve also included a value, “3″. That value is, for this event, the number of errors that the specific visitor has encountered so far. Thus, we know that this invalid email was the third error encountered by the user.
The last event is also for the contact form, in this case tracking a successful submission of the form (the Action), and the type of request that was sent (the Label).
Now, you don’t necessarily need to use Categories, Actions, Labels and Values in the exact way that I’ve laid out. What matters is that you formulate a method for tracking events and stick to it.
The Code: Tracking an Event
To actually implement the tracking on your site, you’ll need to add a little bit of JavaScript code. The code I present here uses the asynchronous method of tracking. This is a new method of tracking that Google recently deployed as its standard tracking method. You don’t have to use the asynch. method to use event tracking, but it may be a good time to upgrade anyways.
You can tell if you have the asynch method by looking at the Google Analytics tracking code on your site. If it begins with the following:
var _gaq = _gaq || [];Then you’re ready to go. If not, just log into your Google Analytics account, re-copy your tracking code and update your site.
Now, you just need to add tracking codes to the events on your site. Suppose, for example, that you have an image gallery on your site, and that clicking on an image enlarges it. Your code for a particular image may look something like this:
<img src="foo.jpg" alt="" />Let’s say you want to track how many people click on this image. You’ll want to add the tracking code to the onclick event for that image. Onclick is a JavaScript event that allows you to execute code when someone clicks on something. To do so, add this code.
<img src="foo.jpg" alt="" onclick="_gaq.push(['_trackEvent', 'Images', 'Enlarged', 'foo.jpg']);" />Let’s examine the code for that event more closely. The code to track an event in Google Analytics is as follows:
_gaq.push(['_trackEvent', 'CATEGORY', 'ACTION', 'LABEL', 'VALUE']);For our event, we coded it as follows:
_gaq.push(['_trackEvent', 'Images', 'Enlarged', 'foo.jpg']);The first field in the _gaq.push() function is called ‘_trackEvent’. What it does is obvious — it tells Google Analytics that we want to track an event. The next field is the category. We set it to “Images”, because that’s what we’re dealing with. The action is the next field, which I set to “Enlarged” to tell us that someone clicked on an image to enlarge it. The last field is for the label, which I set to the filename of the image that is being enlarged, so that I can track how many times each image is enlarged.
Notice how I didn’t add a fifth field for the value. I’m not tracking a value here at all, so I can omit that bit. In fact, you can also omit labels if you don’t need them.
Let’s look at another example: tracking external links. If someone clicks on a link that takes them outside of your site, you don’t have access to the data for the page they land on, so you wouldn’t know it happened. With events, you can trigger a bit of JavaScript to occur right before they leave your site. For example, let’s say I want to track how many people click on the link to my Twitter account. All I need to do is add an event:
<a href="http://twitter.com/cailean" onclick="_gaq.push(['_trackEvent', 'External Links', 'Click', 'Twitter']);">Follow me on Twitter</a>Now, I can track who clicked on the link to my Twitter profile, and how many times that link was clicked.
Reporting Event Data
Once you have your tracking set up, you can view events as they occur. Note that you don’t need to set up events in Google Analytics before you track the codes. Just like you don’t need to add every URL into Google Analytics manually, GA will track event data as it comes in automatically.
You can find the event tracking under Content > Event Tracking. Here, you can view events by Category, Action or Label… or you can drill-down into the heirarchy to see specific subsets of the data (such as all Clicks for a particular link). You can also segment this data just as you can with normal page view data, so you can see which videos were played by people who bought products, or whether or not Canadians had a hard time filling out your forms.

Taking Action on the Data
Event tracking is a great tool to give you some more data, but as always, the value of web analytics is always in the insights that data brings. This is where a web analyst can dive into the data and come up with actionable things you can do to meet and exceed the business goals of your site.
In the meantime, I’ll talk a little more about improving and organizing your event tracking in some upcoming posts. Be sure you don’t miss them by subscribing to Work, Web, Play.






I'm Colin and I'm a Senior Analyst at Napkyn Inc, leading a team of business analysts who support the decision-makers in major online retailers, software companies and sales organizations.