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.

NEW! 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.

GOOGLE TAG MANAGER CONSULTING

Get your own tag manager! Click for information on my Google Tag Manager consulting service.

Comments:5

  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.

Leave a Reply

Your email address will not be published.