Google Analytics + WordPress: Setting up custom dimensions
Written on April 24, 2014 by Christina Blust
I love plugins, don’t get me wrong — but sometimes I want to know exactly what code is being added to my site and why. The “new” Universal Analytics are totally doable to hand-code in, no plugins needed.
Let’s start with finding the basic starter code for your Google Analytics property.
Step 1: The basics
Go in to the Google Analytics property for your site. (If you haven’t set this up yet, follow Google’s instructions to set up a property.) From the Admin area, go to your Property Settings. Under Tracking Info, click “Tracking code.”
Google is smart enough to give you the exact code you need to place in your file. It will look something like this:
<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', 'UA-12345678-1', 'thelivesincerelyproject.com');
ga('send', 'pageview');
</script>
Copy this code and open the file in your theme that contains the closing </head> tag. (For many WordPress themes this is header.php.) Paste this directly before the </head> tag and you are good to go! Give Google time to collect the data, and soon you’ll see beautiful statistics from your site, right in Google Analytics.
I’m guessing you aren’t happy yet, though. So let’s look at how the Universal Analytics custom dimensions can make your analytics even more useful.
“Custom dimension” might sound pretty loopy, but it’s simply one other piece of data for Google to track. Google is already tracking lots of data each time a visitor spends time on your site: the name of the page they went to, how long they spent there, what operating system they were using at the time… these are collected all the time for every single Google Analytics property, and they’re all useful for different things! But we can give Google even more information to track.
For an example, I’ll go through how to add a post’s author as a custom dimension.
Step 2: Create the dimension in Google Analytics
From your Property settings under Custom Definitions, click “Custom Dimensions.” Here we can create a new custom dimension for Author.
Click the “New Custom Dimension” button and give your dimension a name. I like to prefix it with something to make sure I can always tell which dimensions are mine. Here, I’ve called the dimension “WP_Author.” Because the author is determined for each post, we set the Scope to “Hit.”
Once you’ve created the dimension, you’ll see that GA has assigned it an Index. Make a note of it — we’ll need it in the next step. (If this is your first dimension, it will be “1.”)
Now Google is ready to start receiving data for this dimension, so we’ve got to go back to our tracking code and tell Google what to collect.
Step 3: Add this custom dimension to your tracking code
Since the author varies depending on the post, we’ll first need to create a php variable to hold that information.
<?php
if(is_single()) {
$getid = $posts[0]->post_author;
$gaAuthor = get_the_author_meta('display_name', $getid);
}
?>
In this example, our php variable is $gaAuthor.
Then we modify our tracking code from earlier, linking the data in that variable to our Google Analytics custom dimension. Make sure to match the index number that Google assigned our dimension earlier. (Here, we call it “dimension1.”)
ga('send', 'pageview', {
'dimension1': '<?=$gaAuthor?>'
});
So our whole chunk of code now looks like this:
<?php
if(is_single()) {
$getid = $posts[0]->post_author;
$gaAuthor = get_the_author_meta('display_name', $getid);
}
?>
<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', 'UA-12345678-1', 'thelivesincerelyproject.com');
ga('send', 'pageview', {
'dimension1': '<?=$gaAuthor?>'
});
</script>
If you want to check your code, view the source of a single post on your site. You should see the Google Analytics tracking code, but with our WordPress variable data pulled in. If it’s working, you’ll see something like this:
...
ga('send', 'pageview', {
'dimension1': 'Christina Blust'
});
...
Awesome! Now we’ll be able to see our author data within Google Analytics.
Step 4: Go look at your pretty data in Google Analytics
After waiting a little while for Google to collect some data, we’ll be able to see our new custom dimension.
Where did it go?
At first glance you might not be able to find your custom dimension. Never fear, it’s in there waiting for you to use it!
It is now in the list of Dimensions that Google Analytics can pull from in reports and filters. For instance, say I want to make a custom report that only cares about posts I wrote. To do this, I’d go to the Customization tab in Google Analytics and create a new custom report with a filter that only includes data where my custom dimension equals “Christina Blust.”
Hooray!
Questions? Better methods? Let me know in the comments.
Read more like this

Thanks! Your post was the only one on this topic that made sense to me (I’m very much a Javascript beginner– thanks for using plain English!).
Would you mind checking out my website link? It’s my Indiegogo campaign. I’m looking for feedback from musicians on my proposed project, and it appears you happen to be one of those…
Hi Christina!
First, I’d like to say thanks for making this post and I finally found the solution from your blog. I was using Joost’s wp analytics plugin and it isn’t working. After your workaround, things are looking good. I was also able to track the category too using the categories functions! Your such a big help and I sincerely thank you for posting this. So far, WP forums weren’t giving simple solutions for newbs like me.
Take care and keep rocking!!!
– andrew
(I just implemented your code to http://www.justforfacts.com)
Awesome! I’m so glad this helped you. I too started with the Yoast plugin and then realized it just wasn’t cutting it.
Hi,
I would be really interesting to know how with this method you are tracking the categories of your posts?
Thx!
Renaud
Hi, thanks for this walkthrough! I know it’s a bit of an older post, so apologies for only just commenting. I’m trying to get this working on my site, but when I follow all the instructions and check the source code, the author variable data isn’t pulling in. I’ve tried clearing all my caches and trying again, and nothing.
I tried putting the php in header.php, and when that didn’t work I tried putting it in its own separate plugin file and activating it, and that didn’t work either. Any idea what might be going on?
Thanks…
Thank you Christina,
you’ve been a great help!
bye
Christine,
You did a great job here.
Thanks a lot
Harish
Can you suggest me for category and publish date as you done for author.
Ir would be very helpful for me .
Thanks
Harish
Hello, many many thanks for this useful code!
Let me also join the fellows above kindly requesting that you also incorporate category tracking in this code. Would be highly appreciated.
Looking forward to hearing from you.
Thanks,
Zat
Hi Christina,
Your code is exactly what i was looking for, but i need to get some info from woocommerce such as product id, price and final URL. Can you help me find those three variables?
thanks in advance,
Elies
Dear Christina, this post is very useful. However, I expected to see some plugin that could do the work on WordPress blogs. That would have been awesome for non-programmers out there. Also, I believe the datalayer push could be done using a GTM implementation.
For people who need more custom dimensions than the author I added this to Github.
https://github.com/tfadz/Wordpress/blob/master/wordpress-custom-dimensions.php