As I mentioned in my last post, there are plenty of other kinds of tracking that can be done beyond the basics with Google Analytics. In that post I covered in detail how to do eCommerce and Campaign tracking. In this post I’ll be going over a couple more areas of tracking that require customized tracking codes: Event Tracking and Custom Variables.
If you use videos, flash, or other types of interactive, rich media experiences on your site, you will need to use Event Tracking in order to gain the most insight into how well these elements are performing. Event tracking can also be valuable in terms of tracking outbound links on your site.
Setting-Up Event Tracking (Source: GA Event Tracking Guide)
Before you can view event tracking results in your reports, you must set it up on your site following these steps.
- Set up tracking on your site. Make sure you have set up tracking for your website. For information on setting this up, see Tracking Sites.
- Call the
_trackEvent()method in the source code of a page object, widget, or video.
The specification for the
_trackEvent()method is:_trackEvent(category, action, opt_label, opt_value, opt_noninteraction)
- category (required) – The name you supply for the group of objects you want to track.
- action (required) – A string that is uniquely paired with each category, and commonly used to define the type of user interaction for the web object.
- label (optional) – An optional string to provide additional dimensions to the event data.
- value (optional) 0 An integer that you can use to provide numerical data about the user event.
- non-interaction (optional) – A boolean that when set to
true, indicates that the event hit will not be used in bounce-rate calculation.
- View the reports. Once event tracking has been set up and working on your site for a day, go to the Content section of the reports and view Event Tracking.
Understanding Event Tracking
Setting up event tracking is generally fairly simple, however there are a number of elements you need to understand. As seen in the above example, there are several components within each event to be tracked (category, action, label, etc.), and each one maps directly to elements in the Google Analytics reports.
Here are some examples of what this might look like in a real scenario.
Tracking a video:
- <a href=”#” onClick=”_gaq.push([‘_trackEvent’, ‘Videos’, ‘Play’, ‘Baby’s First Birthday’]);”>Play</a>
Here you can see the category=Videos, the action=Play, the label=Baby’s First Birthday, and it’s embedded in the anchor tag around a text link. This same code could be embedded into the ActionScript of a Flash file and be used to record the play, pause and stop, something like this:
- _gaq.push([‘_trackEvent’, ‘Videos’, ‘Play’, ‘Gone With the Wind’]);
- _gaq.push([‘_trackEvent’, ‘Videos’, ‘Pause’, ‘Gone With the Wind’]);
- _gaq.push([‘_trackEvent’, ‘Videos’, ‘Stop’, ‘Gone With the Wind’]);
Tracking Links to External Sites
Another use for custom event tracking could be to track clicks to external sites, like social media links, on your site. For example if you have a link to share content on a page to Twitter or Facebook, your code could look like this:
- <a href=”#” onClick=”_gaq.push([‘_trackEvent’, ‘Social Media Links’, ‘Click’, ‘Twitter’]);”>Twitter</a>
- <a href=”#” onClick=”_gaq.push([‘_trackEvent’, ‘Social Media Links’, ‘Click’, ‘Facebook’]);”>Facebook</a>
Additional Uses for Event Tracking
Besides tracking videos and links to external sites additional uses for event tracking would be:
- File downloads (by file name and/or type)
- Specific link/button locations on your site
- For example you have a “register now!” button in several places on your site, and you want to know which button gets the most clicks
- Any other unique event/action you want to track that isn’t being included in the standard reports.
Custom variables can be set up to track a wide variety of things on your site that can’t be tracked in any of the other methods I’ve discussed so far, however they often can support some of the types of custom tracking I’ve discussed.
- For example, a custom variable can be set up to support eCommerce tracking by tracking how many times items are removed from a shopping cart.
- Another use case for custom variables would be to track different sections of your site (for example About Us, Contact Us, Products, Services).
- This is different than tracking traffic to just your “About Us” page – you may have several pages within your About Us section and you may wish to track how much traffic that entire section receives when compared to your other sections.
- If your site asks for users to sign-up or register and then log-in to access member only content, you may wish to track how many logged in members vs. guests you have visiting your site.
The basis of the custom variable is:
_setCustomVar(index, name, value, opt_scope)
This method accepts four parameters:
- index – The slot for the custom variable. Required. This is a number whose value can range from
5, inclusive. A custom variable should be placed in one slot only and not be re-used across different slots.
- name – The name for the custom variable. Required. This is a string that identifies the custom variable and appears in the top-level Custom Variables report of the Analytics reports.
- value – The value for the custom variable. Required. This is a string that is paired with a name. You can pair a number of values with a custom variable name. The value appears in the table list of the UI for a selected variable name. Typically, you will have two or more values for a given name. For example, you might define a custom variable name
femaleas two possible values.
- opt_scope – The scope for the custom variable. Optional. As described above, the scope defines the level of user engagement with your site. It is a number whose possible values are
3(page-level). When left undefined, the custom variable scope defaults to page-level interaction.
To see detailed code examples of how custom variables are set up, please see the Example Use Cases in the Google Analytics Developers Guide.
Here are some helpful resources to go beyond the examples I gave here:
- Google Analytics Developer Guides & Reference
- Occam’s Razor by Avinash Kaushik