While tracking WordPress pageviews in Google Analytics is pretty easy to accomplish, another interesting metric for many websites is to track author statistics. Namely, how many pageviews is each of your authors getting. One of the new features in Google Universal Analytics is custom dimensions. You can easily use custom dimensions to add the author’s name to each of your pageviews.

To track a custom dimension for each of your pageviews, you first need to setup the custom dimension in the Google Analytics admin console and save the index number so that you can add it to your tracking script.

First, go to the Google Analytics admin menu:

Google Analytics admin menu

Next, make sure that you choose the property where you wish to create a custom dimension. Then choose Custom Definitions > Custom Dimensions.

Google Analytics custom dimensions

Create a new custom dimension and name it “Author”. For tracking authors on each individual pageview, you’ll want to use a scope of “Hit.” You can read more about scope here. You should also note that Google Analytics limits you to just 20 total custom dimensions per property, so choose carefully.

Now you need the code snippet to send dimension values, which contains the numeric index that associates your data with the correct custom dimension in Google Analytics. In our case, “Author” is dimension1.

var dimensionValue = 'SOME_DIMENSION_VALUE';
ga('set', 'dimension1', dimensionValue);

Once you have the custom dimension setup in Google Analytics, it is time to add it to your tracking script. Here is what a default tracking script looks like in Universal Analytics. Obviously each property needs to set it’s own property ID.

<script>
 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
 ga('create', 'YOUR ANALYTICS PROPERTY ID HERE', 'auto');
 ga('send', 'pageview');
 </script>

On our site, we use a PHP function to add the Google Analytics tracking code to the wp_head hook in WordPress, like so.

//Google Universal Analytics
function google_analytics() { ?>
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); 
  
  ga('create', 'YOUR ANALYTICS PROPERTY ID HERE', 'auto');
  ga('send', 'pageview');
</script>    
<?php }
add_action ('wp_head', 'google_analytics');

What we need to do is to add our new custom dimension tracking snippet before we call ga('send', 'pageview'). Obviously it doesn’t make sense to track an author on the home page or archive pages that do not have authors, so we’ll use the WordPress is_singular() conditional statement to only output the snippet when we are on a single article page where the post type is “post”. We have numerous custom post types where we don’t want to use author tracking. If you just have the WordPress default post types, you could also use the is_single() conditional statement. Also note that you need to get the dimension index number correct, based on what you previously setup in Google Analytics. In our case, this is dimension1, but it could be dimension5, for example.

//Google Universal Analytics
function google_analytics() { ?>
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
 
  ga('create', 'YOUR ANALYTICS PROPERTY ID HERE', 'auto');
  <?php if (is_singular('post') : ?>
  ga('set', 'dimension1', '<?php the_author(); ?>');
  <?php endif; ?>
  ga('send', 'pageview');
</script>    
<?php }
add_action ('wp_head', 'google_analytics');

While this has worked for us, it is using the_author() outside of the loop, which may give you unexpected results. A more reliable way would be to use get_queried_object() to retrieve the post that was loaded, which works well for single posts. Our final tracking snippet looks like this.

Once you have this running on your site, you can verify that the snippet looks correct by viewing the page source on individual posts. Look for the GA snippet and see if it is adding in the new line where it sets the dimension and shows the author name. When you View Source on your posts, the resulting Google Analytics script should look something like this in your <head>.

Here are the things to double check when you View Source.

  1. Your site’s Analytics ID is appearing
  2. The dimension number matches the dimension number you set up in Google Analytics
  3. You are not duplicating Google Analytics scripts. This can happen if you are using a Google Analytics plugin or your theme outputs one automatically.
  4. If you are using Google Analytics demographics, display advertising or other features, your snippet will include more ga commands than you see in this example.
  5. If you are tracking authors on custom post types, change the conditional is_singular() statement to reflect that.

To view these new statistics, you can add a secondary dimension when your are on a pagview or other report, using the secondary dimension drop-down.

Google Analytics secondary dimension

The resulting report will now show author names for your posts.

Google Analytics author report

You can also get fancy and create a custom report based on these metrics to show pageviews by author.

Google Analytics author custom report

While we prefer to control our tracking code directly, there are plugins out there that provide this sort of functionality, like Yoast’s WordPress SEO Premium. If you want to use Google Analytics custom dimensions with WordPress SEO, you’ll have to get the paid premium version, not the free version from the WordPress.org plugin site.

Latest Developer Blog Articles

Job Listings on GeekWork

Find more jobs on GeekWork. Employers, post a job here.