24 Oct 2016
Google Analytics (GA) is a free service from Google which is fantastic for tracking visitors to your site. There are so many things you can find out from GA, including country, demographic data like age and sex, which site they were referred from, and the key words they entered in google to find your site. GA is everywhere. If you have your own site, or you build sites for your clients, I would be very surprised if you don't use it already.
GA tells you where people went on your site but it doesn't tell you how they interact with your site. Have a think about these questions:
You can answer all of the above questions, and more, by having event tracking set up. Here's how to do it in 3 easy steps:
<script type="text/javascript"> //GA Event Tracker Script. Licensed under MIT. Free for any use by all. Written by Paul Seal from codeshare.co.uk
// Get the category, action and label from the element and send it to GA. The action is optional, because mostly it will be a click event. var trackClickEvent = function () { var eventCategory = this.getAttribute("data-event-category"); var eventAction = this.getAttribute("data-event-action"); var eventLabel = this.getAttribute("data-event-label"); var eventValue = this.getAttribute("data-event-value"); ga('send', 'event', eventCategory, (eventAction != undefined && eventAction != '' ? eventAction : 'click'), eventLabel, eventValue); };
// Find all of the elements on the page which have the class 'ga-event' var elementsToTrack = document.getElementsByClassName("ga-event");
// Add an event listener to each of the elements you found var elementsToTrackLength = elementsToTrack.length; for (var i = 0; i < elementsToTrackLength; i++) { elementsToTrack[i].addEventListener('click', trackClickEvent, false); } </script>
<a href="/about/" class="ga-event">About</a>
<a href="/about/" class="ga-event" data-event-category="Footer Links" data-event-label="About">About</a>
It's up to you what values you put in these data attributes, just put what makes sense. Use the category to group things together and use the label to give the detail.
There are 2 more attributes which you can add optionally.
By default this code records the the action as a 'click', but if you want to use something else like 'play' or 'download' you just add this attribute data-event-action and set the value to whatever you want.
You have the option to pass a whole number to GA using data-event-value. GA will tell you the total value and average value split by your events. I think this is something you would use for the number of seconds something took to load or play. Perhaps this is to be used more with your own custom scripts and not loaded into the elements by default.
Now you have all you need to get event tracking up and running. It's so easy and it gives you a new dimension of tracking.
If you found this article useful, please share it with others.