Upcoming LunaMetrics Seminars
Los Angeles - Anaheim, Sep 8-12 Washington DC, Sep 22-26 Boston, Oct 6-10 Chicago, Oct 20-24

Extending User Defined Segmentation in GA

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.

Coming soon…Adding logic based on the actual value of the _utmv cookie.

About

http://www.lunametrics.com/blog/2008/02/18/extending-user-defined-segmentation-in-ga/

8 Responses to “Extending User Defined Segmentation in GA”

amit says:

Hi,
This was a post that is dealing with one of my key issues..
The thing is i couldn’t figure out of this, how to solve my problem..

That is because i lack the technical skills you perform here.

My issue is such:

I’m runing 2 major ad campaigns.
1- cpc
2 -Banners

I’d wish to add to each campaign’s landing page a _utmv cookie
so that i’ll be able to identify each stream (cpc or banners) and
his actions on site.

So far it’s quite easy i guess,
The thing is that in my goal page (thankyou.php) i have set
a utmv cookie as well…and probably some of the traffic
will complete conversion…and there for will get the utmv cookie designated
to the goal.

So…
Does that script can prevent the utmv cookie on the goal completion
page from runing over the utmv cookie assigned to the campaigns?

Robbin says:

Amit, there are two issues here:

1) Yes, the script will work, updated, of course, with your values (however you were denoting the setVar on the thankyou.php page instead of ‘member’ above)
2) You might consider not worrying about this at all for your particular setup. Here is why: If two setVars are set during the same session, or if someone starts a session and already has a setVar — a utmv cookie — the first setVar “wins” for that session. And since your second setVar is on the Thanks page, that means when someone converts — if they converted through the campaign landing page — your GA will always show that the campaign’s setVar gets credit. Your problem matters only if someone converts and then comes back again OR never had a utmv cookie set besides the one on the thanks page.

Peter says:

Hi, I am trying to work out if I could use GA to report:
1) tracking anonymous versus authenticated users, plus
2) tracking each user id.

We are building a web app with some anonymous and authenticated user functionality.

Could the User Defined be used here? I could have UD = for anonymous user and = for authenticated ones. Would that possibly work?

That cookie – can it be dropped after a session/browser restart?

Peter says:

Meant to say “I could have UD=blank for anonymous and =user id for autheticated ones”.

John says:

Peter,

You can certainly use the User Defined Segment Variable for that purpose.

Just set the variable for a visitor as they log in. pageTracker._setVar(“authenticated”);
For the anonymous visitors, you would not do anything — everyone not tagged as authenticated would be anonymous.

Although you could set the variable equal to each particular user’s id, it would violate the Google Analytics Terms of Service, which disallow you from tracking personally identifiable information.

I don’t think there is a simple way to change the timeout of the utmv cookie. It would probably have to be altered by some custom JavaScript.

Hope that helps,

John

Peter says:

John. much appreciated. Thanks.

SF says:

So, ┬┐when expires the utmv cookie? ┬┐After session? If one delete the cookies every day (as it set by the admin on all computers in my job place) then this cookie is not usefull. There are cookies that one can set the duration. I’m thinking “resize” for example (see in dynamic drive the javascript cookies section). Pls., confirm me if I understand the timeout of the cookie. The english isn’t my language (as u see :$). Thanks for your atention.

[...] internal traffic which is used for this article. For more advanced Google Analytics, you can have user defined segmentation. This entry was posted in adblock plus, google analytics. Bookmark the permalink. ← Paid [...]