Tracking Events with Google Analytics
Tags: jquery, googleanalytics
Published: 2017-06-13 11:50:08

---
title: Tracking Events with Google Analytics
published: true
description: A simple tutorial on how to create a reusable Google Analytics event tracker.
tags: Jquery, Google Analytics
---

*Please note: This article was written way back in 2015-07-14 on my own blog. I am moving relevant (interesting) articles over here. I made a few edits now.*

Google Analytics can do some pretty awesome stuff, it's a near standard addition to any site. We use it to see how many (or little) people visit our site, from where they come and to where they go. Today's little experiment is all about tracking specific links within a website in a friendly and front-end happy way. 

Turns out the Google Analytics API is quite powerful, and you can do a lot with it. When I designed this website I added a whole bunch of Call to Action type of links, and I figured it would be great to see just how valuable they would be to my end strategy. The simplest way for me to do that would be to treat the links as goals, and to track them specifically.

## Let's build a reusable event tracker!

The plan is to create the google event tracker in such a way that it's easy to implement within your site, and can be reused as often as we need it to.

### The Function

The tracker itself is by no means bulky code, but it's a hassle to rewrite everytime, especially if we can whittle it down to a single line call.

It really is as basic an example as it get's, but it's very useful and can provide a lot of value to your site.

```javascript
$('.ga-tracker').on('click', function() {

  // I want to know on what page the event fired, as my links are on many pages.
  var LOCATION = window.location.href
    
  // The category of the event. On my site I have two - general tracker and social trackers.
  var CATEGORY = this.data('category')
    
  // The action that takes place. Example: On my social tracker I made it 'share'.
  var ACTION = 'Click'
    
  // The label. I add the label we pass in track(); function and add the location.
  var LABEL = LOCATION + ' > ' + this.data('label')
    
  // Send data to Google Analytics
  ga('send', 'event', CATEGORY, ACTION, LABEL)

})
```

### Implementing this into your HTML

The event tracker above is a very basic one, and simply tracks specific links. Therefore our implementation is equally simple. We simply attach the class we named in the tracker we made in section 'B' of the above gist to the link we want to track:

```html
<a href="#" class="ga-tracker" data-category="General" data-label="Link #1">This is my tracked link</a>
```

That really is it. If you click the link a few times you will see the results in Google Analytics under:

* **Reporting** > Behaviour > Events

## Where to go from here

This is a tiny little function that does a tiny bit work. It's basic. So what can we do with:

* The actual data
* Get more useful data

### Working with the data

The first thing I noticed was that the event data is cool, but not very friendly and quite hidden. I created a custom dashboard, and that was cool, but not really all that exciting to me. It didn't tell me all that much.

So I set up Goals, which basically turns those events into conversions, which is really what you want to do. Now you can look at the events being fired as people doing what you want them to do. If that number is high, then good stuff! If it's low against your page views and unique visits, then you have a great opportunity to go figure out what you are doing wrong!

That is SO beyond the scope of this article though, so let's get back to code speak.

### Get more useful data

My example tracks links. Simple as that. But there are so many other things people do on websites that we can track. Imagine the possibilities of knowing when people scroll?

In terms of UX Design we can track how valuable that fancy carousel feature really is.

There is so much we can do, so I guess a little bit of experimentation is called for. Stay tuned!

## Testing

Since Google Analytics are generally delayed a bit with their reporting, testing can be a bit of a pain in the ass. There is a [Chrome Extention](https://chrome.google.com/webstore/detail/google-analytics-debugger/jnkmfdileelhofjcijamephohjechhna) that you can use to check (if you don't mind sifting through console messages). If you do use it you can look for something like this:

```bash
Running command: ga("send", "event", "Social", "Share", "Facebook : http://localhost:4000/blog/code/track-events-with-google-analytics/")
```

The simplest way to test is to simply use Google Analytics live view. It's somewhat dodgey, but does work fairly well.

## Further Reading

* [Event Tracking - Web Tracking (analytics.js)](https://developers.google.com/analytics/devguides/collection/analyticsjs/events)