Google Analytics Custom Variables: A Page-Level Example

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:

1
_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 Google Analytics code.  Here’s the code snippet we’re going to use:

1
2
3
4
5
6
7
8
if (is_single () ) {

$category = get_the_category();
echo "_gaq.push(['_setCustomVar', 2,'Category','". $category[0]->cat_name. "', 3]);";

} else {

}

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.

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):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<script type="text/javascript">

var _gaq =_gaq || [];
 _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);

if (is_single () ) {

$category = get_the_category();
echo "_gaq.push(['_setCustomVar', 2,'Category','". $category[0]->cat_name. "', 3]);";

} else {

}

_gaq.push(['_trackPageview']);
_gaq.push(['_trackPageLoadTime']);

(function() {

 var ga = document.createElement('script');
 ga.type = 'text/javascript';
 ga.async = true;

 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga,s);
})();

</script>

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.

google-analytics-custom-variables

WordPress Categories in Google Analytics Custom Variable 2

  • WordPress to Jekyll: A 30x Speedup
  • Bulk Downloading Adobe Analytics Data
  • Adobe Analytics Clickstream Data Feed: Calculations and Outlier Analysis
  • Adobe: Give Credit. You DID NOT Write RSiteCatalyst.
  • RSiteCatalyst Version 1.4.8 Release Notes
  • Adobe Analytics Clickstream Data Feed: Loading To Relational Database
  • Calling RSiteCatalyst From Python
  • RSiteCatalyst Version 1.4.7 (and 1.4.6.) Release Notes
  • RSiteCatalyst Version 1.4.5 Release Notes
  • Getting Started: Adobe Analytics Clickstream Data Feed
  • RSiteCatalyst Version 1.4.4 Release Notes
  • RSiteCatalyst Version 1.4.3 Release Notes
  • RSiteCatalyst Version 1.4.2 Release Notes
  • Destroy Your Data Using Excel With This One Weird Trick!
  • RSiteCatalyst Version 1.4.1 Release Notes
  • Visualizing Website Pathing With Sankey Charts
  • Visualizing Website Structure With Network Graphs
  • RSiteCatalyst Version 1.4 Release Notes
  • Maybe I Don't Really Know R After All
  • Building JSON in R: Three Methods
  • Real-time Reporting with the Adobe Analytics API
  • RSiteCatalyst Version 1.3 Release Notes
  • Adobe Analytics Implementation Documentation in 60 Seconds
  • RSiteCatalyst Version 1.2 Release Notes
  • Clustering Search Keywords Using K-Means Clustering
  • RSiteCatalyst Version 1.1 Release Notes
  • Anomaly Detection Using The Adobe Analytics API
  • (not provided): Using R and the Google Analytics API
  • My Top 20 Least Useful Omniture Reports
  • For Maximum User Understanding, Customize the SiteCatalyst Menu
  • Effect Of Modified Bounce Rate In Google Analytics
  • Adobe Discover 3: First Impressions
  • Using Omniture SiteCatalyst Target Report To Calculate YOY growth
  • Google Analytics Individual Qualification (IQ) - Passed!
  • Google Analytics SEO reports: Not Ready For Primetime?
  • An Afternoon With Edward Tufte
  • Google Analytics Custom Variables: A Page-Level Example
  • Xchange 2011: Think Tank and Harbor Cruise
  • Google Analytics for WordPress: Two Methods
  • WordPress Stats or Google Analytics? Yes!
  • JuliaCon 2015: Everyday Analytics and Visualization (video)
  • Vega.jl, Rebooted
  • Sessionizing Log Data Using data.table [Follow-up #2]
  • Sessionizing Log Data Using dplyr [Follow-up]
  • Sessionizing Log Data Using SQL
  • Review: Data Science at the Command Line
  • Introducing Twitter.jl
  • Code Refactoring Using Metaprogramming
  • Evaluating BreakoutDetection
  • Creating A Stacked Bar Chart in Seaborn
  • Visualizing Analytics Languages With VennEuler.jl
  • String Interpolation for Fun and Profit
  • Using Julia As A "Glue" Language
  • Five Hard-Won Lessons Using Hive
  • Using SQL Workbench with Apache Hive
  • Getting Started With Hadoop, Final: Analysis Using Hive & Pig
  • Quickly Create Dummy Variables in a Data Frame
  • Using Amazon EC2 with IPython Notebook
  • Adding Line Numbers in IPython/Jupyter Notebooks
  • Fun With Just-In-Time Compiling: Julia, Python, R and pqR
  • Getting Started Using Hadoop, Part 4: Creating Tables With Hive
  • Tabular Data I/O in Julia
  • Hadoop Streaming with Amazon Elastic MapReduce, Python and mrjob
  • A Beginner's Look at Julia
  • Getting Started Using Hadoop, Part 3: Loading Data
  • Innovation Will Never Be At The Push Of A Button
  • Getting Started Using Hadoop, Part 2: Building a Cluster
  • Getting Started Using Hadoop, Part 1: Intro
  • Instructions for Installing & Using R on Amazon EC2
  • Video: SQL Queries in R using sqldf
  • Video: Overlay Histogram in R (Normal, Density, Another Series)
  • Video: R, RStudio, Rcmdr & rattle
  • Getting Started Using R, Part 2: Rcmdr
  • Getting Started Using R, Part 1: RStudio
  • Learning R Has Really Made Me Appreciate SAS