Friday, 19 February 2010

Google Analytics Custom Variables – part 2 – Practical implementation

As we saw last week the custom variables feature in Google Analytics is revolving around the _setCustomVar() method. Typically you will call _setCustomVar() before a _trackPageview() or a _trackEvent() so that the information get delivered with the GIF request sent by _trackPageview() or _trackEvent().
Here is an example:
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
<script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
pageTracker._setCustomVar(1, "pagetype", "productpages" , 3 );
pageTracker._setCustomVar(2, "productcategory", "officefurniture" , 3 );
pageTracker._trackPageview();

} catch(err) {}<script>
This is the typical script you will put at the end of the page before the tag. As we can see we defined two variables “pagetype” and “productcategory” and gave them the values “productpage” and “officefurniture” (all at the page-level scope since last argument of setCustomVar is 3).

How can I track results?

The Custom variables data can be integrated in 3 different report types:
  1. The Custom Variables report (in the Visitors section in the main menu)
  2. The “Advanced Segments”
  3. The “Custom Reports”

The Custom Variables report

While it normally takes only few hours for all reports to be updated, the custom variable report takes a bit longer. To my experience this is roughly around 48 hours. The first time you will run the report you will see a “not set” message in the first column. Do not panic, just be patient. Eventually you will be able to see the data.
As you can see in the pictures, if you click on the variable name you will drill down to the different values.

The advanced segments report

The advanced segment report is an incredibly powerful feature. If you have never used it I recommend checking the Google conversion university and have a look to the corresponding “Advanced Segmentation” presentation.
When you create the custom segment you will find new dimensions in the “visitor” section. Basically two sets:
  • Custom variable key
  • Custom variable value
Each one numbered from 1 to 5, and corresponding to the variable slot.
As you can imagine you can build extremely powerful visitor segmentations.

The Custom Reports

Exactly like in the advanced segment reports you will find two new dimension types:
  • Custom variable key
  • Custom variable value
Each one numbered from 1 to 5, and corresponding to the variable slot. See an example in the previous picture

Next part

Next week we will see the in details the different reports and the differences between the variable scopes in practical measurements.

Sunday, 7 February 2010

Google Analytics Custom Variables – part 1 - Introduction

One of the most underused features of Google Analytics is the “Custom Variables” report, not to be confused with the “User defined” visitor tracking report. Custom variables were made available in Advanced Segments and Custom Reports in December 2009 and are one of the most powerful additions of the last 6 months.

What are custom variables?

Let’s have a practical example. Let’s suppose we have an office supplies online shop that is selling to several typologies of customers let’s say:
  • Consumers
  • Home office, small office
  • Large businesses
  • Public administrations
At the same time let’s suppose that the products sold online fall into 3 broad classes:
  • Computer and Printer hardware
  • Office supplies and stationery
  • Office furniture
With custom variables we will be able to tag a visitor with a customer type (the first variable) according to different factors like: Registration data (if it’s already a customer), Landing pages, campaigns the visitor is coming from, content he is viewing and so on. Additionally we will be able to tag the visitor with a second variable corresponding to the type of products he is viewing preferentially. The possibilities are endless as we will be able to cross correlate the two dimensions and have a better understanding of our visitors.

What can we use custom variable for?

Additional examples are:
  • Segmenting visitors
  • Segmenting customers according to the order history
  • Segmenting visitors based on the campaigns they see prior converting
  • Segmenting visitors based on content visited
  • Segmenting visitors based on landing page
…to name a few.

How can we use Custom Variables?

In order to use custom variables we will have to modify the tracking code (the snippet of JavaScript that we paste before the tag) and use the _setCustomVar() method. I will give all the details on how to do this practically in next week post “Google Analytics Custom Variables – part 2”

Variable attributes

The Custom Variables features is powered by a new method: _setCustomVar()
As we will see later we can have up to 5 simultaneous variable tracking. Each variable has 4 attributes.

1 - Variable Scope

Before going deeper into our explanation we have to introduce the Variable scope concept. The scope is the context where we are going to define the variable. It is quite important to understand this.
There are 3 scopes:
1.1 - Page-level
Every type of variable that we can associate to a single page view (or event tracking) can be set at the page level.
Example: In our office supplies shop, suppose that we classify the product pages with a product type (“Computer and Printer hardware”, “Office supplies and stationery”, “Office furniture”). We will then set a variable with 3 possible values that we will associate to each product page. We will be than able to count the page views for each of the product type we have defined.

Pushing the idea further, if we want, we could also create a subtype variable extending our product classification.
1.2 – Session-level
Every type of variable that we can associate to a session. Example: we can number the visits that resulted with purchase and compare it to the visits that didn’t have any purchase attempt. In the session level scope, the variable will be counted at the end of the session and it will take the last value assigned before the end of the session.
1.3 - Visitor-level
Every type of variable that we can associate to a permanent cookie. You would typically use visitor-level variables to distinguish categories of visitors across multiple sessions. For our example you would assign the visitor type (Consumer, Home office small office, Large business or Public administrations) once and then track this value across multiple visits.

2 - Name

This is the string with the variable name. It will appear in the top-level Custom Variables report.

3 - Value

This string contains the value that will be attributed to the variable. Example, you could define the variable “gender” and give as value either “male” or “female”.

4 - Index

This is the placeholder or slot in which we are going to load the variable. Index can be an integer from 1 to 5. A variable should not be used across different slots. Theoretically you will be able to set 5 page level, 5 session level and 5 visitor level variables, for a total of 15 variables. In practice thought there will be interactions between variables making the reuse of variables in the same slot tricky. I will detail these interactions in the next post. My advice is to start using a maximum of 5 different variables only, placing them in different slots. In my next post I will give you few tips on how to use more than 5 variables.

Syntax

Now that we have seen the attributes we can set to the variable let’s see how we can call the function.

_setCustomVar(index, name, value, opt_scope)

Index: Can have integer values from 1 to 5
Name: The name for the custom variable (a string)
Value: The value for the custom variable (a string). Be careful that the sum of the length of Name and the length of Value cannot exceed 64 characters.
Opt_scope: This is he scope for the custom variable. The possible values are 1 (visitor-level), 2 (session-level), or 3 (page-level). When left undefined, the custom variable scope defaults to page-level interaction.

Next week

Next week I will show a practical implementation of the custom variables report and how to alter the tracking code in order to start using this extremely powerful new feature.