Classify your Blog Posts in Analytics Using Content Groupings


holy-grailToday we are going to go on a quest to find the scientifically-proven best blog post ever. We will do so with math.

We have a hypothesis: the best blog post ever must contain these five things:

A handful of tasteful images
A YouTube video
A length between 1200 and 1500 words
A concise title
Published on a Friday

On December 19th, Google Analytics introduced a new feature called Content Grouping. Long in the making, this feature allows analysts to combine common pages by way of Rules, URL/Title Extraction, or with a piece of JavaScript placed on those pages.

Then, just yesterday, Google Tag Manager introduced an integration with this piece of JavaScript, so that its built-in Analytics tags could define content groupings based on Rules/Macros.

And there was much rejoicing.

Our Quest

Screen Shot 2014-01-23 at 3.10.58 PMOn this journey, I will show you how to classify your blog posts inside Analytics, using Google Tag Manager, so that you can see which components of your blog posts lead to the lowest bounce rates, the greatest time-on-page, and ultimately the most conversions.

Thou wilst need jQuery for this demo, and Google Tag Manager.

(I really strongly recommend that you create a brand new View for this demo, so that if you decide you don’t like these Content Groupings you can just delete the View and not pollute your existing data! Currently there is no way to delete a content grouping once you create it!)

Step 1:

The first thing we’re going to do is log in to Google Analytics and define five content groups. Visit your Admin and look under View Settings for “Content Groups.”

The Groups will all use the “Tracking Code” configuration method. We will name the first group, “Blog Post Images“. Click “Group by Tracking Code”, select Index 1, click Done, and then click Save.

Screen Shot 2014-01-23 at 3.16.41 PM

Then, rinse and repeat for all content groupings, selecting the appropriate Index for each:

Content Grouping Name Select Index
Blog Post Images Index 1
Blog Post Videos Index 2
Blog Post Length Index 3
Blog Post Title Length Index 4
Blog Post Publish Day Index 5

Step 2:

Screen Shot 2014-01-23 at 3.37.07 PM
Now log in to Google Tag Manager and visit the container for your website. If you already have created a Universal Analytics built-in tag — or even the Traditional Analytics tag — you can use that one and skip the next paragraph.

Otherwise, we are going to create a new tag for “Universal Analytics (beta). Click on the “New” button and select “Tag”. Name the tag “Universal Analytics GATC – Blog Posts“. We need to give it the correct Tracking ID (it starts with “UA-“) from the tracking code, and choose “Page View” as the “Track Type”. We’ll also give it a Firing Rule for “All Pages” on our website, or we’ll create a rule which tells the tag to only fire on our blog… it’s up to you.

Now we’re going to take advantage of a brand new feature (added yesterday!) in Google Tag Manager which allows us to pass content groups through the built-in Analytics tag. Look under “More settings (optional)” for Content Groups, and add the following five Indexes and “Content Groups“. We’re going to be making a reference to some macros that we’ll define in a moment, using brackets {{}} to indicate a macro name.

Screen Shot 2014-01-23 at 7.22.51 PM

The plain text for those macros is below:

Index Content Group
1 {{blog post images}}
2 {{blog post videos}}
3 {{blog post length}}
4 {{blog post title length}}
5 {{blog post publish day}}

Click “Save” to save the tag.

Well done! Our quest now takes us into the strange and sometimes harrowing Land of JavaScript. But do not fear! There is light behind every shadow.

Step 3:

Screen Shot 2014-01-23 at 4.02.54 PM
This step is really five steps in one: we’re going to create a new macro for each of our new content groups. Macros are Google Tag Manager’s way of pulling information off of a page, to be used in Rules and Tags. These macros are going to search through each blog post and count the number of images, number of videos, blog length and title length, and set the day of the week the post was published!

Macro #1: blog post images

We are assuming that each blog post is contained in a <div> tag with an id of “content”. (This is common with WordPress blogs.) You will need to inspect your source to ensure that this is the case for your blog. Also, we are assuming that each of our blog images have a class defined with the name “wp-image”.

Create a new macro called “blog post images“, with “Macro Type” set to “Custom JavaScript”. Then insert the following JavaScript:

This script looks through all the images on your page, inside <div id=”content”> counting images whose class contains “wp-image”. It will then return an image count to our first Content Grouping.

Save this macro and get ready for number two.

Macro #2: blog post videos

Similarly, we’ll now count the number of YouTube videos on the page. Create a macro called “blog post videos“, with Macro Type “Custom JavaScript”. Add this code and save the macro:

Macro #3: blog post length

For length we are going to count the number of words in the plain text (non-HTML content) inside <div id=”content”>. Create a new macro called “blog post length” and add this Custom JavaScript:

Macro #4: blog post title length

The blog post title length macro is very similar. Here we’ll refer to the <title> element of the page, but you might choose the <h1> instead.

Macro #5: blog post publish day

Finally, our last macro, blog post publish day will return the day of the week that the post was published! For our demo, we are going to assume that your blog post URLs have the same format as LunaMetrics’ blog posts: a URL format like 2014/01/24/. Your regular expression will be different if your URLs are set up differently.

