Adding Google Analytics Content Group Tracking to a WordPress Theme using Google Tag Manager

By Megalytic Staff - June 11, 2015

The marketing team just told you they need Google Analytics configured to track statistics based on the type of content users are consuming. Specifically, they want to know if visitors are watching videos or if they’re reading blog articles. Your answer will help inform content marketing efforts companywide—either bumping up the video investment or dedicating more time toward writing and researching authoritative articles.
You are the go-to person for setting up Google Analytics tracking, so you know the right way to do this, and the best way to get the marketing team their answer, is by using Content Grouping. You do have some concerns, though. The website content that marketing wants to track is all in WordPress; the theme developers are not going to be too happy with you modifying their code to introduce the Google Analytics tracking code needed for Content Grouping.
Luckily, there is a solution!
Combine a WordPress plugin that captures content attributes with Google Tag Manager (GTM) to add the necessary tracking code without modifying any of the WordPress theme code.
This posts walks through how we used this method to successfully add Content Grouping to the Megalytic Support website.

 

Blog Image Google Analytics Tag Manager and WordPress

 

Setting Up a Content Grouping in Google Analytics

As mentioned, in this post we are going to show how we added Content Grouping to the Megalytic Support website, which is hosted in WordPress and uses the KnowHow theme.

The first step in setting up Content Grouping is to go into the Admin section of the Google Analytics account where you want to track progress. Click on “Content Grouping” and then select “Group by Tracking Code.” We’ll be using this group to track the type of Support page viewed by the user, so we’ll name this Content Group “Support Type.”

 

Creating a Content Group in Google Analytics

 

The code that has been circled in the image will be used to track the Content Type.

ga(‘set’, ‘contentGroup1’, ‘My Group Name’)

Grouping WordPress Content

WordPress provides a variety of content attributes that can be used to define Content Groups in Google Analytics. For example when you create content in WordPress you can select a post type (e.g., post, page or any custom post type). Furthermore, within a post, you can define a variety of attributes, such as categories and tags.

 

WordPress Post Categories

 

In the above image, you can see that the post type being edited here supports categories. We also see that this particular post has been categorized with “Branding,” “Formatting,” and “Images.” A simple Content Grouping can be created using the category attribute, where we can group all content as either a Video (if the Category of “Video” has been selected), or an Article if the “Video” category does not apply.

Of course, the exact custom posts, categories and tags available on your WordPress installation will depend on the WordPress theme you’re using. However, regardless of theme, there will always be similar attributes that you can take advantage of to define Content Groups.

To track Content Groups based on these attributes, the Google Analytics tracking code needs to be able to access these attributes. To make the attributes available, they need to be stored in a data layer.

Google Tag Manager can be used to read the data layer and use the attributes to fire the appropriate Google Analytics tracking code to define the Content Groups.

So, it all starts with the data layer. Luckily, there is a plugin that we can use to automatically set up the data layer to contain the attributes needed by Google Analytics for Content Grouping.

Install a Plugin for Google Tag Manager

The plugin we use is DuracellTomi’s Google Tag Manager for WordPress. To install it, log in to WordPress as an administrator. Then, click on Plugins > Add New. In the Search Bar enter “DuracellTomi” and click “Search.” This will bring up the “DuracellTomi's Google Tag Manager for WordPress” plugin.

 

DuracellTomi GTM Plugin for WordPress

 

Click on “Install Now” to install the plugin. After installation is complete, click on “Activate Plugin” to turn on the plugin to begin using it.

Once installed, you’ll need to enter your Google Tag Manager ID. We recommend creating a new GTM account for your WordPress site to use with this plugin. Once your GTM account is set up, enter the GTM ID in the plugin.

 

Enter the Google Tag Manager ID

 

The Data Layer

Once the plugin is installed, we can configure the data layer. Click on the tab “Basic data” and you will see the attributes needed for Content Grouping. Notice the second item in the list: “Category list of current post/archive.”

When this box is checked (as it is by default), the plugin stores the “Category list” in the “dataLayer”—the variable where data is stored by default. That is great news for us! Because we implement Content Groups from the Category data, we do not need to do any custom programming to get the data we need into the dataLayer.

 

Basic Data for the GTM Data Layer

 

Once the plugin is configured for our needs, we can check that is working properly by opening the console in our browser and entering “dataLayer.”

In the image below, you can see that for some video content, the categories stored in the data layer include “video”. In fact, as you can see below, all the categories, defined for the page in WordPress, are not stored in the dataLayer.pageCategory array.

 

Content Group Video - Data Layer Contents

 

Accessing The Data Layer from Google Tag Manager

