Track content groups in Google Analytics

August 22, 2015 2:08 pm
Published by
Leave your thoughts

Most websites have content groups of some sort – if you’re a travel company you might group content based on destination or style of travel, if you’re an eCommerce site you could group content by product line. Like most blogs the posts here are grouped by category and it would be nice to track the performance of each category in Google Analytics, here’s how… First up it’s important to mention that there’s a number of different ways to do this. You could simply let data flow in to your account as normal and use advanced segments, you could use a plugin like Google Analytics Premium by Yoast, or if your website has a very clear hierarchy you might be able to use the configuration controls built right in to the Google Analytics admin interface. content-grouping-configuration However if you have more advanced requirements and you like to get your hands dirty read on . . . To track content you need to ensure there is a mechanism for grouping it! Chances are this already exists based on how you’re categorising or tagging your posts. For example I simply use the post category for this blog to group content in Analytics (i.e. Digital Marketing, Web Design, Web Development etc.), but you could use tags or any other taxonomy your CMS provides for organising content. If for any reason you would like to group content based on criteria unrelated to how your content is organised on your site you could use a custom field to classify everything behind the scenes – just be aware that whatever taxonomy you decide to use it will be visible in your code. Before configuring your analytics account it’s a good idea to test the logic on your website first to ensure you can categorise your content to meet your requirements. Depending on your CMS there’s lots of different ways you could do this – here’s my WordPress implementation which checks for the category of a page and stores it in the $allCats variable.

<?php // if this is the homepage or a normal page (e.g. contact, about) then it's general) if ( is_home() || get_post_type() == 'page') { $allCats = 'general'; } // if this is a category or post page use the category slug elseif (is_category() || get_post_type() == 'post') { $categories = get_the_category(); foreach ($categories as $category) { $catName[] = $category->cat_name; } // implode the categories into a comma separated string as we only have 5 content group slots! $allCats = implode(', ', $catName); } // otherwise how did we get here?! // you could be more imaginative and use $_SERVER['REQUEST_URI'] here for debugging else { $allCats = 'error'; } ?>
This is a very basic example that covers all the page types used on my blog, if you have custom post types or other things going on you will have to accommodate it in your logic – but the simpler the better. It needs to be robust and future proof otherwise you’ll have to keep coming back and updating your code, or worse you’ll just end up with a lot of uncategorised content. For posts that belong to more than one category I still record these as a single group (whose name will be a comma separated list of category names) due to the fact that Google Analytics allows only 5 content groups in total. For example if you also wanted to track content by tag and author you would only have 2 groups left to use. I recommend testing your content grouping thoroughly before moving to analytics, there’s always edge cases or long forgotten pages that you may not have accounted for. Using the code above you can output the your category names to a HTML comment and then test on various pages.
<?php
echo '<!-- xyztest: ' . $allCats . '-->';
?>
Then use a tool like ScreamingFrog to crawl your entire site and check for the existence of the correct HTML comment. Once you’re happy you can add the GA snippet to your page. For Google Tag Manager you can use the dataLayer – don’t forget this needs to come before the GTM tag code:

<script> dataLayer = [{ 'pageGroup': '<?php echo $allCats ?>' }]; </script>
For Universal Analytics it will look something like so:

ga('set', 'contentGroup1', '<?php echo $allCats ?>');
Again this needs to be done before your initial pageview is sent ga('send', 'pageview'); Now you can head to Google Analytics to configure your content group. As always I highly recommend trialling this in a test view first – this is particularly important as you can’t delete content groups once created! In Google Analytics go to:
Admin > view > content grouping > new content grouping
admin-content-grouping Click new content group and give it a name – you will now be prompted to modify your tracking code – we’ve already done this using the snippets above. I recommend doing it this way because if you create a content group first and then abandon the whole idea you can’t delete the content group from GA – I mentioned that right? admin-content-groups For Google Tag Manager users there’s a couple of extra steps to get your data from the data layer so load up GTM.
click on your container name > variables > user-defined variables > new
You want to create a Data Layer variable – as above I used the name pageGroup for the dataLayer variable. gtm-config
now click tags > your universal tag > configure tag > content groups
gtm-content-grouping-tag Here you want to use your dataLayer variable as content group index number 1 which matches the index number we used in Google Analytics. . . . and you’re done! There’s a fair bit of setup, but if you have a sound logic to categorise your content you can sit back and watch the data flow in to your account without any further work. To view your content groups in action head to your analytics reports:
behavior > site content > all pages
analytics-content-grouping Then you can begin to track and analyse your data based on content category: How much traffic does our bathroom renovation content bring to the site? How does kitchen renovation convert compared to bathroom renovation? Which of our content groups has the highest bounce rate or the longest time on page and why might that be? There are heaps of interesting insights you can gain from tracking your content in aggregate. One final thing to mention is that you might tear your hair out if you encounter a significant amount of traffic categorised as (not set). This isn’t necessarily a problem with your setup/configuration – it might well be because of ghost referrals or virtual pageviews. Essentially this traffic never ‘visits’ your site so it doesn’t get categorised, there’s more info on how to deal with ghost referrals here, and you can always update your categorisation logic to deal with virtual pageveiews.

Categorised in: ,

This post was written by WillyNilly

Leave a Reply

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