Extending User Defined Segmentation In GA

February 18, 2008

The user defined segment in Google Analytics is a very underused feature. But those that do use it, are often frustrated because you can only use it once on a site. However, there are situations where it can be safely and easily used for multiple purposes. Here is a situation I encounted that makes an excellent example. It uses some JavaScript to determine if the User Defined Segment Variable (_utmv) is already being used and decides what to do accordingly.

Quick Refresher:

The user defined segment based on the value of the _utmv cookie.

You set the value of this cookie with the __utmSetVar() function in urchin.js or with pageTracker._setVar() if using ga.js.

To view the user defined segment in your analytics, go to >Visitors->User Defined.

Background:

You currently use the _utmv cookie to segment your employees’ actions from other visitors, as well as exclude them from certain profiles.
For our example, we set the emplyee’s cookie value to “notrack”. (Justin Cutroni has a great post explaining what this is and how to do it.)

Now, you want to track your members separately from other visitors. You’ve configured your login page to set the _utmv cookie to “member” upon successful authentication. Now, they’ll be tracked as a part of the “member” segment for any pages they view.

The Problem:

The problem is that once your employees log in, their “notrack” cookie is overwritten with the “member” cookie.

On their next visit, it’ll look like they’re a regular member. (We don’t want that.)

The Solution:

With a tiny bit of JavaScript, we can easily keep the “member” cookie from overwriting the “notrack” cookie when an employee logs in.

To set the _utmv cookie, we would normally just place the code below on the landing page after the user logs in.

– urchin.js

<body onload="javascript:__utmSetVar('member');">

– ga.js

<body onload="javascript:pageTracker._setVar('member');">

However, this would overwrite any _utmv cookie that was previously set. (ie, our “notrack” cookie)

We just need to add a short JavaScript function to see if the cookie already exists, and update our onload event to call the new function instead of __utmSetVar.


    <script type="text/javascript">
    //<![CDATA[
    function tagMembers()
    {
    var allcookies = document.cookie;
    var mycookie = allcookies.indexOf("__utmv");
        if (mycookie == -1)
            {
                    __utmSetVar('member');
            }
    }
    //]]>
    </script>

* For ga.js, just replace “__utmSetVar(‘member’);” with “pageTracker._setVar(‘member’);”.

Your body onload event should now look like the example below:

<body onload="tagMembers();">

What does it do?

Instead of automatically setting the _utmv cookie every time, we check to see if the cookie is already set. (It doesn’t matter what it’s set to. If it’s “member” already, we don’t need to set it again, and if it’s “notrack” we don’t want to set it again.

Now, when your employees log into your site, they’ll continue to be tracked or excluded as such.

I hope this helps to expand everyone’s use of the _utmv cookie. With a little logic, you don’t need to be bound to using the _utmv cookie for only one type of user. I welcome your questions, comments, and suggestions.