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:
- Content Title – So that I can look at articles like “how to's” and other commonly titled articles.
- 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.
- Content Author – So that I can view our guest authors and see which ones are driving engagement and conversions.
- 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:
Within Content Grouping, you'll want to add each 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.)
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.
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.
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.
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:
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:
- Select Add a Tag
- Select Universal Analytics, name your tag in the top left and enter your UA-XXXXX-XX id
- Now tell the tag when to fire now by clicking on Triggering and selecting all pages.
- 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.
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:
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:
- The WordPress page is requested.
- The WordPress Plugin displays the dataLayer.
- Google Tag Manager script executes and passes the dataLayer from WordPress to Google Tag Manager.
- Google Tag Manager variables are identified in the dataLayer.
- Google Tag Manager triggers are identified based on the variables.
- Google Tag Manager fires specific tags based on the triggers.
- 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.
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:
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:
Here's an example of one of those triggers for our category for Content Marketing:
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:
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”):
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!