How to Use Custom Variables in Google Analytics
It’s easy to track campaigns, ecommerce transactions and JavaScript events in Google Analytics. Custom variables offer a further level of control which permits you to segment all visitor data, e.g.
- monitoring pages viewed by members and non-members
- discovering which products are bought by new and existing customers
- categorizing content by topic.
A single custom variable is set using the following code:
_gaq.push(["_setCustomVar", index, name, value, scope]);
where:
index (required)
You can set up to five custom variables on any page so the index is an integer between 1 and 5.
I recommend keeping things simple — define five or fewer custom variables per website and assign a consistent index. It’s possible to create more if they are spread over multiple pages, but it can lead to confusion.
name (required)
The custom variable name.
value (required)
The custom variable value. It’s possible to set numeric values, but data is passed and treated as a string.
scope (optional)
An integer between 1 and 3 where:
- 1 is visitor level — the custom variable data persists for every visit and page viewed by an individual.
- 2 is session level — the custom variable data persists during the single visit made by an individual.
- 3 is pageview level — the custom variable data only persists during the current pageview.
For example, the following visitor-level variable could be set after the user registers and logs in for the first time. It would only need to be set once because it would remain associated with the user (even when they log out of our system):
_gaq.push(["_setCustomVar", 1, "Member", "yes", 1]);
Perhaps we now want to segment members by the number of months they’ve been registered (up to a maximum of 12 months). We could set a session-level variable whenever they log in:
_gaq.push(["_setCustomVar", 2, "RegisteredFor", Math.max(months,12)+" months", 2]);
If we want to track which topics are of interest to users, we could set a pageview-level variable:
_gaq.push(["_setCustomVar", 3, "Topic", "JavaScript", 3]);
Custom variables are not sent immediately and should normally be set prior to calling _trackPageView(). They are also sent when a custom event occurs with _trackEvent(), but that’s not a situation you can rely on. Our full code could therefore be:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
// set custom variables
_gaq.push(["_setCustomVar", 1, "Member", "yes", 1]);
_gaq.push(["_setCustomVar", 2, "RegisteredFor", Math.max(months,12)+" months", 2]);
_gaq.push(["_setCustomVar", 3, "Topic", "JavaScript", 3]);
// track page view
_gaq.push(['_trackPageview']);
(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);
})();
Custom Variable Reporting
The easiest way to view custom variables in Google Analytics is the Visitors > Custom Variables report.
However, Advanced Segments in My Customizations offers a greater level of analysis. Click Create new custom segment then expand the Visitors section of the Dimensions box. Define a Segment by dragging a custom variable name or value to the dimension box. The following screenshot defines a new segment named “Logged In” which looks for data where the “Member” variable is set to “yes””:
Once the segment has been saved, you can open any report and click the “All Visits” tab at the top-right. You can restrict it to show members only by ticking the “Logged In” checkbox:
Have you found any interesting uses for custom variables in Google Analytics?