412.343.3692
1.800.975.1844

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.

Here is a technique (and the JavaScript) that I have used when I’ve needed to overcome this particular limitation.

How it Works

With the old version of the Tracking Code using urchin.js, we set the variable with:

_utmSetVar('someValue');

And in the new version using ga.js:

pageTracker._setVar('someValue');

But instead of using those, we’re going to use a new function:

superSetVar('someValue');

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:

superSetVar('/eyes=blue');
superSetVar('/hair=blonde');

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.

Naming Conventions

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:

unSetVar('/status=');
superSetVar('/status=member');

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.

Implementation:

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:
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type=”text/javascript” src=”/path/to/super_set_var.js”></script>
<script type=”text/javascript”>
var pageTracker = _gat._getTracker(”UA-xxxx-y”);
. . .
</script>

3b. For the old version of the GATC:
<script src="http://www.google-analytics.com/urchin.js"
type="text/javascript"></script>
<script type=”text/javascript” src=”/path/to/super_set_var.js”></script>
<script type=”text/javascript”>
_uacct = “UA-12345-1″;
. . .
</script>

4. Use superSetVar(”/someName=someValue”); wherever you would have used pageTracker._setVar(”someValue”); or _utmSetVar(”someValue”);

Technical Note:
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.

-John

Share and Enjoy:
  • Digg
  • del.icio.us
  • StumbleUpon
  • Sphinn
  • Facebook

5 Responses to “Stuff More Than One Value into GA’s User Defined Segment”

  1. André Scholten Says:

    I like this solution, but is had some disadvantages. I want to track someone’s search behaviour with this solution. The main search function has 3 pulldown menu’s, and I want to know which options are used most.

    The pulldowns have 50, 50 and 20 options, that makes 50.000 combinations. And with the 500 rows max export function I can’t report all of them. Right now I created 3 different profiles that track each pulldown separately, that works slightly better except from the fact that I can’t combine that info easy.

  2. Leonardo Naressi Says:

    Reallly good solution John!
    I used to to do this dirty stuff job only with filters, but, reading cookies was the next step…
    But, this solution works with multiple-domains implementations? this cookie-reading function works when combined with setDomainName “none”, or, even, cookiePath attribution?
    Thanks!
    Best,
    Leo Naressi

  3. John Says:
    Leonardo,

    As it is, it doesn’t account for setting the domain name or cookie path, and just uses the default of the full domain name. This is something I should have covered in the post. You have a few options. You can alter the super_set_var.js file to set the domain name and cookie path to what you are using. For example:

    var superSetVarTracker = _gat._getTracker(”UA-1-1″); // set up a tracker to call _setVar from
    superSetVarTracker._setDomainName(’domain.com’);
    superSetVarTracker._initData();
    superSetVarTracker._setVar(newVar); // call _setVar with the new value

    An alternative is instead of using this fake UA number and separate tracker, just use whatever tracker is already defined. in which case instead of the above, you would just have something like

    pageTracker._setVar(newVar);

    Doing that would automatically take into account what your domain was set to or what cookie path you were using.

    Sorry for any confusion there.

  4. Winston Says:

    Any idea how to populate user defined variable through the url?

    For instance, http://some…site.com/?utm_campaign=This+one&utm_userdefined=That+one

    Could parse the url values and call the setVar function, just wondering if there’s a more efficient way..

    Cheers,
    Winston

  5. Leonardo Naressi Says:

    Good Work John! It inspired me a lot.

    And Winston, You have read my mind!
    Populate user defined trough URL is a good way to handle specific situations.
    It´s not so hard to do, just iterate the parameters and using the superSetVar to add the variables…

    Good Luck, soon i will try this too…

    Best,
    Leonardo

Leave a Reply