WordPress – Add Google Analytics Content Grouping with Google Analyticator – Post Category

Google Analytics content grouping is a feature that really helps to break down site usage in a new level that can be super powerful.  But the point of this post isn’t to promote Google Analytics Content Grouping – you can read more about Content Groups from Google if you need to know more.  The focus of this article is around how to simply add content grouping to a WordPress website.

A Plethora of Options

The first, and possibly most simple solution, would be to use the rule builder within Google Analytics itself to group together content.  However, it’s common that most sites don’t have a standard pattern that is consistent enough to write a rule or regular expression that will create the group sufficiently enough.  That is the reason for going one step further with writing some code to add the appropriate tag to your Google Analytics requests.

I’ve seen many solutions that utilize Google Tag Manager to implement this, and that is something that I have chosen to NOT use.  Google Tag Manager is powerful and helpful, but honestly I have never utilized it for several reasons.  The two main ones are that I am a developer and so thus I prefer to just put the code in place directly as opposed to utilize a 3rd party tool to do something I can do.  The other issue I see with GTM is that it is a 3rd party tool – the more code you try to load that doesn’t come from your server directly the more potential issues you introduce in terms of site load time, possible server outages, etc.

Another way to approach this problem is to hard-code Content Grouping code into the site, but if you are like most WordPress admins that use a plugin to implement Google Analytics you’ll need to send the Content Grouping request to Google Analytics via another request – typically an event request.  This isn’t a bad solution, but is inflating the requests you are sending and it’s not really all that necessary when you could pass this into the standard page view request.  That is what the solution below will do.

Requirements for the Solution

 but I’ll focus on the method I’m using, which includes utilizing the Google Analyticator WordPress Plugin to implement Google Analytics.  There may be other plugins that will work with this solution – it all depends on if you can inject additional tracking parameters through the plugin or not.

You also need to be able to modify your functions.php file.  So if you don’t have access to your codebase or feel comfortable modifying your site’s code, then this is not the solution you should use.

Setup Content Grouping in Google Analytics

The first step is to enable and setup content grouping within Google Analytics.

Configure Google Analytics Content Group
Configure the Google Analytics Content Grouping settings to create a content group. Google Analytics allows you to have 5 different content groups, but you can have sub-content/groups under each content group.
  • Login into your Google Analytics account
  • Select the property you want to setup Content Grouping for
  • Click on Admin at the top of the page
  • Under the View column, click on Content Grouping
  • Click the New Content Grouping button
  • Enter a name for this content group – I used Primary Category
  • Choose Enable Tracking Code.
  • Ensure the Enable switch is set to On.
  • Take note of what the Index number is.  If this is your first rule, it will be set to 1 by default, though you can change this to a different index if you so choose to.
  • Click Done.
  • Click Save.

Update functions.php within WordPress

Before I go too deep into this step, make sure you understand how to properly modify your theme within WordPress.  If you are modifying any files in a theme folder, such as the functions.php file, you should ensure you are utilizing a child-theme to allow future theme updates to be applied without breaking new features, such as this one.  I won’t go into detail on child-theming, but you can read about how to set it up in the WordPress Codex about Child Themeing.

I considered making this script into a plugin which would prevent the modifications to the functions.php file, but that seemed like overkill.  However if there becomes a large need for this I could put that together at some point.  Feel free to leave a comment if you have an opinion about this.

The purpose for adding a function to the functions.php file is to create a javascript variable in the <head> tag of the site that lists the categories that a post is listed under.  This javascript variable can then be accessed by the javascript code added below to the Google Analyticator plugin.

This new variable, wpCategories, is an array with two sub-arrays.  The first is the parent level categories a post is listed under, and the second sub-array is the child categories that a post is listed under.  It maybe wasn’t necessary to list all of these here and I could have instead only returned one category to be used in the tracking, but I choose to leave more data incase it could be useful someday in the future.  It’s still a tiny bit of code and shouldn’t show any sort of performance degradation.

So onto the code.  It’s a simple block of PHP code that shouldn’t need any modifications.  Just place this into the functions.php file, save it and upload it to your server.

Add Content Grouping Code to Google Analyticator

The last piece to the puzzle is to use this newly created javascript variable with the categories to populate the Content Grouping in the request to Google Analytics.

The Google Analyticator plugin utilized in this example already offers a feature to inject additional tracking options into the request.  So utilizing that field in the WordPress Admin we add some conditional javascript code that ensures the above variable is set.  Keep in mind that this code is only applicable on a post, but the code is designed to not cause issues on non-post pages on the site.

Update the Google Analyticator settings
Insert conditional javascript to insert the Content Grouping tag into the Google Analytics request.

This script first looks to see if there is a parent category defined.  If so, it utilizes that category for the data sent to Google Analytics.  If there is not a parent category set, then it utilizes the first child category.  If there isn’t a child category defined, the Content Group is not added to the Google Analytics request.

One thing to point out is that in a case where you have multiple parent or children categories, I am unsure of how WordPress sorts those.  I’m assuming it is either alphabetically or numerically based on the ID given to the category.  I’m assuming it’s the later, and if so the the order is really based on when a category was added to WordPress.

There is one item in this script that you may need to update.  Remember back when we were setting up the Content Group in Google Analytics how I said you should take note of the Index of the Content Group that we created?  Well this is where that becomes important.  If you are using Index 1 then this script is good to go as is.  If not, update the index number on the 3rd to the last line to reflect the index number you have set in Google Analytics.

Testing the Code

The last step after saving our two blocks of code is to test that it worked.  I like to use Chrome as my browser, and a great plugin called Omnibug is an AWESOME tool to get instant proof that this worked.  Omnibug is available for Firefox as well as long as you have Firebug installed.

Keep in mind of any rules you have set for Google Analyticator – I have it set to not track when logged into the site.  So to do testing I either log out or use an incognito window.

With the developer tools open, load your post page.  In the developer tools, choose the Onmibug tab and expand the last request.  Look for the Hit Type – we are looking for this to be pageview.  Oftentimes you’ll also see a hit listed as dc – this stands for double click and is related to paid search.  We can ignore that hit for now.  Find the last hit listed as a pageview.

Look at the bottom of this request in the Other section for the property labeled cg1 (or other number depending on the index you are using in Google Analytics for this Content Group) – the value will be the category name that is being passed to Google Analytics as the Content Group label for this pageview.

Use Omnibug to test Google Analytics requests
Test that the Content Grouping code is working correctly with developer tools and Omnibug.

If you choose to not use Omnibug, you could also wait for the data to show up in Google Analytics and trust that it is being sent from the site correctly.  I like to test early and ensure that I’m confident that changes I made to the site worked though.

I also discuss other methods, some that do not require plugins, to test your Google Analytics traffic in this article about implementing features of Google Analytics.


Content Grouping in Google Analytics is a great addition to the reporting of any website.  Utilizing an already great WordPress plugin we were able to extend it’s base functionality to also include Content Grouping with only two small blocks of code.

You can also consider this as the basis for other possibilities.  It wouldn’t take much effort to modify these two code examples to create content groups under different premisses.

I hope this helps someone out there enhance their reporting capabilities!