facebook google plus twitter
Webucator's Free Advanced Google Analytics Tutorial

Lesson: Custom Variables

Welcome to our free Advanced Google Analytics tutorial. This tutorial is based on Webucator's Advanced Google Analytics course.

In this lesson, you will learn the basics of custom variables and how to develop your own for your website.

Lesson Goals

  • Fundamentals of Custom Variables.
  • How to Define Custom Variables.

Introduction to Custom Variables

Google Analytics has a plethora of default metrics, which can be used for slicing and dicing your data. For example, you can segment your data based on geographic locations, traffic sources, language preferences, etc. In addition to these default variables, Google Analytics allows you to define your own variables to classify and segment your data any way you like.

These user defined variables are called Custom Variables in Google Analytics. You can define up to 50,000 custom variables.

Why and When to Use Custom Variables

Practically you can create a custom variable even for segmenting internal traffic which is traditionally done via filters. In general you may want to define custom variable under the following conditions:

  1. If you want to create a different category which is not available in Google Analytics for segmenting your visitors.
  2. If you plan to capture specific information about your visitors and segment your data based on that information.

Examples of Custom Variables

  1. Identifying subscribers versus non-subscribers.
  2. Segmenting buyers based on their shipping preferences.
  3. Segmenting your visitor based on their professional background (e.g. technical vs. non-technical background). First make sure that you can legally obtain and use such information.

Architecture of Custom Variables

The _setCustomVar Method

To create a custom variable you will need to use the _setCustomVar() method. You can pass four different arguments through the _setCustomVar method.

Argument Name Explanation
Index or Slot This is a required field. You have total of 5 slots to play with for a single visit. This field will only except an integer argument, i.e. numbers from 1 to 5.
Name This is a required field which helps in identifying the custom variable within the report section. Name can be any series of letters, numbers or symbols (e.g. Professions).
Value This is a required field and it is always paired with the argument Name. One custom variable can have many values (e.g. a custom variable named Profession can have two values; engineering and marketing).
Scope There are three types of scopes, each represented by a numerical value. 1 - Visitor, 2 - visit or session and 3 - page-level. This is an optional field and by default it is set to page-level interaction.

Syntax for creating a custom variable is as follows:

_gaq.push(['_setCustomVar', index, name, value, opt_scope]);

Implementing Custom Variables

Your code for creating a custom variable must be placed prior to the _trackPageview() method.

Consider that you are managing a website for an online magazine which offers different types of articles for their visitors to read. You want to determine which category is more popular. You can accomplish this task using custom variables.

You will need to define custom variables at the page level for every single article based on how you wish to categorize your articles. Your code snippet may look like:

_gaq.push(['_setCustomVar',1,'Article Category','Technology',3]);

In the above example you are assigning your custom variable to slot number 1. You are labeling your custom variable as Article Category and for this particular article you are setting the value = Technology. Since this interaction is happening on a pageview level, the scope of your custom variable is set to 3.

Application of Custom Variables

Duration: 10 to 15 minutes.

On your news letter signup page you have a drop down menu where visitors can select their professional background (e.g. Engineering, Marketing, etc). You want to segment your visitors based on their profession.


In the above example, you will first need to write code that will capture the user input from a drop down menu in regards to their profession background and pass that parameter to _setCustomVar method. Your _setCustomVar method may look like:

 _gaq.push(['_setCustomVar',1,'Profession Type','Engineering',1]); 

In the above example:

  1. Custom Variable is assigned to slot #1.
  2. Custom Variable is labeled as Profession Type.
  3. Value of this Custom Variable is assigned to Engineering.
  4. Since this is a visitor level segmentation, the scope of this custom variable is set to 1.