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.

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:

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.”

AddCustomDimension

Easy-peasy.

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.

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.”)

So our whole chunk of code now looks like this:

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:

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.”

In this case, I named my custom dimension "SP_Author."

In this case, I had named my custom dimension “SP_Author” back in Step 2.

Hooray!

Questions? Better methods? Let me know in the comments.

9 Comments

Howard says:

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…

Andrew says:

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)

Christina Blust says:

Awesome! I’m so glad this helped you. I too started with the Yoast plugin and then realized it just wasn’t cutting it.

renaud says:

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…

Roger M says:

Thank you Christina,
you’ve been a great help!

bye

Harish says:

Christine,

You did a great job here.
Thanks a lot
Harish

Harish says:

Can you suggest me for category and publish date as you done for author.

Ir would be very helpful for me .

Thanks
Harish

Zat says:

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

Join the conversation




* Required


Basic HTML code will probably work. Inappropriate or irrelevant comments will be gleefully deleted. (You can disagree with me, just don't be a poop.)

Recent Dribbble Shots

Error retrieving Dribbble shots

See Christina on Dribbble.