What is a custom variable and when do I use it? Custom variables are tags that you insert into your tracking code that allows you to be able to filter your Google Analytics tracking down to certain segments of your site. You will be able to define those sections and segment your visitors to get a better understanding of how they use your site. Running custom variables will allow you to gather data that Google Analytics does not provide in their standard set of reports.
We are going to look at custom variables in three parts. This post, Part I is on Session-Level variables or the period of time during which the visitor is active on the site. To track the actions of the visitor during their session, the actions are stored in a cookie which is deleted when the session cookie expires.
The most common use for this custom variable is for logged in members versus anonymous visitors.
As you are able to set up an assortment of custom variables to track user activity for your site, you’ll usually create your own JavaScript utilities to manage them. Your script will use the basic method for creating a custom variable as follows:
_setCustomVar(index, name, value, opt_scope)
This method accepts four parameters:
Example of the Custom Variable coding structure:
_gaq.push([‘_setCustomVar’,
1, // This custom var is set to slot #1. Required parameter.
‘User Type’, // The name of the custom variable. Required parameter.
‘Member’, // Sets the value of “User Type” to “Member” or “Visitor” depending on status. Required parameter.
2 // Sets the scope to session-level. Optional parameter.
]);
When you install the code it should look like this:
<script type=”text/javascript”>// <![CDATA[
var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-XXXXXX-1’]);
_gaq.push([‘_setCustomVar’,1,’VisitorType’,’Member’, 1]);
_gaq.push([‘_trackPageview’]);
(function() { var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true; ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘http://www’) + ‘.google-analytics.com/ga.js’; var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s); })();
// ]]>
</script>
The total combined length of any custom variable name and value may not exceed 64 bytes, which is not the same as characters. A space is coded as %20 which is 3 bytes, not 1.
Once your code is implemented you can find the custom variable reports under Audience>>Demographics>>Custom Variables.
Noteworthy
In our next post we will be discussing Page-Level Custom Variables.