Google Analytics Custom Variables: A Page-Level Example

October 2, 2011

Once you’ve implemented Google Analytics on your WordPress blog, you’ll likely find that the default reports aren’t providing the site-specific information you are looking for…or, maybe just not at the level of aggregation you’d prefer. Google Analytics custom variables provide a method of capturing your site-specific information, depending on whether the information changes once per visitor, once per session, or once per page. Examples of custom variable usage includes:

Demographic information, such as Gender (Visitor-level, never changes)

Visitor logs in to your website (Session-level, may not log in during future visits)

Each section of the website a visitor “touches” (Page-level, changing multiple times during a session)

This tutorial will cover the Page-level custom variable type, capturing the WordPress Category for each blog post. With this information, we’ll be able to see which categories of posts are most popular on your WordPress blog over time.

Setting a Google Analytics custom variable

To set a Google Analytics custom variable, we need to use the following syntax:

_setCustomVar(index, name, value, opt_scope)

The “index” section of the variable indicates which of the five allowable custom variables we want to use to record our information (slot 1-5). “Name” indicates what we want to call our variable. “Value” is going to be the actual value we are looking to save. And finally, “opt_scope” represents whether we want the variable to be page-level, session-level, or visitor-level.

Recording WordPress category into a custom variable

In order to capture the WordPress category in a Google Analytics custom variable, we’re going to use a combination of PHP, WordPress functions, and (obviously) Google Analytics code. Here’s the code snippet we’re going to use:The “is_single” part of the code is a WordPress function, which evaluates whether or not a given page is a single post. Since only single post pages have categories, we use this function to set the Google Analytics custom variable only when there is going to be a category value available on the page. The “$category” part of the code is a PHP variable that stores the entire array of WordPress info that goes along with the “get_the_category” function. Finally, the part of the code that starts “echo” is the PHP code needed to build the Google Analytics custom variable string we want to have. Within this code, you can see the “_setCustomVar” code described in the first part of the tutorial; we’re setting the “Index” value to ‘2’, which means we’re using Google Analytics Custom Variable 2. The “Name” of the variable will be “Category”, the “Value” to be set is the WordPress category value (from the “‘”. $category[0]->cat_name. “‘ variable), and the “opt_scope” value is set to “3”, which means page-level.

According to Justin Cutroni, who literally wrote the book on Google Analytics, we want to put our custom variable code BEFORE the “_trackPageview” portion of the Google Analytics tracking code whenever possible. This is because on the last page of a visit, if your custom variable code is after the “_trackPageview” code, Google Analytics won’t “see” the custom variable code, since the data has to tag along with a _trackPageview call. Here’s what the final set of code will look like (place in your header.php file):

Example of Custom Variable report

Here’s what the report will look like in Google Analytics. To see the report, go to “Visitors –> Demographics –> Custom Variables”.

@DrewAPicture I’ve found that it can take up to 24 hours for the data to start showing in the custom variables….however, I went to your site and used a debugger tool, and it doesn’t look like you are setting custom variables. If you want, send me a page URL, and I’ll see if I can figure it out.

Hey. This post is much appreciated and very well explained. My one question is what will happen if I have some post and check off multiple categories? Will it be counted under each one or only once? For example, if I write a put up a post and I categorize it as “Music News” and also “Business News” and someone comes to my site and see the post, will my pageview count increment by one in both “Music News” and “Business News” or neither or just one?

ryanstuThe way the code is setup here, the “$category[0]” portion of the code picks the first category only. I’m not sure exactly if it picks by alphabetical order or what WordPress considers “first” by an internal value. I’m not quite sure what would happen if you tried to pass the entire category PHP array to the Google Analytics Custom Variable. At best, you’d get a comma separated string in your GA variable, but I suspect that it would cause a GA or JavaScript error.