This is a summarized version of a great post by North Studio about setting up event tracking. For more information read the original article.
Google Analytics allows you to track metrics like sessions, page-views and bounce rates without any special set up. Event tracking gives you greater insight into how readers are interacting with your website by tracking events like downloads or link clicks.
Setting up event tracking requires three steps:
- Decide what you want to track
- Add a tracking code to your website
- Set up a goal in Google Analytics
Once you've decided on what to track, you need to add a tracking code to the link for that action. For example, if you want to track PDF downloads, you need to add a tracking code to the download PDF.The code you use will depend on which version of Google Analytics you are using. To determine the version you have installed, go to the "Page Source" for your site, CTRL+F to search for "analytics.js" (Universal Analytics) and "ga.js" (Classic Analytics), whichever appears is the version of Google Analytics installed.For Classic Analytics (ga.js) your code will follow this format:
onClick="_gaq.push(['_trackEvent', 'category', ‘action', 'opt_label', opt_value, ‘opt_nonInteraction’]);"
For Universal Analytics (analytics.js) your code will follow this format:
onClick="ga(‘send’, ‘event’, ‘category’, ‘action’, ‘label’, value, {‘NonInteraction’: 1});"
These are the variables you need to consider:
- Category (required) - This variable specifies the type of object that was interacted with.
- Action (required) - The specific action a user took to trigger an event.
- Label (optional) - Allows you to provide more context about the interaction.
- Value (optional) - The numerical-value of the event.
- NonInteraction (optional) - A boolean that when set to ‘true’ (or “1” for Universal Analytics) excludes the event from affecting metrics like Bounce Rate.*
*By default, Google Analytics considers events as an “interaction”, which in turn affects your bounce rate.Continuing with the PDF download example, your download link might look like this:
<a href="http://mywebsite.com/article1.pdf"> Article 1 PDF </a>
And your variables would be as follows:
- Category = PDF Downloads (because you're tracking PDF downloads across your site)
- Action = Click (because that's the action the reader needs to take to access the PDF)
- Label = Article 1 (because that's the name of our PDF)
- Value = (none)
- NonInteraction = false
With Classic Analytics Tracking (ga.js) your code would look like this:
<a href="http://mywebsite.com/article1.pdf" onClick="_gaq.push(['_trackEvent', ' PDF Downloads', ‘Click', 'Article 1');"> Article 1 PDF </a>
With Universal Analytics Tracking (analytics.js) it would look like this:
- <a href="http://mywebsite.com/article1.pdf" onClick="ga(‘send’, ‘event’, ‘ PDF Downloads’, ‘Click’, 'Article 1');"> Article 1 PDF </a>
Finally, you need to set up Google Analytics to track it.- Log in to your Google Analytics account.
- Go To “Admin”
- Under the “View” column, click on “Goals”.
- Click on “New Goal”.
- Under “Template”, click on “Custom”.
- Choose a name for your goal and then under “Type”, select “Event”.
- Enter your category and action. Verify your goal and then click “Create Goal”
To make sure everything is set up correctly, do a quick test.
In Google Analytics, go to the "Reporting" click on "Real-Time" then click on "Events".
In a separate window, go to your page and click on the event you're tracking (ie. the Article 1 PDF link).
The action should show up in your Google Analytics: confirm that it has the correct Category, Action, and Label and you're all set!