Often times when analyzing a blog, people ask how their posts are performing. That’s easy right? Just run a report in Google Analytics by Page Title. Yes, that works, but what if you want to export that report for a presentation and find that the title of your posts read ‘Post Name | Blog Title’. Then you’re forced to do a little find and replace in Excel before you can place that table in your PPT. Sure, find and replace is not hard, but I’d rather skip that step once and for all.

In this post, we’ll take a look at how you can pass your post title into Google Analytics using Google Tag Manager. I don’t mind if my other pages like search results pass in the default <title> because I’ll use my Site Search reports for those metrics.

1. Create a custom variable

Log into Google Tag Manager (GTM) and create a new Custom JavaScript variable. I’m going to title it ‘Post Page Title’ because as I mentioned, I’m only concerned with my blog posts.

Next we’re going to write a couple lines of JavaScript to find the post title. All of my blog post titles contain the class post-title, so I’d to set my variable to grab the text within the tag that contains that class.

Some of you might be asking why we don’t just push this post title to the data layer. We can’t do that because pushing the post title variable to the data layer in the code will fire after the page load and our Page Title will be undefined.

Paste the following JavaScript below into your variable configuration. I’m using the getElementsByClassName because my post title has that class, but you could easily use another JavaScript method or class name to get this string.

function (){
title = document.getElementsByClassName('page-title')[0].innerHTML;
return title;

Your final variable should look like this:


2. Edit your Universal Analytics pageview tag

Go into your Universal analytics pageview tag that fires on every page. Mine is titled GA – Universal Analytics Pageview. Under the tag configuration, expand More Settings > Fields to Set. This is where we’ll pass in that custom variable to the title.

Set the Field Name to title and the Value to {{Post Page Title}}.

Now you might be asking, what if I’m viewing a page that doesn’t have that variable? For example, my homepage doesn’t have a tag with the class page-title. Luckily, Google is smart enough to fall back to the document.title if that’s the case.

Your final variable should look like this:


3. Test

First, you’ll want to make sure that the variable is populating with the post title. Look for the variable on Pageview as shown below.



Next, look at the Fields to Set property in your GA – Universal Analytics Pageview tag as shown below.



2 comments on “Create Custom Page Titles Using Google Tag Manager

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.