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:

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:

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 written more than 1,000 articles for SitePoint and you can find him @craigbuckler