Next, we are going to use GTM to access that data layer and set the content type for each particular page as either “Support-Article” or “Support-Video.” “Support-Article” will be the default, and we will use “Support-Video” only when the pageCategory array contains the category “video.”

We assume that you already have GTM set up, and have created a basic tag for Google Analytics tracking. If not, please set this up as described in Google’s help article: Install Google Analytics via Google Tag Manager.

We are going to add Content Groups to the basic tag that you have set up. But, to do that, first we need to set up a couple of GTM variables that will help us derive the correct content type from the data layer.

Calculate the Content Type with a Custom JavaScript Variable

As discussed above, the data layer is storing the categories in the pageCategory variable, as an array – for example:

["admin", "connections", "video"]

So, we need to be able to access the pageCategory array from inside GTM. To do that, we create a “Data Layer Variable” inside of GTM that will hold the value of pageCategory. Start by selecting “Variables” on left rail of GTM, scrolling down to “User-Defined Variables” and clicking “New.”

 

New Variable in Google Tag Manager

 

We give the new variable the name “categoryArray,” since it holds the array of categories. We select the type “Data Layer Variable,” and populate it with the data from the data layer variable pageCategory that the plugin has recorded.

 

Pulling Data Layer Variable in to GTM

 

The reason for doing this is that now we can reference this variable as from JavaScript run by GTM.

Remember, we want to define the content type to be “Support-Video” if the category “video” appears in the array. That calculation is performed by the JavaScript code below that returns “video” if the article should be classified as a video and “article” otherwise.


var categoryArr = ;
if (categoryArr == "") {
return "none";
}
var contentType = "article";
for (i = 0; i < categoryArr.length; i++) {
if (categoryArr[i] == "video") {
contentType = "video";
break;
}
}
return contentType;

This code simply scans the array from looking for “video.” To put this code to work inside of GTM, we create a Custom JavaScript variable similar to how we created the data layer variable. After clicking on “Variable” and “New,” then populate the fields by naming the variable “getContentType,” selecting “Custom JavaScript” as the variable type, and inserting the code that calculates the content type.

 

Custom JavaScript Variable in Google Tag Manager

 

Lastly, we need to create a table that maps the return values from getContentType to the values that will be used in our Content Grouping. For this, we create another variable and name it “contentLookupTable” with type “Lookup Table,” and the Input Variable set to the Custom JavaScript variable just defined – .

 

Google Tag Manager Lookup Table

 

In addition, we add rows to the Lookup Table to map each possible input value (the return values from ) to a content type. In this case, the mapping is very simple – “video” is mapped to “Support-Video” and “article” is mapped to “Support-Article.”

Modify the Page Tracking Tag to Add the Content Group

At this point, we have all the calculations needed to set the Content Group value. So, we can now modify the basic page tracking tag to include the Content Group.

To do that, start by editing the tag and clicking on the pencil next to “Configure Tag.”

 

Editing a Tag in Google Tag Manager

 

From there, click on “More settings” to open up additional tracking options.

 

Google Tag Manager - More Settings

 

Next, click on the “Content Groups” option and click “+ Add Content Group.”

 

Google Tag Manager - Selecting GA Content Groups

 

At this point, we simply map the index of the Content Group (1 – 5) to the lookup variable that returns the correct value. In this case the index is 1 because when we set up the Content Group we assigned it to that index number.

 

Google Tag Manager - Setting a Content Grouping

 

Save these changes to the tag, and we are done. When we publish the updated GTM configuration, we will start tracking the Content Group named “Support Type.”

Viewing the Content Group in Google Analytics

We give our new Content Group tags in GTM a little time (6 – 12 hours) for data to start showing up in Google Analytics. Then, go to the Pages standard report (Behavior > Site Content > All Pages). Select the Content Group “Support Type” from the Primary Dimension selector directly above the table.

 

Google Analytics Content Groups in Pages Report

 

Here, we can see that the values “Support-Article” and “Support-Video” are starting to show up in the report. From this, we can tell that the configuration of the plugin and GTM to assign the Content Group values are working correctly.

Conclusion

In this post, we’ve show how to add Content Group tracking to a WordPress website without having to do any modifications to the WordPress code. We used the WordPress plugin DuracellTomi’s Google Tag Manager for WordPress to save information about our content (i.e, the post categories) into a data layer that can be accessed by Google Tag Manager (GTM). We then configured GTM to read this data layer and fire the appropriate Google Analytics tracking code to set the Content Group value for each page. Finally, we validated the setup by looking at the All Pages report in Google Analytics and confirming that data about the Content Group was flowing into the report.

Content Offer

An introductory guide to inbound marketing

Get to grips with marketing in the digital age

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat.

Download Guide
Comments

We promise that we won't SPAM you.