How to send Author & Category from WordPress to Analytics Content Groups

 

Send Author & Category information from your WordPress blog posts to Google Analytics.

Google Analytics recently released Content Groups to the world. In this post I will show you how to track the most popular Authors and Categories from your WordPress blog within your Google Analytics reports.

Content Groups allow you to group your existing sites pages in new ways, rather than being grouped by URL as they are traditionally, you can now group your pages on any attribute you wish, we will be using this feature to send the Author and Category information from our WordPress blog posts to GA and set up reports to view these new groups.

Why would we do this?

Sending this additional information about each blog post that is viewed is data you could never have seen within GA before, but obviously when deciding what to track on your website you are looking for Insights, and with that in mind, my motivation for tracking this information is…

A) Knowing which categories in our blog provide the most useful content, we can write more in those categories, or try and improve the content in our other categories.
B) Out of curiosity / competition (who’s posts get the most attention)

So along with the standard page view for each of our blog posts, we will also be sending the Author and Category of that blog post, for example…
Pageview/blog/5-trends-to-watch-in-2014/
Author: “Rachael Bilby”
Category: “Internet Marketing”

 

Step 1. Configuring Content Groups

Content Grouping

You will need to be an Administrator for your GA Profile (View), Click Admin, then “Content Grouping”

We then need to create 2 new Content Groups

Click the “Create New Content Grouping” button at the top.

Add in the title of your Group, in our case “Blog: Author”.. but you can call it anything you wish.

Just below the title is where you select the tracking method you wish to use, we will be tracking via the GA javascript code, so click the first option, “Group By Tracking Code”, you don’t need to change anything on this screen, just press “Done” at the bottom.

That’s the first group done, how simple was that!? Now do the same process again for the next “Category” group… this time title the group “Blog: Category”, but when clicking the tracking code implementation option, change the “Slot” option, to slot #2, as per the screenshot below.

Content Group: Slot

Step 2. Modify the GA WordPress Tracking Code

This is the slightly technical step that may require you to speak to your friendly web developer. What we do in this step is modify the Google Analytics tracking code within our WordPress theme and tell it to send some extra information about the post being viewed.

Where exactly your tracking code is within your wordpress installation will depend on how you have it configured, our installation was simple because the GA code is within our templates header.php file, however if you are using a plugin to output your GA tracking code, you may have to root around to find the appropriate code to change.

Here’s an example of what the start of your GA tracking code would look like using the Async version of GA’s tracking code…

var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-XXXXXX-X’]);
<?php
if (is_single()){
    echo “_gaq.push([‘_setPageGroup’, 1, ‘”.get_the_author().”‘]);\n”;
    $category = get_the_category();
    if ($category && !empty($category[0]->cat_name)){
        echo “_gaq.push([‘_setPageGroup’, 2, ‘”.$category[0]->cat_name.”‘]);\n”;
    }
}
?>
_gaq.push([‘_trackPageview’]);

What the code does…

if (is_single()){

Ensures that we are only looking at post pages, because we only want to track posts, not other pages such as generic content pages or the home page.

echo “_gaq.push([‘_setPageGroup’, 1, ‘”.get_the_author().”‘]);\n”;

this line is setting the value of our first content group to the Authors name.

$category = get_the_category();
if ($category && !empty($category[0]->cat_name)){
    echo “_gaq.push([‘_setPageGroup’, 2, ‘”.$category[0]->cat_name.”‘]);\n”;
}

this section is getting the first category from the list of categories this post is in (we can only track 1 category per post) and outputs the tracking code to track our 2nd content group, the category.

 

Step 3. Viewing your newly collected data

Once this new data has begun flowing in to your GA account, head over to your All Pages Report….  Behaviour -> Site Content -> All Pages

You will see a new option appear at the top of the report asking which content group you would like to see your pages grouped by. You should now see your Authors & Categories grouped together with useful on-site behavioural metrics.

Content Group: Report

the numbers are so low because I wrote this blog post just a few minutes after adding the code to our site :-)

 

Step 4. Create a Dashboard to easily see this information. (Optional)

