Stuff More Than One Value into GA’s User Defined Segment
The User Defined Segment Variable in Google Analytics allows us to give each visitor to a site a distinctive label. Member or Non-member, Male or Female, Large or Small. This value is then stored in a cookie called __utmv on the visitors computer. Each time the visitor comes to your site, the Tracking Code checks for a value in that cookie and sends it along with the rest of the data.
The More-Than-One-Value Problem
But sometimes you would like to keep track more than one piece of information in GA’s User Defined Segment. You might want to know when you have a Small Female Non-member or a Large Male Member.
Most of you already know that Google Analytics only gives us one User Defined Variable. At first glance, that isn’t an issue, we can just set our variable to ‘Small/Female’ and now we have more than once piece of information about that visitor, right?
But you don’t always learn every piece of information at the same time. And whenever you set the value of the variable, it overwrites the previous value.
How it Works
With the old version of the Tracking Code using urchin.js, we set the variable with:
And in the new version using ga.js:
But instead of using those, we’re going to use a new function:
This function will check to see if the visitor already has ‘someValue’ assigned. If the value is already there, the function doesn’t do anything. But if it is not there, it adds this value to the end.
For example, if we set two values in a row with superSetVar:
instead of the second value overwriting the first, and making the User Defined Segment Variable equal to ‘/hair=blonde’, with superSetVar it is equal to ‘/eyes=blue/hair=blonde’.
In this way, we can use the superSetVar function whenever we need to add a piece of information to the User Defined Segment Variable in the __utmv cookie, and each time, the new value gets added to the end.
This brings us to some naming conventions. You’ll notice that I’m using the format /name=value for my variables. Aside from helping us visually in the GA reporting interface, and giving us a little something extra to work with when setting up filters, it also assists us in our next task – unsetting a value.
[This naming convention is required in order for everything to work as advertised. However, feel free to alter the code within the super_set_var.js to suit your own needs.]
With the original GA functions, we donâ€™t need to unset our variable, since every time we set a value, it overwrites the previous value — a sort-of built-in unset. So if you have a value of NonMember and later become a Member, the NonMember values gets overwritten and you just end up with ‘Member’. But if you used superSetVar you would end up with something like /status=nonmember/status=member.
Because of this we need to have another function, unSetVar. You could use it like this:
The unSetVar finds any instance of ‘/status=something’ and deletes it. So if you wanted to change someone’s eye color:
unSetVar('/eyes='); // clears all /eyes=someColor
superSetVar('/eyes=green'); // adds '/eyes=green' to the end of the __utmv cookie
What value will show up in the reporting interface?
Sometimes figuring out what you’re going to see in the reporting interface is a little tricky. With this method of setting the User Segment Variable, it should work like this:
If the visitor does not have a value already defined, then the first value that you give them will be used.
This means that if you call superSetVar multiple times, the latter values won’t show up, until the user comes back for another session.
However, if the visitor already has a value for the User Defined Segment Variable when they arrive at your site, then that value will be used. If you set additional values, they won’t show up in the GA Reporting Interface until the users next session.
1. Download super_set_var.js
2. Copy it to your web server.
3a. For the new version of the GATC, add the line in bold to your GATC after the call to ga.js, making sure to change the /path/to/super_set_var.js to the location on your webserver where you put that file in step 2:
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
var pageTracker = _gat._getTracker("UA-xxxx-y");
. . .
3b. For the old version of the GATC:
_uacct = "UA-12345-1";
. . .
4. Use superSetVar(“/someName=someValue”); wherever you would have used pageTracker._setVar(“someValue”); or _utmSetVar(“someValue”);
This implementation chooses to set the __utmv cookie with a pageTracker._setVar(‘â€¦’); that is associated with a ‘fake’ UA number. You could instead choose to alter the code to write the cookie by hand, or to use a _setVar(‘â€¦’); that is associated with the actual UA number for the account.
If you have any comments, improvements, alternatives, etc, please post them below. I’m sure some of you have had the opportunity to do this before, so share any pitfalls you’ve encountered or just tell me why your method is better than mine.