- 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]);
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.
The custom variable name.
The custom variable value. It’s possible to set numeric values, but data is passed and treated as a string.
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:
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:
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?
Craig is a freelance UK web consultant who built his first page for IE2.0 in 1995. Since that time he's been advocating standards, accessibility, and best-practice HTML5 techniques. He's created enterprise specifications, websites and online applications for companies and organisations including the UK Parliament, the European Parliament, the Department of Energy & Climate Change, Microsoft, and more. He's written more than 1,000 articles for SitePoint and you can find him @craigbuckler.
The Principles of Beautiful Web Design, 4th Edition
Learn PHP in One Day and Learn It Well
Learn SQL (using MySQL) in One Day and Learn It Well