(If the publish date is not included in your URLs, you could write a different macro to scan the page for the post date… but each situation will be different and your mileage may vary.)

Step 4:

Screen Shot 2014-01-23 at 7.37.27 PMWe’re almost done. Now that we’ve created the tag and the five macros we need, we will create a new version and click “Publish”.

And now…

Our journey is almost over. Now we must wait. You can’t see content groups appear in the Real Time reports, and unfortunately your data will take a little while to populate in the new reports. For now our quest brings us pause, and reflection.

Screen Shot 2014-01-23 at 7.43.22 PMIn a few hours, we can visit the All Pages report, inside Behavior > Site Content. You’ll now see that our new Content Groupings are available as a new Primary Dimension!

Screen Shot 2014-01-23 at 7.45.29 PM

If our quest was successful, after a few hours, you’ll begin to see some data in these reports!

Screen Shot 2014-01-24 at 10.41.33 AM

Screen Shot 2014-01-24 at 10.41.57 AM

Screen Shot 2014-01-24 at 10.44.21 AM

Our journey comes to an end.

You can consider the new Content Groupings essentially to be a simplified form of Content Experiments. We can examine this data, look at bounce rates, % exit rates, conversion rates, and make decisions about how to formulate our blog posts: how long should our posts be, what days should we publish, and how many images and videos should we include.

And there was much rejoicing.

Alex Moore is Vice President of our Analytics & Insight practice. He started building websites in the late-90s, and has spent over ten years in agencies, focused on front-end development, SEO/SEM, and of course web analytics. He also leads trainings in Google Analytics and Tag Manager around the country. Alex received his master's degree in Dublin, where he explored the Irish coast with a furry dog and lots of pints.

  • I like the idea of Content Grouping. It helps bloggers in having better statistics of their blogs. It is important what draws visitors to a site so that you keep doing what works.

  • Hi Alex,

    Great post you’ve written about using the content grouping of Google Analytics in combination of Google Tag Manager. I will definitely take a look into it to see what kind of analysis you can do based on these values.

    I think that especially the length of the blogposts can give you great insights about what works best for your readers and how far goes their attention span.

  • I don’t even knokw how I ended up here, but I thought
    this post was good. I don’t know who you are
    but certainly you’re going to a famoujs blogger if you are not alread 😉 Cheers!

  • Hey, Thanks for this post. I tried making groups, wrote the macros and it was all fine until the 5 groups I made, all have the same urls in it and everything is similar. I passed an id on all my pages that I wanted to group and checked its value. The value is different for different groups, yet it shows the same urls in the group. Can you suggest something I should try? Also what is not set ?

  • Alex, amazing article (and JS code :-).

    Just wanted to ask why not use custom dimenions instead of page groups in some cases — thouse should allow you to filter/segment data…

  • Erez

    Thank you for this great post

  • Bobby

    Great stuff. How will this work if you paginate your posts? We have a lot of long posts that are split into 5-6 pages at 500 word per page.

  • Hi Alex,

    Great post. I’ve been waiting for something like this for ages!

    Just a quick question. Is there an option to compare posts published by month? For example: I’d like to compare post traffic published in July vs June.

    Awesome. Thanks.

  • Mohankumar Swaminathan

    Hi alex,

    Interesting post. When i login to latest GTM, i’m unable to see Macro in menu. can you help me as per latest UI?

    • Alex Moore

      Hi, Mohankumar. This post is a little old, as you can see. Macros are now called “Variables” in the new UI. Rules are now called “Triggers”. Everything else should function just as it did in the old UI. Thanks!

      • Mohankumar Swaminathan

        Thanks Alex.

  • Hi, great post.
    I was wondering how do I get the “Macro #5: blog post publish day” to work if my url isn’t like yours?
    I appreciate any help you can provide.

  • Shannon Smith

    This is an excellent post. I’m having a bit of an issue with the jquery selector. My blog does not use a div with and id=”content”. I have tried using the “article” element which my blog uses and also some classes but with no luck. My blog is here and chance you could help?

  • Duck

    Thanks Alex,
    Maybe you can help me with something here.
    I adjusted everything but when page loads everything is getting undefined, on the first click on the page, doesnt matter where and how I get all the right variables, anything you can think of causing this to be undefined on load but okay after a random click ?

    Thanks !

  • evethestara

    I use “Pageview” firing tag but it doesn’t track everything.
    If I use “Dom ready” firing tag, will it be better?

  • Hi there, I had to change $(“#content”).text(); —-> jQuery(“#content”).text(); in line 2 of the Word Count function code to get the macro to work.

Contact Us.

Follow Us



We'll get back to you
in ONE business day.
Our Locations
THE FOUNDRY [map] LunaMetrics

24 S. 18th Street
Suite 100

Pittsburgh, PA 15203


4115 N. Ravenswood
Suite 101
Chicago, IL 60613


2100 Manchester Rd.
Building C, Suite 1750
Wheaton, IL 60187