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
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.
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:
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:
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:
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.
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: