Google Event Tracking


Since I’ve recently moved from WordPress to a custom solution I’ve missed a few things about the WordPress stats – more specifically which links were clicked in.  I think it’s a good thing to know whether or not people like the links I often include in my blog articles.  I try not to link out too much, as the whole goals of my articles are for you to stay and read them!  Crazy concept, I know…

Like most people, I rely on Google Analytics to provide me the stats about my website.  I certainly don’t have the traffic that requires a custom solution!  The nice part is that Google has made this very easy through Custom Event Tracking.

 When you are inside your Google Analytics account under Standard Reporting the following should be available for you.  Click Content -> Events -> Overview.  This will provide you a list of the events that were tracked on your website.

Of course, if you have never used them before, this page will probably show 0.  So let’s explore adding and tracking new events.

The first thing that is required is a function that your code can call that will send a tracking event to Google.  Below is a very common one:

function gaTrackEvent(category, action)
_gat._getTrackerByName()._trackEvent(category, action);

Be sure this code is placed below your script that includes GA itself!

Now that I have a function to use, I’m going to leverage jQuery selectors to globally track certain Javascript click events.  The example below will track all click events inside of my post class:

$(“.post a”).click(function() {
gaTrackEvent(“PostLinks”, $(this).prop(‘href’));

In the above example I’ve attached a click event to all links within the body of my posts.  When a click happens, I call the previously created gaTrackEvent function passing in the two required parameters.

The first parameter specified will be the Event Category and the second parameter is the Event Action.  Inside of my Google Analytics, I now have two ways to track the clicks; firstly I can get a summary for the category PostLinks or I can view the individual action.  In the above example, I’m simply supplying the full URL that was clicked on.

For much more detailed information visit the eventTrackerGuide from the Google developer zone.

About the author

By Jamie

My Books