Better Google Analytics Reports

Better Google Analytics Reports

We recently implemented detailed Google Analytics (GA) tracking for a client.

GA tracks a lot of things out of the box. But it was not answering several questions that our client had about the usage of their website, such as:

  1. How many times have articles written by Jane Doe been viewed?
  2. How many times have posts categorized under “Politics” been viewed?
  3. How many times have reports categorized under “Foreign Policy” been downloaded?

So we created 3 GA events in Google Tag Manager (GTM):

  1. Track an author’s name when one of their posts is viewed
  2. Track a post’s category when a post is viewed
  3. Track a report’s category when its PDF is downloaded from a report page

Authors and post categories are not available to GA by default. So we had to add them to GTM’s data layer before the three events above could be defined. We were able to do so automatically using this GTM plugin for WordPress. But even if your website’s CMS doesn’t have a plugin like this, your developer can easily add variables like these to GTM’s data layer.

Smooth sailing so far. But here’s where we started running into trouble. A post typically has more than one category. And sometimes more than one author. But GA4 events can’t accept many values as an event parameter value. In GA3 (Universal Analytics) there is a hack. You can string many category names together as a single value using a delimiter character such as “|”. E.g. “|Politics|Foreign Policy|”. This would then be captured using content grouping which does not have a strict character limit. But that was in GA3. In GA4, everything is an event. There is no separate feature for content grouping like in GA3. Since event values have a character limit, we cannot rely on stringing many category names together. Some categories names might get cut off or be omitted entirely.

image alt text
Example of many categories stringed together and tracked as a single value using GA3 content grouping

Luckily, this “hack” is no longer necessary. Instead of sending a single event with all categories or authors, we can send a separate event for each category and for each author.

Each category is tracked as a separate event (new method)

To achieve this, we used a guide from Slider Rule Tech which includes a template file that can be imported into GTM. This allowed us to create a “taxonomy tracker” tag to successfully track all the categories of a post when it’s viewed.

You may be wary of importing a foreign template file into a system that connects to all your website’s usage data, but this template file is safe. It merely includes a simple data layer loop to iterate over taxonomy values and send each of them to GA as an event. Your developer will be able to verify this by inspecting this template file. Below is the entirety of the code for reference:

const callInWindow = require('callInWindow');
const createQueue = require('createQueue');
const dataLayerPush = createQueue('dataLayer');
const taxonomyValuesArray = data.taxonomyValuesArray;

// Check if taxonomyValues is an array to avoid an error
const isArray = arr => callInWindow('toString.call', arr) === '[object Array]';

if (isArray(data.taxonomyValuesArray)) {
  data.taxonomyValuesArray.forEach(t => dataLayerPush({
    taxonomyValue: t,
    taxonomyName: data.taxonomyName,
    event: 'taxonomyTracker'
  }));
}

// Call data.gtmOnSuccess when the tag is finished.
data.gtmOnSuccess();

It seems like tracking multiple content categories and authors should be a feature that’s baked into GA, but until there is an official solution, we think this is the best way to achieve this and extend your GA setup.

Contact

1 Lee Hill Road
Lee, NH 03861

‪(978) 238-8733‬

Contact Us

Connect

Subscribe

Join our email list to receive the latest updates.