Analytics Market Chart

Custom Variables in Google Analytics

NOTE: This article refers to an old feature, which has been deprecated in favor of multiple custom variables. Users are encouraged to use the new, more powerful code.

When to Use User-Defined Variables

Google Analytics gathers all the information it can about each visit: location, network speed, browser, operating system, screen resolution, traffic source, number of previous visits to the site, etc. But these variables are all generic. They apply to every site, and some of them are meaningless to yours. Using a custom variable makes reports much more useful.

Here are some ways websites have used the user-defined variable:

  • Compare registered users to prospects
  • Record each visitor's username to determine lifetime value of each visitor
  • Capture demographic or survey information from a form the visitor fills out
  • Compare how visitors to different versions of a landing page respond

_setVar()

Creating user-defined variables is just a matter of executing the _setVar() method. This creates a new cookie on the visitor's browser that is tied in with the other tracking cookies. It contains the value you pass to the method and persists for multiple visits.

For example,

pageTracker._setVar('registered');

would create a cookie with the value "registered". The visitor would retain this cookie for future visits unless it was overwritten with a new value or manually deleted.

You can create one user-defined variable per visit. If you execute _setVar() multiple times during a single visit, the cookie will be overwritten each time. Only the first value will be applied to the entire visit, but the last value will apply to the next visit.

Reporting Custom Variables

When using custom variables, new reports will be populated with the values you supplied automatically. Most reports can be segmented by the user-defined values by changing the Dimension dropdown. This will compare all the user-defined values that apply to that specific report.

Also, a high-level report of all the user-defined values is available in the Visitors section.

This allows you to see how different types of visitors interact with your site differently. This can give you valuable insights beyond basic reports like total number of visits or overall bounce rate.

How to Execute _setVar()

The _setVar() method is a simple JavaScript method that can be executed any time after the ga.js file has been loaded and passed any kind of string. It can be executed before or after _trackPageview().

It can be called when a page loads either by adding it directly to the main tracking code on the page or by creating an onload event for the body.

<body onload="pageTracker._setVar('Landing Page v.1');">

It can be executed when a visitor clicks a button with an onclick event.

<a href="link.html" onclick="pageTracker._setVar('registered user');">Click here</a>

It could also be executed when a visitor selects a value in a form.

<form onSubmit="pageTracker._setVar(this.mymenu.options
[this.mymenu.selectedIndex].value);">
<select name=mymenu>
<option value="Technical/Engineering">
Technical/Engineering</option>
<option value="Marketing/PR">Marketing/PR</option>
<option value="Manufacturing">Manufacturing</option>
<option value="General Management">General Management</option>