How to setup Content Grouping in Google Analytics for your WordPress site

Content Grouping in Google Analytics is a really powerful feature that allows you group your pages according to your own definition in order to analyse them properly. There are a lot of people out there using WordPress as a platform and today we’re going to define our Content Groups and make sure that we send all the data we need to Google Analytics using, well… Google Tag Manager of course.

What we want to achieve is basically to look at data in Google Analytics based on what kind of page type being display, such as:

  • Front Page
  • Blog Post
  • Author Page
  • Category Page
  • Etc

Have a look at this video about Content Grouping by the Google Analytics team to get an even better idea of the usages of this very nice to have feature.

But as with almost everything i Google Analytics, nothing comes for free, so grab a cup of coffee and let’s get this show on the run!

Pull data from WordPress into GTM dataLayer

First of all, we need to retrieve the data we need from WordPress. This is of course something you could do with any other CMS as well, but you’ll have to do your own research on how to retrieve the correct data in that case.

WordPress has a really good developer documentation available to us and we’re looking for something that’ll tell us what kind of page it is that’s being displayed to the user. It comes as no surprise that the documentation they have about their conditional tags are pretty solid, and just what we’re looking for.

Based on this information, we can write PHP code (the language most part of WordPress is written in) to retrieve this, and then combine it with some JavaScript in the end in order to pass the data on to the dataLayer.

I’ve got a copy/paste code for you right here that will put most of your pages into a specified category. If you’re using other page types in WordPress, extend this code accordingly. You’ll notice that I have changed the code somewhat from the original piece on Stackexchange.

function gtm_posttype() {
    global $wp_query;
    $cat = '(not set)';

    if ( $wp_query->is_page ) {
        $cat = is_front_page() ? 'Front' : 'Page';
    } elseif ( $wp_query->is_home ) {
        $cat = 'Home Page';
    } elseif ( $wp_query->is_single ) {
        $cat = ( $wp_query->is_attachment ) ? 'attachment' : 'Blog Post';
    } elseif ( $wp_query->is_category ) {
        $cat = 'Category';
    } elseif ( $wp_query->is_tag ) {
        $cat = 'Tag';
    } elseif ( $wp_query->is_tax ) {
        $cat = 'Taxonomy';
    } elseif ( $wp_query->is_archive ) {
        if ( $wp_query->is_day ) {
            $cat = 'Day Archive';
        } elseif ( $wp_query->is_month ) {
            $cat = 'Month Archive';
        } elseif ( $wp_query->is_year ) {
            $cat = 'Year Archive';
        } elseif ( $wp_query->is_author ) {
            $cat = 'Author';
        } else {
            $cat = 'Archive';
    } elseif ( $wp_query->is_search ) {
        $cat = 'Search';
    } elseif ( $wp_query->is_404 ) {
        $cat = '404 Page';

    return $cat;

    window.dataLayer = window.dataLayer || [];
    dataLayer.push({'pageType': '<?php echo gtm_posttype(); ?>'})

I’ve outputted this code within the <head> section of all pages. This makes sure that all information is present in the dataLayer when Google Tag Manager initiates.


Setting up Content Grouping in Google Analytics

With the data in place in the dataLayer, we need to start preparing Google Analytics to receive the information as well. We’ll find the Content Grouping setting under the Admin tab.



Just hit New Content Grouping and you’ll be presented with a variety of option to configure your brand new Content Group.


The Content Grouping in Google Analytics can be defined with a few different settings, that can be combined if needed.

  • Group by Tracking Code
  • Group using Extraction
  • Group using Rule Definitions

In our case, we’re going with the first one; Group by Tracking Code. And it should come as no surprise to you that we are going to use Google Tag Manager for this.

So basically, just enable the first rule, and be aware of what Index you are using. If this is your only Content Group, it will default to ‘1’, as shown above.

That’s it for setting Google Analytics up, now…

Configuring Google Tag Manager for Content Grouping

The code above will push the actual page type into the dataLayer where we can pick up the pageType, so all we need to do is define a Variable in GTM to pull this info from the dataLayer.

Google Tag Manager dataLayer Variable for WordPress Page Types

Just create a Variable, select type as Data Layer Variable and set the Data Layer Variable Name to pageType as defined by the code we wrote above.

With that data in a Variable, we just need to pass that data along with the Page View that we’re already sending to Google Analytics.


In the Universal Analytics Tag just go to More Settings and then Content Groups. Add the Content Group Index (1 in our case) and the Variable defined recently under the Content Group field as outlined above.

The result?

Content Groups inside Google Analytics

Looking at e.g. the All Pages report in GA while changing the Primary Dimension to our own Content Group we will now be able to look at the data from a “what type of page is it” view.

Imagine applying this to other kind of data as well. Let’s say you have a lot of authors on your site. With Content Grouping (with or without WordPress) you could group all posts by every individual author and start comparing them to each other.

How do you intend to use Content Grouping on your site?