Friday, March 11, 2016

Tracking categorical variables with Google Analytics and Google Tag Manager

Recently I was working with a magazine’s website. And one of the questions was to construct the report about an authors’ popularity. Surprisingly, this one turned out a bit challenging.

I needed to sum up all visits to all of the articles’ pages for every particular author. Using these numbers, I would build a rating of authors. The problem is in fact that any author can have many articles, and any article can be written by several authors (coauthors). Speaking SQL, it’s a “Many-To-Many Relationship”.

First of all, ‘author’ is a categorical variable since it takes discrete values of a string type, like an author’s name concatenated with a surname. Therefore, we can’t use Custom Metrics of Google Analytics since it can only be a number, time or currency. No dictionary option here.

On the other hand, Google Analytics offers Custom dimensions for such cases. If you have implemented Google Tag Manager, you can just make the new custom dimension ‘author’, and pass the author’s name from each article’s page through the Data Layer. But not in our “Many-to-Many” case!

If an article was written by two or more authors, you can’t pass one long string with the authors’ names, like this: “A.Johns, B.Johnson, C.Jacobs”. This is not an option, because you need to make three (in this example) distinct database entries: one for each author.

So we have the categorical variable with multiple values for each pageview.

Here is my approach on how to handle the situation..

  1. As it was said, first of all we need a new custom dimension ‘author’. The Scope obviously is Hit level, because every article (page) has its own set of authors.
    We can make it in Admin panel of Google Analytics interface. Note that if you use a free version of GA, you have only 20 custom dimensions (as of March 2016).
  2. We need to make the corresponding (User-Defined) Variable ‘author’ in GTM.
  3. Now we implement the variable into Data Layer on our website. But not the usual way.
    Instead of putting it into common Data Layer block, we use dataLayer.push() for each author to make several distinct entries. And don’t forget to include ‘event’ into each call. It’ll help us to fire particular Author’s Tag via Author’s Trigger (see later).

    Altogether, the example is as follows (for the case of three authors):
'author': 'A.Johns',
'event': 'author-event'

'author': 'B.Johnson',
'event': 'author-event'

'author': 'C.Jacobs',
'event': 'author-event'

  1. Next, we return to GTM and make Authors Trigger which has the ‘Custom Event’ event type and fires only on the articles’ pages and under the condition if 'author-event' occurs.
  2. Almost there! We need to set up an Authors Tag. The key moments are:
    1. Track type - Event
    2. Non-interaction - True
      (because the author name transfer to the GA server is not an interaction with the user)
    3. Custom Dimension - here we put our custom dimension ‘author’. Don’t forget to put the right index which should match the index of the ‘author’ dimension in GA Admin panel (see point 1 earlier).
    4. Fire on our recently made ‘Author trigger’.
  3. Finally we need to publish all the changes in GTM to our website.

So that’s it. If everything is done properly, statistics about the authors should start to flow into the GA database. Now we can build custom reports such as rating of the authors’ popularity and many more.

To check whether our trigger and tag work properly, we can use GTM preview mode. After switching it on, we can browse our website and see GTM panel with tags, triggers, events, variables and their values at the bottom of the screen.

Important note!
When doing custom reports on a categorical variable established this way, don’t forget to use ‘Hits’ metric instead of ‘Pageviews’. Recall that we used custom trigger instead of common ‘All Pages’ trigger and our ‘author’ variable didn’t get into common Data Layer block. Generally speaking, our custom tag can be fired after the main ‘Pageview’ event (if we put dataLayer.push() after the main GTM container). So there will be zero Pageviews for B.Johnson, but plenty of Hits!

No comments:

Post a Comment