Google Analytics Event tracking is a very useful feature that allows you to track users’ interactions with different elements on your website, that Google Analytics would not normally be able to track. Most often, it is used to track clicks on buttons, video player, submission forms, but it can also be used for many more website elements that you consider important for user’s engagement.
To enable event tracking in Google Analytics, you’ll have to create a snippet of custom code and add it to the code of the website elements you want to track.
Don’t panic! It’s not so difficult as it may sound.
In this article, I’m explaining the anatomy of the Event Tracking code in a simple and comprehensive way (I hope!). You will learn how the code works and how exactly to customize it, according to your needs.
In the next article, we will see how exactly to implement the customized event tracking code to your website.
NOTE: Before continue reading, you must consider what version of Google Analytics you’re using. This article explains the anatomy Event Tracking when you’re using the standard Classic Asynchronous Google Analytics (ga.js) code. If you’re using Universal Google Analytics (analytics.js) which is currently in beta, you should refer to the official documentation.
Event tracking method for Classic Google Analytics accounts
If you’re using Classic Analytics Web Tracking (ga.js library) you’ll have to implement the _trackEvent() method to track events on your website.
The code for the _trackEvent() method looks like that:
_trackEvent(category, action, opt_label, opt_value, opt_noninteraction)
The _trackEvent() method uses 5 parameters (or components).
Only two of these parameters are required, the other three are optional.
Each one of these components sends data to Google Analytics.
Now, let’s have a closer look at each one of these parameters.
That’s the name you give to a group of objects you want to track. Think of the category parameter as a type of object.
Typically, you will use the same category name multiple times, because you’ll group all identical objects under a given category (all videos will be regrouped in the category “Videos”).
Typical examples of categories are:
“Category” is a required element.
This parameter is used to name the exact type of user interaction (event) you want to track for an object from a particular category.
Because your visitors may have different options to interact with the objects from a specific category, you need to use to action element to distinguish these different actions.
An action might be:
- “Play” or
- “Click” or
- “Click on a Button” or
- “Click on a link.”
For instance, within a “Videos” category, you can track different types of interactions, such as:
- “Play” button clicks
- “Stop” button clicks
- “Pause” button clicks
Within a “Resources” category, you can track the following actions:
- “Downloads” button clicks
- “Preview” button clicks
- pdf button clicks or doc button clicks – you could even provide the document file type as the action parameter
So use the Action parameter to name
these specific types of events. A unique event is determined by a unique action
You can use duplicate action names across categories, but this can affect how unique events are calculated.
“Action” is a required element.
opt_label can be used to provide
additional dimensions to the event that occurred.
Most often it is used to know where (on which page) the event took place.
Imagine that you have the same download button on multiple pages. Each one of these buttons can use the “Button” category with the “Download” action, but each could also have a separate label. You can use the label parameter to distinguish the buttons on the different pages or to distinguish the buttons with different colors so that they appear as distinct elements in the report.
If you have different videos, you can use the label parameter to name the different videos, and therefore you’ll be able to distinguish them in the Event Reports.
If your websites offer different documents to download (regrouped in the “Resources” category), you can name these different documents using the label parameter.
Some typical examples for labels are:
- URI of the page on which the event occurred
- The exact position of the object on which the action took place (sidebar, footer)
- Color of the object on which the action took place
- The exact name of the object on which the event occurred
“Label” is an optional element.
This is an integer that you can use to assign a numerical value to a tracked page object.
opt_value can be used for whatever metric you want, as long as it’s a positive number.
For example, the value parameter can be used to track:
- dollar value: if you assign a value (revenue) for the event
- time: when you assign time (in seconds) of the event (time of a player to load)
Value is an optional element.
The last parameter in the _trackEvent()
method is opt_noninteraction.
It is used to tell Google Analytics if it should count the Event as an interaction request or not.
By default, the event hit is considered an interaction hit, and therefore it is included in bounce rate calculations.
Therefore, every time when you implement Event Tracking you will notice a decrease in the bounce rate for the pages on which you track Events.
To prevent this from happening you should set the opt_noninteraction to true.
To illustrate, let’s look at this session:
- User A visits a page B where Event Tracking is present,
- User A performs the Event (play a video or click on a button) on page B,
- User A leaves your website after performing the Event and without visiting any other web pages.
Normally, this visit should be considered as a bounce: it’s a single-page visit and Google Analytics server must have received one single GIF request for the entire session. But things change when Event Tracking is present.
When User A completes the Event on page B, Analytics records this Event Completion as an additional hit and sends it to the server as an additional GIF request. This interaction hit (GIF request) is treated by Google Analytics in the same way as visiting another page. At the end GA has recorded two hits for User A’s session: the first one is the actual visits of page B, the second one is the completion of the event on page B.
Therefore User A’s single-page visit is not considered a bounce anymore and the bounce rate of page B will drastically decrease.
To prevent this from happening you can use the opt_noninteraction element.
opt_noninteraction is a boolean value which just means it can be either true or false:
- true = makes the event a non-interaction – to specify that this Event should not be counted as an interaction and thus, should be ignored for the bounce rate calculation.
- false = count the event as a user interaction – to specify that the Event should be considered as an interaction.
- empty = default = false – opt_noninteraction is an optional parameter, but it’s default value is false. This means that if you leave it empty, Google Analytics will consider the event interaction and the bounce rate of the page will decrease.
Now, after we’ve broken down to the basics of the event tracking, you are able to alter the tracking code to your own needs and start to track your own events. The next step would be decided on which elements of your site you want to track to implement the code to your website and to start collecting data. Check out my next post, to see how and where exactly to put the event tracking code.