Analytics & Testing, Content Marketing, Marketing & Sales Videos, WordPress

How to Implement Google Analytics Custom Groups with Google Tag Manager

In a previous article, I shared how to implement Google Tag Manager and Universal Analytics. That’s a fairly basic starter just to get you off the ground, but Google Tag Manager is an incredibly flexible (and complex) tool that can be used for dozens of different strategies.

While I realize some development could alleviate some of the complexities of this implementation, I opted to go manual with plugins, variables, triggers and tags. If you have a better means of implementing this strategy without code – by all means share it in the comments!

One of those strategies is the ability to populate Content Grouping in Universal Analytics using Google Analytics. This article is going to be a combination of a rant, problems to be aware of, and a step-by-step guide in specifically implementing Content Grouping using DuracellTomi’s Google Tag Manager plugin for WordPress, Google Tag Manager and Google Analytics.

Google Tag Manager Rant

For such an amazingly complex tool, Google support articles absolutely suck. I’m not just whining, I’m being honest. All of their videos, such as the one above, are these bright and colorful videos on what can be accomplished with absolutely no step by step videos, no screenshots in their articles, and only top level information. Sure, they’ll include all the options and flexibility you have at your disposal but you don’t actually have any details on deploying it.

After 30 versions of deploying my tags, dozens of edits within Google Analytics, and a few weeks passing in between changes to test… I found this exercise incredibly frustrating. These are two platforms that should work seamlessly but truly have almost no productized integration whatsoever outside a couple of fields to prepopulate.

Google Content Grouping Rant

While categorization and tagging has been around for a couple of decades, you won’t find it in the abilities of Content Grouping. Perhaps I publish a post like this that incorporates multiple categories, a dozen or so tags, screenshots, and video. Wouldn’t it be amazing to slice and dice that information using Google Analytics? Well, good luck, because your ability to develop content groups are restricted. There are no means of passing an array of categories, tags, or characteristics to Google Analytics. You’re stuck with basically 5 text fields limited to one variable each.

As a result, I’ve designed my Content Grouping the following way:

  1. Content Title – So that I can look at articles like “how to’s” and other commonly titled articles.
  2. Content Category – So that I can look at the primary category and see how popular each category is and how the content is performing within.
  3. Content Author – So that I can view our guest authors and see which ones are driving engagement and conversions.
  4. Content Type – So that I can look at infographics, podcasts, and videos to see how that content performs in comparison to other content types.

The rest of this tutorial is based on the fact that you’ve already signed up for Google Tag Manager.

Step 1: Setting Up Google Analytics Content Grouping

You don’t actually have to have any data coming to Google Analytics in order to setup your Content Grouping. Within Google Analytics, navigate to administration and you’ll see Content Grouping on the list:

content-groupings-admin

Within Content Grouping, you’ll want to add each content grouping:

Add Content Grouping

Note the two arrows! To save yourself from ripping your hair out when your data isn’t showing up in Google Analytics, be absolutely vigilante in double-checking the slot matches your index number. Why this is even an option is beyond me.

The finished content grouping list should appear like this (when you click sort… because for some reason Google Analytics likes to torture us obsessive compulsive users who wonder why they aren’t already sorted in numeric order. Oh… and if that’s not torture enough, you can’t ever delete a content grouping. You can only disable it.)

content-grouping-list

Whew… looking good. Our job is done in Google Analytics! Sort of… we’ll have to test and send some data later that we can review.

Step 2: Setting Up DuracellTomi’s WordPress Plugin for Google Tag Manager

Next up, we need to begin publishing data that Google Tag Manager can capture, analyze, and trigger Google Analytics code through. This could be quite an undertaking it weren’t for some amazing WordPress developers out there. We love the options available through DuracellTomi’s WordPress plugin. It’s well managed and supported.

Grab your Google Tag Manager ID from your Workspace in Google Tag Manager and place it in the plugin’s general settings > Google Tag Manager ID field.

google-tag-manager-id