Here’s a simple Dashboard you can install on your own profile, Click Here to install the Dashboard in GA.

Content Group - Dashboard

 

All Done! Now the important bit, use the insights this information gives you to make better decisions about what to blog about (and maybe invoke some inter-office competition)

 

 

 

12 thoughts on “How to send Author & Category from WordPress to Analytics Content Groups

  1. Hi Ross, thanks for the post, very helpful.
    I’ve just tried setting this up, I’m using the new Universal Analytics. When viewing my report its just saying not set. Is it likely that I just have to wait a little longer to view the data being tracked correctly?

    I’m a bit unsure how the setup above enables different authors to be tracked in the report, maybe I needed to do something extra not mentioned above?

    • Hi Matt,
      The individual authors are tracked when you add the PHP code to your wordpress template. The new data only comes in when people view the new pages, it doesn’t backdate, you should see some information in your reports now. How are you getting on?

    • Hi David, I don’t have the code for GTM right now as I haven’t implemented it using this method, I think you would need to store your custom dimensions in the dataLayer and then pull it out with an event in GTM.

  2. Thanks a lot for your explanation. It’s exactely what I needed to get more detalied info on my site’s usage. It would be great to learn how to do this setup for the new analytics.js code. Or at least, to get a validation of this code:
    cat_name)){
    echo “ga(‘set’, ‘contentGroup2’, ‘”.$category[0]->cat_name.”‘);\n”;
    }
    }
    ?>
    Sure, the number of the group will vary, depending on your selections.

    • Hi Corina P,

      I think your code sample is correct for any sites running Universal Analytics (analytics.js). I believe the full code implementation would be:

      ga(‘create’, ‘UA-XXXXXXXX-Y’, ‘example.com’);

      < ?php if (is_single()){ echo "ga('set', 'contentGroup1', '".get_the_author()."');"\n"; $category = get_the_category(); if ($category && !empty($category[0]->cat_name)){
      echo “ga(‘set’, ‘contentGroup2’, ‘”.$category[0]->cat_name.”‘);”\n”;

      }
      }
      ?>

      ga(‘send’, ‘pageview’);

      Try that and let us know if you have any problems.

      Thanks :-)

      • Hi Chris!
        I tried to implement your code but it doesn’t work. Problem is that this code doesn’t print anything in source code.

        I added it in my genesis theme using simple hooks. Normally PHP works fine by this way.

      • Hi Pete,

        If you want to email me using the format firstname.surname[at]highposition.com with more info/examples I’ll do my best to help resolve the problem.

        Thanks,

        Chris

  3. Hi Pete,
    I tried your version of the code, but it looks like there’s a code error. I tried validating the code with a PHP code checker and looks like there’s a problem with “\n”;
    If I find anything by myself I’ll be sure to come back and post it here.
    Thanks a lot!

  4. Hello,

    If anyone is having problems with the code not displaying the author name for the content grouping try wrapping the code inside the IF statement with the WHILE HAVE_POSTS() method as follows:

    < ?php if (is_single()){ while ( have_posts() ) : the_post(); echo "ga('set', 'contentGroup1', 'T:".get_the_author()."');\n"; $category = get_the_category(); if ($category && !empty($category[0]->cat_name)){
    echo “ga(‘set’, ‘contentGroup2’, ‘”.$category[0]->cat_name.”‘);\n”;
    }
    endwhile; // end of the loop.
    }
    ?>

    Thanks.

    Chris

  5. Hello Chris,

    when using the Genesis framework and adding the following code via the theme settings, PHP is not being parsed. The PHP code still remains visible in the source code, when accessing a website. Neither author nor category information are being passed on:

    (function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,’script’,’//www.google-analytics.com/analytics.js’,’ga’);

    ga(‘create’, ‘UA-xxxxxxxx-xx’, ‘auto’);
    cat_name)){
    echo “ga(‘set’, contentGroup2, ‘”.$category[0]->cat_name.”‘);n”;
    }
    }
    ?>
    ga(‘send’, ‘pageview’);

    Best regards,
    Alexander

Leave a Reply

Your email address will not be published. Required fields are marked *