When analyzing a blog with a lot of posts, it can sometimes get daunting to cut through that data using just the post titles. With the help from Google Tag Manager and a little extra code, sending custom dimensions to Google Analytics allows you to easily gain more insights without manual work.

In this post we’ll understand how to pass WordPress values to the data layer and pass them to Google Analytics.

Note: There are WordPress plugins that accomplish this, but this is a very good basic exercise to better understand WordPress functions and get your hands dirty with data layer variables and custom dimensions. This is a very good skill to have.

1. Define the requirements and KPIs

Before diving into the code, let’s first understand our business requirements. In this scenario, let’s imagine we manage a blog for a company with multiple business units, and each BU is responsible for a single category. Each BU also has multiple authors.

Here are the requirements:

  1. Understand how each business unit’s content is performing in regards to general volume, engagement and traffic it’s driving to the website.
  2. Understand how each author’s content is performing in regards to the same criteria in the first requirement.
  3. Break each post down by date for month over month analysis.

Here are our KPIs:

Page views, time on page and entrances for each post broken down by the following:

  • Publish date
  • Post category
  • Post author

2. Push the publish date, category and author to the data layer

This step will require us to go into the theme files to add code that will send our metrics to the data layer. Since we’re going to send this data on page load of a single post, we’ll want to open the single.php file in our theme.

Below is the code that will get us post namedate, author and category. The category requires a little more code because it returns an array since there can be more than one category assigned to a post.

I’m also passing in the post name because I’d like to easily export this report from GA and share it with others. More details on how that works can be found in another post of mine. The rest is pretty straightforward if you’ve ever modified a WordPress theme

<script>
 dataLayer.push({
'date': '<php echo get_the_date(); ?>',
'author': '<?php the_author_meta( 'ID' ); ?>',
'postname': '<?php the_title(); ?>', 
<?php
$categories = get_the_category();
$separator = ', ';
$output = '';
if ( ! empty( $categories ) ) {
foreach( $categories as $category ) {
$output .= esc_html( $category-&amp;gt;name ) . $separator;
}
echo "'categories':'" . trim($output, $separator) . "'";
}
?> 
}); 
</script>

3. Create the custom dimensions in Google Analytics

Go into Admin >  Property > Custom Definitions > Custom Dimensions. Create a custom dimension in Google Analytics for the Date, Author, Categories.  Each of the dimensions should have a scope of hit.

 

Take note of the index for each of these dimensions as we’ll need to use them in the next step.

Screen Shot 2016-02-02 at 10.00.05 PM

 

4. Create data layer variables for each custom dimension

In GTM, create a new data layer variable for the date, author and categories. The data layer variable name will correspond with the names we set in step 2. The custom dimensions we created in the previous steps will be passed into these data layer variables and then sent to GA on page load.

Screen Shot 2015-11-08 at 12.49.26 PM Screen Shot 2015-11-08 at 12.49.42 PM Screen Shot 2015-11-08 at 12.50.17 PM

5. Send the custom dimensions to GA on page load

Create a new tag and add the following dimensions as shown below making sure that the index corresponds with the indices from step 3. Fire the tag on DOM ready because that’s when the data layer variables we created in the previous step will be populated.

Screen Shot 2015-11-08 at 1.00.31 PM

5. Create a custom report in Google Analytics

Now we have all the data we need to perform our analysis, and it’s time to create the report in Google Analytics. Looking back at our requirements and KPIs, we’ll want to create a report that allows us to break our posts down by the custom dimensions with page views, time on page and entrances. Create a custom report as shown in the screenshot below.

Screen Shot 2015-11-08 at 1.28.30 PM

 

I added a filter for the Page to contain ’20’. All of my posts follow the same structure where it starts with the year, so this will get rid of other pages like About, Homepage and Contact.