Track Popular Blog Tags and Categories in Google Analytics

If you have a blog or work with content, you will likely want to know which type of posts are viewed most frequently by your readers. Google Analytics provides a lot of options for tracking content: it will show you what specific posts were the most viewed in the Top Pages report, allows you to group content together with the Content Grouping function, and lets you add attributes like author or post date into custom dimensions.

The problem arises in the common case where you have multiple categories or tags that apply to a single post, since both Content Grouping and Custom Dimensions set just one value per category. Therefore this tutorial will show you the setup steps and scripts to produce an Event tracking report showing the most popular blog categories and tags on your site.

For a quicker start, in the final part of the post I’ve provided a downloadable Google Tag Manager (GTM) container that includes all the tags and scripts referenced in this post.

API CONNECTOR ADD-ON FOR GOOGLE SHEETS

Check out my new API Connector Add-on to import data from thousands of platforms (e.g. Shopify, Harvest, Mailchimp, ActiveCampaign, VWO, YouTube, etc.) directly into Google Sheets.

STEP 1: COLLECT EACH BLOG POST CATEGORY INTO AN ARRAY

This is the most varied step, so we’ll start here: we need to gather together all the blog categories into an array. To make this as generalizable as possible, I’ll use WordPress for the example, and JavaScript to access the necessary values. This way you can do everything through Google Tag Manager, and don’t need to change any of your backend site code (though if you’re a developer you may prefer to, and can do it that way instead).

WordPress often lists all the blog categories within an <article> tag near the top of the page. You can click Ctrl-U to view the page source and check if this is the case for your blog. The categories are all class names prefaced with the word ‘category’, while tags are all prefaced with the word ‘tag’.

popular-categories-img1

For this example we’ll extract the list of categories, so we write a script to loop through the list of classes and push all the ones prefaced with “category” into a new array. Extracting all the tags would be done exactly the same way, just changing the text from “category” to “tag” in the script.

If your WordPress blog shows the list of categories in an <article> tag as shown above (many do), you can just copy and paste the above script into a Custom JavaScript variable in Google Tag Manager.

popular-categories-img2
If it doesn’t, or you’re not using WordPress as your blogging platform, you’ll need to inspect your site and write your own code to pass the categories into a GTM Variable containing an array. Once you’ve done that, the rest of the steps are the same.

The output in the GTM Preview pane will look like this:popular-categories-img3

STEP 2: PUSH THE ARRAY VALUES INTO THE DATA LAYER

Create a custom HTML tag with the following script. It loops through the content category array and for each element within it, pushes the value and a GTM event into the data layer.

Set it to fire on All Pages – DOM Ready. This will delay the push slightly to ensure the category values are available in time.

popular-categories-img4

STEP 3: CREATE A TRIGGER

Set up a trigger based on the ‘contentTracker’ event that gets pushed into the data layer by the custom HTML tag. This is done at Triggers > New > Custom Event, Event name = contentTracker.

popular-categories-img5

STEP 4: ADD A DATA LAYER VARIABLE

The custom HTML tag is also setting a data layer variable with the key called ‘contentCategory’, so set up a data layer variable to read it:

popular-categories-img6

STEP 5: SET UP THE EVENT TAG

We now have all the components: the blog categories in a data layer variable, and a trigger to send them off to the Google Analytics servers. The final step is to put it all together into an event tag. Navigate to Tags > New > Universal Analytics and make the following selections:

Track Type = Event
Category = Content Categories
Action = {DL – Content Category}}
Label = {{Page Path}}
Non-interaction Hit = True (this will stop the events from affecting bounce rate)
Firing Trigger = Event – ContentTracker

Fill in your Google Analytics tracking ID, and you’re done.

popular-categories-img7

CONCLUSION

After completing the above steps, you can navigate to Behavior > Events > Top Events and drill into Category = Content Categories to see a list of all your blog categories or tags, ranked in order of highest views.

popular-categories-img8

Finally, to provide a quick start, I’ve compiled all the scripts and tags above into a downloadable container you can import into your own GTM account. Right-click the link and click ‘Save link as’ to save them to your own computer: GTM Blog Categories Container

Once you’ve downloaded the container, in Google Tag Manager, navigate to Admin > Container and click Import Container. When you choose to import the container, put it into a new workspace and choose the options Merge and then Rename conflicting tags, triggers, and variables. This will ensure that nothing affects your existing GTM setup.

Comments:13

  1. I was just about to implement google content group when I found this article. Are there any downsides of using events instead of content groups?

    1. With content groups, you can only assign a page to a single category. With this method, you can assign a page to multiple categories. If you only need to assign a page to a single category, just use content groups — it is super easy to set up and doesn’t require any custom code.

  2. Hi Ana. Thanks for the prompt reply. For some reason, I didn’t get an email notification. Just to be clear, if I have one blog post tagged with category-paid-search and category-reporting. If it got 3 visits, it would look like your report with each category having 3 visits?

    1. Right, your report would have one line for category-paid-search showing 3 visits, and a second line for category-reporting showing 3 visits.

  3. Hey Ana,
    with this setup, I am trying to see within each category what people are clicking on… Thus this would mean checking for another type of event within another event…
    See what I mean?
    Is there a way to cross these two together?

    1. Hi Julien!I’m not totally sure what you’re looking for, but in general think of what you want the output to look like and then work backwards.
      Do you want something like this?
      Event category = {{Content Category}} //e.g. Marketing
      Event action = Clicked Link

      If so, then you’d just need to identify which content category they’re in and pass that into the category. Typically you can identify the content category via the URL, the data layer, or some text on the page.
      Please let me know if that makes sense.

  4. Hi – thanks for this article and step by step instructions. I wanted to confirm I understand what you meant when you said the following, “After completing the above steps, you can navigate to Behavior > Events > Top Events and drill into Category = Content Categories to see a list of all your blog categories or tags, ranked in order of highest views.” I thought that meant that I could see in Google Analytics the total number of page views that contained a particular tag that is in use on our site. For example, we have articles instead of blog posts. We tag our articles with keywords like lighting, video, audio. We want to see in Google Analytics how many times any article with the tag “audio” has been viewed. I thought that is what you meant in your last sentence for implementing this solution. We had our 3rd party web team implement the solution but instead of showing us the total number of page views for that tag we are getting the total number of articles that are live on our site that uses that tag. I am trying to figure out if I misread the end result of the solution (total page views for the tag) or if our implementation is not correct.

    1. Hi Rebecca, thanks for the message. You’ve understood this article correctly. The tracking should show you the number of times an article with that tag was viewed (i.e. the number of pageviews for that tag).

      I checked your site and it looks like tracking on your site is implemented correctly. I just navigated to a couple of articles on your domain, and in the GA debugging tool, I saw 1 event fire for each tag that I viewed.

      How are you determining that event counts refer to ‘the total number of articles that are live on our site that uses that tag’? From what I can tell, that’s not the case here.

Leave a Reply

Your email address will not be published.