# How to Track User Behavior With Google Analytics Events

Long story short; you have just redesigned your website, and you’ve made use of all the best practices you’re aware of. The website loads quickly, and it certainly looks much better with the new interface.

But have you considered how to measure your new design? How will you know if the new popup you have just added drives conversion, or harms the experience? How often is the popup displayed and how many people abandon it? How many users navigate the site from the off-canvas menu which you've also just added? How many people view the image slider on the homepage beyond the first slide? The questions are many and varied.

In this tutorial, I’ll show you how to leverage the Google Analytics Event Tracking API (what a long name!) to find the answers.

## Getting Started

If you aren’t already a Google Analytics user, create an account and follow the onboarding instructions to generate a tracking script for your website. Typically, the snippet you’re given will look like the example below with the UA-XXXXXX-X being the unique tracking ID of your website.

For this exercise you will also need to install a Chrome extension, Google Analytics Debugger, to debug the Google Analytics scripts on our site later.

Once we have these set up, we can begin with our first example.

## Tracking a Click

Imagine we have a couple of buttons (or anchor links styled as a buttons). We add the first one above the fold in our so called “hero” area, and a second button just before the footer. As you can see below, each button is pointing to the same page, has various styling classes, and a unique ID. In this case, we can utilize the “Event Tracking API” to find out which button receives more clicks.

Using Events Tracking is relatively easy, since it doesn’t strictly require conventions in terms of defining the event. First we need to bind the buttons to the click event.

Then we add ga() which is a function exposed from the Google Analytics scripts we added earlier, and which is the same function we use to record a “pageview”. Similarly, we track an event using the send command with event set as the hitType argument along with a number of required parameters, namely:

• eventAction: specifies the user interaction or the user interface state e.g. clickplaypause, etc.
• eventCategory: specifies the Object to track e.g. VideosButtonsPop-ups, etc.
• eventLabel: a unique label or ID of the event. We add this variable to categorize multiple instances of the same Object.

As mentioned, Google does not set strict rules; you can apply these in any way you see fit on your website. In our case, we set these variables as follows:

With the Google Analytics Debugger extension active, we can click one of our buttons and examine the DevTools Console to see whether the tracker works:

And once Google Analytics received the data, it will be recorded under the Real-time > Events and Behaviour > Events.

## Tracking a Carousel

Our second example will involve an Image Slider, or Carousel. You will probably have encountered arguments for and against using carousels on websites; “people don’t actually interact with carousels” or “people only interact with the first slide”. But do these arguments apply to your website? Without proper data, it is difficult to tell.

Let’s incorporate Google Analytics Events into our carousel, which we’ve built using Slick.js. This jQuery plugin provides a handful of custom jQuery Events which is just what we need to run Google Analytics Event Tracking. Please head over to the Slick documentation for details on how to build the carousel.

Our carousel consists of five slides. Similarly to our first example, we’ve also added a unique ID for each of the slides (e.g. slide-1slide-2slide-3, etc.) which we will pass in the eventLabel parameter. The goal here is to find out:

1. whether users view the carousel beyond the first slide
2. and to collect the number of views for each slide.

To do this we can utilize the Slick.js events swipe and afterChange.

### Swipe

The swipe event, as the name implies, is triggered when the user navigates the carousel using the swipe gesture. In this case, we set the eventAction parameter to swipe as well.

### afterChanges

The afterChanges is the event triggered when the slide is changed; in other words, when the user views the next or previous slide within the carousel. In this case, we set the eventAction to view for “new slide view”.

Once Google Analytics has gathered the data, we will soon find out how many users interact with our carousel, the number of views each slide receives, and how many users use a swipe gesture when using the carousel.

As we can see above, our carousel receives a total of 19 views, 14 of which are done through the swipe gesture.

## What’s Next?

We have just seen two examples of integrating Google Analytics Events Tracking into our web page for collecting user interaction data. These numbers give us a solid reference for judging whether the new design performs better than the old design, ultimately helping us improve the UX for our website.

I encourage you to check out the docs to further stretch your use of Google Analytics Events Tracking API.