« »
May 18
2010
Smart analyst, smart analyst, what do you see?
I see more segmentations made just for me
!

Marketer, marketer, what do you see?
I see detailed reports and valuable insights made available for me!

Business owner, business owner, what do you see?
I see more money for me!

Online shopper, online shopper, what do you see?
I see an amazing website customized and optimized for me!

Say you have an online store where you sell clothes for men, women, and children. Wouldn’t it be nice to have more visibility into the shopping experience based on site visitors’ age, gender, and products of interest?

Say Hello to a feature known as “Multiple Custom Variables!” To read more about how it works, check this post in the Google Analytics blog.

In this post, I would like to walk through our implementation strategy and technical details.

The Strategy

As visitors enter the store website, they will be tagged with different labels based on some personal unidentifiable information (gender and age), and based on the pages they visit (products and store departments):

Example 1:

Tom is a new visitor.  He registered on the site, viewed a product (women’s pajamas), added the item to the shopping cart and then completed the purchase.


At the end of the session, Tom’s visit will be labeled as the following:

Example 2:

Sara is a returning visitor who in a previous visit registered to the site. She viewed a few products (men’s and women’s apparel), and then left the site.

At the end of the session, Sara’s visit will be labeled as the following:

The How

1. Customizing the tracking code:

Visitor-level Labels

At the visitor level we will use custom variables to segment visits based on their entries in a form. Let’s use the values that visitors voluntarily enter in the “gender” and “age” fields in the following form. These custom variables will remain attached to the visitors for future visits starting from the visit in which they filled the registration form (until they clear their cookies).

Setting the value of “gender” and “age”:

  1. Add custom code that takes the gender and age values from the registration form.
  2. Pass the two variables (gender-variable, age-variable) to the registration confirmation page
  3. In the registration confirmation page, add the following code inside the GATC right before* the pageview GIF request _trackPageview()

pageTracker._setCustomVar(1, “gender”, gender-variable, 1);
pageTracker._setCustomVar(2, “age”, age-variable, 1);

* If it is not possible to add the custom code inside the GATC, then add the following code after the GATC

<script type=”text/javascript”>
pageTracker._setCustomVar(1, “gender”, gender-variable, 1);
pageTracker._setCustomVar(2, “age”, age-variable, 1);
pageTracker._trackPageview();
</script>

Session-level Labels

At the session level we will use custom variables to distinguish visitors’ behavior across sessions based on their conversion. In this way, we can segment visits by those who complete e-commerce transactions versus those who just browse products on the site.

A visitor will be tagged as a “buyer” if he or she completes a transaction. If they do not buy anything, the visitor will be tagged as “justlooking”.

Setting the value of “visitor-type” to “buyer”:

In the transaction confirmation page (thank you page) add the following code inside the GATC right before the pageview GIF request _trackPageview()

pageTracker._setCustomVar(3, “visitor-type”, “Buyer”, 2);

Setting the default value of “visitor-type” to “justlooking”:
All visitors will be tagged by default as “justlooking” once they enter the site by setting the value of the custom variable “visitor-type” to “justlooking” at the session-level.

Add the below code* to all landing pages right after the GATC.

* High-level description of the code:

  • extracts the “_utmb” string from the cookies set by Google Analytics
  • extracts the “pageview count” value from the _utmb cookie
  • if the session’s “pageview count” is equal to 1 (landing page), set the value of “visitor-type” to “justlooking”

<script type=”text/javascript”>
var utmb = get_utm_value(document.cookie, ‘__utmb=’, ‘;’);
var utmc = get_utm_value(document.cookie, ‘__utmc=’, ‘;’);
var pageview_count = get_utm_value2(utmb, utmc);
if(pageview_count<=1)
{
pageTracker._setCustomVar(3, “visitor-type”, “justlooking”, 2);
}
//This function extracts the “_utmb” and “_utmc” string from the cookies set by Google Analytics
//This function was originally written by the Google Analytics team (urchin.js)
function get_utm_value(l,n,s)
{
if (!l || l==”" || !n || n==”" || !s || s==”") return “-”;
var i, i2, i3, c=”-”;
i=l.indexOf(n);
i3=n.indexOf(“=”)+1;
if (i > -1)
{
i2=l.indexOf(s,i);
if (i2 < 0)
{
i2=l.length;
}
c=l.substring((i+i3),i2);
}
return c;
}
//This function extracts the “pageview count” value from the _utmb cookie
function get_utm_value2(utmb,utmc)
{
var i, j, k;
var utmc=utmc+’.';
i=utmc.length;
j=utmb.indexOf(“.”, i);
k=utmb.substring(i,j);
return k;
}
</script>

Page-level Labels

At the page level we will use custom variables to determine which product categories and store departments are visited. We will set a custom variable at the page level for each product, where the product category and the department for that product is set as a custom variable.

Setting the value of “store-department” and “product-category”:
In each product page, add the following code inside the GATC right before the pageview GIF request _trackPageview()

pageTracker._setCustomVar(4, “store-department”, “Men”, 3);
pageTracker._setCustomVar(5, “product-category”, “Suits”, 3);

2. Creating Custom Advanced Segmentation

Enough coding!! Now it is time to create some custom segments we can use for deep analysis.

In this section, we will set up a simple custom segment for all ladies (31 to 50 years’ old) who visited the men’s department, looked at suits, and bought an item during their visit.

  1. Sign in to your Google Analytics account
  2. Advanced Segments > Create a new advanced segment
  3. Add the following dimensions:

3. Viewing Reports

We are now ready to analyze based on custom segments built upon our custom variables! Let’s look at the amazing data available in our new segments:

Segment 1: Male shoppers (31 to 50 years’ old) who visited the women’s department, looked at Suits and bought an item during their visit

Segment 2: Female shoppers (31 to 50 years’ old) who visited the men’s department, looked at Suits and bought an item during their visit

From the first glimpse at the dashboard numbers above, we can confirm that for the 31 to 50 age group:

  • It takes men fewer number of page (around 3 pages on average) to buy suits for women compared to women shoppers who visit 5 pages on average to make a suit purchase for men.
  • The number of women who shop for men is more than the number of men who shop for women!  Shame on us men :) .

