Tracking Users in Google Analytics

Published Jan 13, 2015 (10 years ago)
Danger icon
The last modifications of this post were around 10 years ago, some information may be outdated!

Google Analytics provides some great insights into which pages are being accessed and a lot of usage statistics about them. But for web applications, you canā€™t immediately track which users are using which pages, unless you are doing your own tracking in the database code. However, I found a quick way to do some basic user tracking without much setup.

Configuring User Tracking

The newest version of Google Analytics (the analytics.js file) changed the implementation of its events tracking to make it a lot easier and customizable. An ā€œeventā€ can now be generated down to 3 levels of data:

  • Category
  • Action
  • Label

Technically there is a 4th level (value) but since it can only be a numeric field, we canā€™t really use that (unless you track user id values).

So send a ā€œ3 levelā€ event, we simply add the following code after we setup your Google Analytics tracking:

ga(ā€˜sendā€™, ā€˜eventā€™, 'category', 'action', 'label');

For our user tracking, weā€™re going to define our levels as follows:

  • Category - Controller/Page user is on (Shipments Page)
  • Action - Action/Feature user is performing (Show Shipment)
  • Label - Username of user

In my case, I have a .Net MVC app in place, so my Razor code on the front end looks something like this:

@if (User.Identity.IsAuthenticated) 
{
<text>
var page = '@ViewContext.Controller.ValueProvider.GetValue("controller").RawValue';
var action = '@ViewContext.Controller.ValueProvider.GetValue("action").RawValue';
var user = '@User.Identity.Name';

// Change the action to friendlier names.
if (action == 'Index') { action = 'Home'; }

ga('send', 'event', page, action, user); </text>
}

Thatā€™s all there is to it. Start up your app, log in, and you can start seeing the events tracking in action. Since events are supported in the beta ā€œReal Timeā€ tracking as well, you can even see which users are logged in to your application.

Viewing User Activity

Once you start getting data in, youā€™ll want to start tracking the activity of your users. Log in to your Google Analytics account, expand the ā€œBehaviorā€ section, then the ā€œEventsā€ section and click the ā€œOverviewā€ link. This will bring up a page like this:

At this level you can see our categories, which corresponds to our pages. This isnā€™t that different than what you can see elsewhere in Google Analytics. The magic comes when you click on the ā€œEvent Labelā€ section in the ā€œTop Eventsā€:

Now you can track how many events each user raised. This will help you see which users are more active than others on the site. The next step is to track what a given user is doing. Go ahead and click on one of the Event Label values to bring up their details. The default view wonā€™t show much detail, however, if you apply a second dimension to the report, Behavior -> Event Category, you get a better idea of what your user is doing:

Now we can see that our user spends most of their time on the shipments page. We can refine this even more into a report screen that will drill down each user to list their Categories and Actions, to give us an idea of the specific things they were doing on each page.

Iā€™ll admit that this isnā€™t the most elegant of solutions, but setting up custom dimensions within Google Analytics, and then wiring them up through the code can take some time and you still donā€™t have the real time data offered through events. This will give you a quick way to get some nice metrics on your app usage without much setup time.

If you know of any other shortcuts through Google Analytics, definitely pass them along!