I would highly recommend installing the plugin by utilizing the custom method where you insert the script into your theme (typically the header.php file). If you don’t, it can cause another issue that will absolutely drive you crazy… the dataLayer that the plugin is sending to Google Tag Manager must be written before the script is loaded for Google Tag Manager. I don’t understand the logic involved there, just know that you’ll be pulling your hair out wondering why data isn’t being properly sent without this placement.

google-tag-manager-custom

The next step is to configure what dataLayers you want to be passed into Google Tag Manager. In this case, I am passing the post type, the categories, the tags, the post author name, and the post title. You’ll see that many other options are available, but we’ve already explained the groupings we’re configuring and why.

Google Tag Manager WordPress dataLayer

At this point, the plugin is installed and Google Tag Manager loaded, but you don’t actually have data passed to Universal Analytics (yet). If you view the source of your page now, you will see dataLayers published for Google Tag Manager, though:

Code View

Notice that the dataLayer is joined in key-value pairs (KVPs). In Step 4 below, we’re going to show you how to verify these without looking at the code source of your page. For the DuracellTomi Plugin, the keys are:

  • pageTitle – This is the title of the page.
  • pagePostType – This is whether it’s a post or page.
  • pagePostType2 – This is whether it’s a single post, category archive, or page.
  • pageCategory – This is an array of the categories the post was categorized in.
  • pageAttributes – This is an array of the tags the post was tagged for.
  • pagePostAuthor – This is the author or the post.

Keep these handy, we’re going to need these later as we write our triggers.

I’m assuming you have a Google Analytics plugin loaded or you’ve embedded the analytics script tag in your theme yourself. Write down your Google Analytics ID (looks like UA-XXXXX-XX), you’ll need that next. You’ll want to remove the script tag or the plugin, then load Universal Analytics through Google Tag Manager.

Step 3: Setting Up Google Tag Manager

If you’re panicked about not having Google Analytics published on your site at this point, let’s just do that real quick before we make any modifications. When you log into Google Tag Manager, select your Workspace:

  1. Select Add a Tag
  2. Select Universal Analytics, name your tag in the top left and enter your UA-XXXXX-XX id
  3. Now tell the tag when to fire now by clicking on Triggering and selecting all pages.

Universal Analytics Add Tag Google Tag Manager

  1. You’re not done! Now you have to click Publish and your tag will be live and analytics will be loaded up!

Step 4: Is Google Tag Manager Actually Working?

Oh, you’re going to love this one. Google Tag Manager actually comes with a method to test your tags to help you troubleshoot and fix them. There’s a little menu on the Publish option that you can click on – Preview.

Google Tag Manager Preview and Debug

Now open the website that you’re working on in a new tab and you’ll magically see the Tag Manager info in a footer panel:

Google Tag Manager - Preview and Debug

How cool is that? Once we get into passing the Content Grouping data using Google Tag Manager, you can see what tag is firing, what’s not firing, and any data that’s being passed! In this case, it’s the Tag we named Universal Analytics. If we click on that, we can actually view the Google Analytics tag information.

Step 5: Setting Up Content Groupings in Google Tag Manager

Woohoo, we’re almost done! Well, not really. This is going to be the step that may really give you a tough time. Why? Because firing a pageview in Universal Analytics with the Content Grouping must be accomplished in a single event. Logically, here’s how it has to happen:

  1. The WordPress page is requested.
  2. The WordPress Plugin displays the dataLayer.
  3. Google Tag Manager script executes and passes the dataLayer from WordPress to Google Tag Manager.
  4. Google Tag Manager variables are identified in the dataLayer.
  5. Google Tag Manager triggers are identified based on the variables.
  6. Google Tag Manager fires specific tags based on the triggers.
  7. A specific tag is fired that pushes the appropriate content grouping data to Google Analytics.

So… if the first thing that happens is that the dataLayer is passed to Google Tag Manager, then we must be able to read those key-value pairs. We can do this by identifying those variables passed.

Google Tag Manager User-Defined Variables

Now you need to add and define each of the variables passed in the dataLayer:

  • pageTitle – Content Title
  • pagePostType – Content Type
  • pagePostType2 – Content Type (I like this using this one since it’s more specific)
  • pageCategory – Content Category
  • pageAttributes – Content Tags (you may wish to use this from time to time instead of just categories)
  • pagePostAuthor – Content Author

