September 25, 2021

Custom Variables in Google Analytics: Everything you need to know

The custom variables in GA are defined as a key-value pair to add additional custom segments to your data. The custom variables can help in segmenting data according to some key page, session, and visitor-level attributes. I know all this might be going above your head so hang on and I promise by the end of this article you will know everything related to Custom Variables.

The custom variables are made up of 4 ingredients namely index, name/key, value, opt_score. An index is a number less than 5. Think of it as a slot or as Justin Cutroni describes a parking space. The name is name-value pairs of data. Nothing really special about these. It is any user-defined string and has to be less than 64 characters. The value is also any user-defined string that should be less than 64 characters. The opt_scope is how long the car will be parked in the parked slot defined as an index.

The Opt_scope could be page level, session-level or visitor level (tied to cookie).

In the free account of Google Analytics, you are allowed to have a max of 5 indexes. Thus the decision to use the index should be taken very judiciously. The opt_scope can take 3 numeric values. But what are these values? You guessed it right, 1,2 and3. 1 meaning it’s a visitor-level variable, 2 meaning it’s a session-level variable, and 3 meaning it’s a page-level variable.

The page-level variables have a hit-level session impact and while the session and the visitor-level variables have a pan session impact. To make you understand in simple terms, a page-level variable is only concerned with a page and the session, and variable-level variables are concerned with the entire session of the user.

The code for a custom variable is as follows; _setCustomVar(index, name, value, opt_scope)

Now you can define custom variables for log in to your website and you can compare the activity of people who have logged in v/s people who haven't. Cool, isn’t it?

But there are some things you should take care of while implementing Custom Variables,

As you have a maximum limit on the number of indexes which can be used, you should use it wisely. I personally recommend not reusing the index of page-level variables with session/visitor-level variables as it can lead to some complications.

Here are some cases of mix use of different types of variables and their end results

Suppose in a single session, page 1 has a page-level variable that says type=’news’ and page 2 has a session-level variable saying conversion = “false” and page3 has a session-level variable that says conversion= “true”, (with the same index number) then the number of visits to the page type= “news” will be 0 and the number of visits with conversion=”false” is also 0 and the number of visits with conversion=”true” is 1 as the session-level variable on the third page overrides the page and the session-level variable on the first and the second page.

Let’s take an example with 3 different sessions

Each session has 1-page visit, the first session has a page visit with a visitor-level variable (index 1) saying log_in=”yes” and the second session has a page visit with a session-level variable (index 2) saying converted=”false”. Now the third session has a page level variable on the page visit (index 1) saying type=”product”. Note that there are only 1-page visits in all the 3 sessions mentioned above. Now when the code will run, GA will record the number of visits for log_in=”Yes” as 2 as its index (index 1) was called 2 times in the first and the third session. The number of visits for the page level variable type =”product” is 1 as this time the page level variable was applied after the visitor level variable and thus this time the visitor level variable can’t override the page level variable. Finally the number of visit for the session-level variable converted=”false” will be 1 as its index was called only when the session 2 was happening.

So if on your website there is a session-level variable of login and then only a person can access the product page which is a page level variable you can very well use the above technique to execute the same with both the types of variables working in a single index.

If there are more than 1 page-level variables in a single session for a single index, all the page-level variables will be applied.

I know it was tough getting all this nerdy stuff, bear with me this the last time and then you can call yourself a pro in custom variables.

Suppose one last time the

The visitor level variable takes over the others if its index number is called.

If the page level variable comes after the visitor level variable it won't override and would show as being triggered despite being in the same index number.

There are some recommended practices by Google ;

  • Do not use duplicate key names across slots.
  • Call the _setCustomVar()function when it can be set prior to a pageview or event GIF request.
    In certain cases this might not be possible, and you will need to set another _trackPageview() requestafter setting a custom variable. This is typically only necessary in those situations where the user triggers a session- or visit-level custom var, where it is not possible to bundle that method with a pageview, event, or ecommerce tracking call.
  • Use a slot matrix to track large numbers of custom variables.
    If you have complex tracking requirements, where you have a mix of page- and session-level variables that might collide, you should build a slot matrix to ensure that session-level variables do not inadvertently over-ride page-level variables.
  • Consider using Event Tracking for certain applications, rather than custom variables.
    For example, suppose you have an online music store and you want to track both login sessions, purchase attempt sessions, and sessions where music samples were played. It would make sense to use Event Tracking to track the number of attempts to play music rather than using session-level variables to achieve this. Here, you could use the 4th value parameter of the event tracking call to pass in session data from your own cookies.
  • Don’t use session-level variables to track behavior you can track with page-level variables.
    For example, suppose you track login status and shopping attempt status by sessions, and your site offers a “Members’ special” page that you also want to track. Since a page-level custom variable will show the number of visits to that particular variable, you will already have available the number of visits that included that page at least once.

Ankit Bagga

Related Blogs

Google Analytics: Multi-Channel Attribution Models

Google Analytics: Multi-Channel Attribution Models

So when it comes to attribution modeling, it’s got to get ugly and we all know it. It’s like a typical discussion on UX where no matter who you are, you will always have something to say. I have seen some excellent attribution models and some very ugly ones and the one thing that differentiated […]

Read More »
Marketer’s guide to Markov and Shapley Multi-Channel Attribution models

Marketer’s guide to Markov and Shapley Multi-Channel Attribution models

When it comes to multi-channel attribution models, the 2 most common models are Markov and Shapley models. While there are plenty of blogs talking about these models along with the Python libraries to implement them, it was a struggle to find resources that talk about these models, not to a data scientist but a marketer. […]

Read More »

Subscribe to my newsletter

[contact-form-7 id="919" title="my form"]

Form

Copyright © 2020. AnkitBagga. All rights reserved.
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram