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->cat_name. “‘ variable), and the “opt_scope” value is set to “3”, which means page-level.
Incorporating custom variable code into Google Analytics tracking code
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”.