Do this by writing in the Data Layer Variable Name and saving the variable:

Variable Configuration

At this point, Google Tag Manager know understands how to read the dataLayer variables. It would be nice if we could simply pass this data right into Google Analytics, but we can’t. Why? Because your array of categories or tags will surpass the character limits set on each Content Grouping allowed in Google Analytics. Google Analytics (sadly) can not accept an array. So how do we get around it? Ugh… this is the frustrating part.

You’re going to have to write a trigger that searches for your category or tag name within the array string passed in the dataLayer variable. We’re okay passing title, author, type since they’re single text terms. But category is not so we need to review the first (primary) category passed in the array. The exception, of course, is if you don’t select multiple categories per post… then you can just click the button and select Content Category.

Here’s a partial look at our list of Triggers:

Triggers by Category

Here’s an example of one of those triggers for our category for Content Marketing:

Some Page View Triggers

We have a regular expression here that matches the first (primary) category passed in the array in the dataLayer, then we make sure that it’s a single post.

If you’re having a difficult time writing regular expressions, you may wish to just stop pulling your hair our and get on Fiverr. I’ve had incredibly great results on Fiverr – and I typically ask for the expression as well as documentation on how it worked.

Once you have a trigger set for each category, you’re ready to build your tag list! Our strategy here is to first to write a catch-all Universal Analytics tag (UA), but is not fired whenever any of our category tags are fired. The completed list appears to look like this:

Tags in Google Tag Manager

Alright… this is it! We’re now going to bring all the magic together with our tag. In this example, I’m going to pass the Content Grouping for any single post that’s categorized with Content Marketing (“content”):

Category Content Groups

Name your tag, enter your Google Analytics ID, and then expand More Settings. Within that section, you’ll find Content Groups where you’ll want to enter the Index number exactly how you entered it in Google Analytics Admin settings.

Here’s another dumb thing… the order must match the order of your Analytics Admin settings for the data. The system isn’t intelligent enough to grab the right variables for the proper index number.

Since the category isn’t passed (because of the array difficulty), you’ll have to type in your category for Index 2. However, for the other 3 content groups, you can just click the box to the right and select the variable that’s directly passed within the dataLayer. Then you’ll need to select the trigger and save your tag!

Repeat for each of your categories. Then be sure to go back to your UA (catch-all) tag and add exceptions for each of your categories. Preview and debug to test and ensure you’re firing your tags and sending data to the content groupings properly.

You should be able to verify everything, but you’ll still have to wait a few hours for Google Analytics to catch up. The next time you log in, you’ll be able to use Content Title, Content Category, and Content Author to slice and dice your data in Google Analytics!

Download a Sponsored Marketing Whitepaper:
YouTube for Business - Everything You Need to Know

YouTube for Business - Everything You Need to Know

Internet analytics company Alexa currently ranks YouTube No. 2 both domestically and globally in web traffic. Download Now