If you came up with more interesting segments please share with us, we are very interested hearing from you.

Happy analyzing!

Tags: , , ,

18 Responses to “Google Analytics’ Gift to Humanity: Multiple Custom Variables”

  1. Nikki Rae says:

    Hi

    This is great. Thanks. Going to use the nursery rhyme on my toddler too :)

    Nikki Rae

  2. Ophir Prusak says:

    Great post (as usual).

    I’m not sure everyone gets the brown bear brown bear reference though:
    http://www.amazon.com/dp/0805047905

    - Ophir

  3. Thanks Nikki and Ophir,

    As you can see, fatherhood will have an impact on our professional work ;)

  4. Omarjead says:

    Great post Mr Ezzedin. Provides a refreshing perspective on custom variables and segmentation.

  5. Ophir Prusak says:

    You can actually get more than 5 custom variables using an undocumented function.

    Details here:
    http://analyticsimpact.com/2010/05/24/get-more-than-5-custom-variables-in-google-analytics/

  6. Kamal says:

    Another excellent article Allaedin. Not sure about the font type for your signature though – they’re a litigious bunch ;)

  7. romy says:

    Great post!Thank you for sharing.

    Allaedin, I was wondering are custom variables limited to just categorical variables (for instance gender which can just take two values)? Could we tie in something like a company name into a form (where we don’t know what the range is)?

  8. Hi Romy, Thanks for reading!

    There is no limitation in the number of values you assign for any custom variable. Typically, you will have two or more values for a given name. For example, you might define a custom variable name “product-category” and supply: Suits, Pants, Shirts, Shoes, Sports Apparel, Socks as possible values.

  9. Alex says:

    Hi Allaedin,
    Really it was a great post. Thanks for sharing with us.
    How it could possible to sharing a male or female members visited webpage?

  10. Hi Alex,

    We use the value that visitors voluntarily enter in the “gender” field of the account registration form.

  11. Jeff says:

    Hi Allaedin,

    What would need to change to your code above to set the default value of “visitor-type” to “justlooking” if I wanted to use that code with the Asynchronous GA code? Obviously the pageTracker._setCustomVar would change, but are there any changes needed to use this that you can see?

  12. Hi Jeff,

    You need to change all code syntax to Asynchronous (Read more: http://code.google.com/apis/analytics/docs/tracking/asyncMigrationExamples.html)

    As for the Custom Variables, the syntax should look like this: _gaq.push(['_setCustomVar', 3, 'visitor-type', 'justlooking', 2]);

    Good luck,

  13. Jeff says:

    Hi Allaedin,

    I think I didn’t ask my question very well, the first time, so I’ll give it another shot. I know all the GA code needs to change, but I was specifically talking about the code below and more specifically, the get_utm_value – whether that will still work in capturing the cookies when using Asyncronous code?

    Thanks again!

    =======

    var utmb = get_utm_value(document.cookie, ‘__utmb=’, ‘;’);
    var utmc = get_utm_value(document.cookie, ‘__utmc=’, ‘;’);
    var pageview_count = get_utm_value2(utmb, utmc);
    if(pageview_count -1)
    {
    i2=l.indexOf(s,i);
    if (i2 < 0)
    {
    i2=l.length;
    }
    c=l.substring((i+i3),i2);
    }
    return c;
    }
    //This function extracts the “pageview count” value from the _utmb cookie
    function get_utm_value2(utmb,utmc)
    {
    var i, j, k;
    var utmc=utmc+’.';
    i=utmc.length;
    j=utmb.indexOf(“.”, i);
    k=utmb.substring(i,j);
    return k;
    }

    ====

  14. Hi Jeff,

    Good question, there is no change in the way how the GA cookies are written, so the code above should work just fine. (code is tested with Asynchronous)

  15. There is no limitation in the number of values you assign for any custom variable. Typically, you will have two or more values for a given name. For example, you might define a custom variable name “product-category” and supply: Suits, Pants, Shirts, Shoes, Sports Apparel, Socks as possible values.

  16. Allaedin,

    After an hour of searching online for information about Custom Variables, your post was the first to answer my question, can you use multiple ‘visitor’ Custom Variables’ on the same page.

    The addition of advanced segments, showing exactly what type of data is available when correctly structured custom variables are used.

    Thanks very much and keep up the posts (images and screenshots make such a difference).

Leave a Reply