Web Analytics

Google Analytics’ Gift to Humanity: Multiple Custom Variables

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!

Allaedin Ezzedin

Allaedin Ezzedin is a Senior Director at Merkle | Cardinal Path, renowned for his dedication to melding business strategies with technological innovation, particularly in the realm of digital marketing optimization. With an unwavering commitment to enhancing the digital analytics landscape, Allaedin is at the forefront of advocating for Google Analytics as a pivotal enterprise analytics solution.

Share
Published by
Allaedin Ezzedin

Recent Posts

Optimizing user experiences with Digital Experience Analytics (DXA) platforms

As consumers become increasingly digitally savvy, and more and more brand touchpoints take place online,…

1 month ago

Enabling Value-Based Bidding with Google Tightlock

Marketers are on a constant journey to optimize the efficiency of paid search advertising. In…

2 months ago

Resolving “Unassigned” Traffic in GA4

Unassigned traffic in Google Analytics 4 (GA4) can be frustrating for data analysts to deal…

2 months ago

This website uses cookies.