3 Comments

  1. 1

    Hi Douglas,

    Thanks for taking time to put together this article. As a professional who spends a lot of his time working with Google Tag Manager and Google Analytics, I’d like to share some thoughts I have to the points you’ve raised.

    I think that there are a number of weaknesses with both tools; this reply is not going to focus on that. Rather, I’m going to address points in your article where I think you are correct, and other areas that I disagree agree with. I believe this type of dialogue is healthy within our professional sphere. I am NOT trying to troll.

    “For such an amazingly complex tool, Google support articles absolutely suck”

    I think you’re looking at the wrong documentation. With regards to the “high level” videos, yeah — you’re not going to get very far. Google’s documentation certainly did used to suck, but it is much better now.

    Since both GTM and GA are tools which require a fair amount of technical knowledge to implement correctly, I’d like to suggest that your readers turn to the developer guides for these products:

    https://support.google.com/tagmanager/
    https://developers.google.com/tag-manager/devguide

    Also, the internet has no shortage of easily accessible guides for doing basically whatever the heck you want with GTM. The best sources of knowledge are:

    https://www.simoahava.com/
    https://www.thyngster.com/
    http://www.lunametrics.com/blog/

    Basically, anything that I’d want to write myself about GTM has already be covered by those three.

    As far as I’m concerned, the A-Z documentation doesn’t need to come from Google. The community is so robust you can find any answer with little effort.

    “These are two platforms that should work seamlessly but truly have almost no productized integration whatsoever outside a couple of fields to prepopulate.”

    I think you are misunderstanding what GTM is. It works great with GA, much better than any other TMS. GTM is not only for deploying Google Analytics. That said, I would not deploy GA using any other tool.

    GTM’s Google Analytics tag is a graphical user interface for deploying code that many people would find difficult to manage.

    When it comes to content groups, it sounds a lot easier to me to fill in a little box in GTM with a variable than to write

    ga(‘set’, ‘contentGroup’, ”);

    and have the dynamic values of your fields populated by server-side logic that is more difficult to maintain than a data layer.

    “There are no means of passing an array of categories, tags, or characteristics to Google Analytics”

    While you are correct that Google Analytics records values for Content Groupings as strings, not arrays or objects, that is simply a technical terminology.

    You absolutely can pass an array of categories or tags to GA. Turn your array into a delimited string and you’re set.

    A simple custom javascript variable will turn your array into a string.

    function(){
    var pageCategory = {{dl – page – pageCategory}};
    return pageCategory.join(“|”);
    }

    See this article for examples of how to analyze that data: http://www.lunametrics.com/blog/2016/05/25/report-items-in-multiple-categories-in-google-analytics/

    Do you need to know some basic javascript to use GTM effectively? Definitely. Is that a short coming of the tool? Absolutely not. It’s a TMS. Of course you need to know javascript to use it.

    ” Oh… and if that’s not torture enough, you can’t ever delete a content grouping. You can only disable it.”

    INDEED. There really should be toggles to remove the field from reports.

    “the dataLayer that the plugin is sending to Google Tag Manager must be written before the script is loaded for Google Tag Manager”

    This is a problem with the plugin. The author of the plugin is initializing the dataLayer incorrectly and not using an “event” which is GTM’s internal messaging bus. Don’t pull out your hair, though. It’s not worth it.

    Jumping to step 5 (other steps seem on target)

    “Because your array of categories or tags will surpass the character limits set on each Content Grouping allowed in Google Analytics. Google Analytics (sadly) can not accept an array. So how do we get around it? Ugh… this is the frustrating part.”

    This is not an issue of character limits for GA. You just need to change your array to a string, which is the expected value in GA’s API. A dimension describes thing. So a string (word) is what is expected.

    “Once you have a trigger set for each category, you’re ready to build your tag list!”

    Noooooo! 🙂 Don’t go down that path. Use a delimited value and you save yourself tons of headache.

    “Here’s another dumb thing… the order must match the order of your Analytics Admin settings for the data. The system isn’t intelligent enough to grab the right variables for the proper index number.”

    I don’t believe that is true. As long as your index is a number, the value for the index will populate your tag with the correct value.

    The main positive takeaway I have from your article is that your readers are exposed to a critical way to “slice and dice” data in GA. That is of huge importance and there are free plugins for WordPress that will allow them to do so.

    In terms of managing their data collection in a more sophisticated manner, it’s a really a function of IT to provide the proper data to marketing which has business value. The challenge that tool like GTM has introduced into the market (because of its huge adoption) is that marketers don’t think that they need to rely on IT to collect data. They do. Case in point –> The GA API requires a string for Custom Dimension fields. If you don’t transpose an array into a string, you’ll end up creating an absurd number of tags. That’s not an elegant solution, or even required.

    I hope that my feedback on your article is received well. I am not trying to troll. Rather, I am trying to add my experience with tools that you are discussing to broaden the conversation in a professional and constructive way.

    Best,

    Yehoshua

    • 2

      Yehoshua, are you kidding? That’s not trolling… that’s AMAZING feedback. Absolutely love the feedback and expertise you’re sharing with our audience.

      Note: I did have the indexes set correctly on the data passed for Content Groups but it did NOT work when it was not in the correct order.

      Thanks again!

  2. 3

This site uses Akismet to reduce spam. Learn how your comment data is processed.