This document provides setup and usage instructions for custom user segments.
Google Analytics provides a number of pre-defined segments for your visitors, such as geographic region and language preference. You can create custom segments based on your website needs. Once these are working, you can see the break-down of visitors based on custom segments by choosing the custom segment dimension in the Analytics reports. For example, you might want to segment visitors according to the following:
To create a custom visitor segment, call the _setVar() method
in your web page. You can attach this call to a form widget, within the tracking
code, or within a link to a page. Most typically, custom segments are set
by client-side or server-side logic that sets the variable depending on the
page being visited, or as a result of a selection on the part of the visitor,
such as logging in.
The following example shows two code snippets, where the first is a series of links to sections of a pet store website, and the second shows the custom user segment that gets set when a user selects a given link.
<!-- Links to different sections of the site --> <ul id="mainMenu"> <li><a href="/home/dogSupplies.html">Dog</a></li> <li><a href="/home/catSupplies.html">Cat</a></li> <li><a href="/home/fishSupplies.html">Fish</a></li> <li><a href="/home/birdSupplies.html">Bird</a></li> </ul>
<!-- Resulting page that sets a custom segment --> <html> <head> </head> <body> Website Content <script type="text/javascript">
function getUserPref() {
var pathName = window.location.pathname;
var startSlice = pathName.lastIndexOf('/') + 1;
var endSlice = pathName.lastIndexOf('.');
var pageName = pathName.slice(startSlice, endSlice);
var endPageSlice = pageName.indexOf('Supplies');
var petPreference = pageName.slice(0, endPageSlice);
var userType = petPreference + ' Lover';
return userType; } </script> <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"> try{
var pageTracker = _gat._getTracker("UA-xxxxx-x");
pageTracker._setVar(getUserPref());
pageTracker._trackPageview(); } catch(err) {} </script> </body> </html>
When custom visitor segmentation is working correctly on your site, a visitor cookie called __utmv is set to the selected value on the visitor's browser. This first-party cookie persists for 2 years unless it is over-written with a new value. (For more information on cookies in Google Analytics, see Cookies and Google Analytics.)
Customized visitor segments were designed for a specific type of use. To get the most out of using customized visitor segments, consider the following:
_setVar() value to "Non-member" and the login success page sets the value to "Member," two GIF requests are made, each with the respective value. (Note: Since this type of implementation sets and resets the custom segment values in a given session, it is an example of how NOT to implement customized visitor segments for member/non-members.)__utmv cookie, and the first associated value for the next session._setVar() method sets the custom user segment for a given page, the value defined in the __utmv cookie determines which custom segment is applied to the page view. (This value might exist from a previous session or had been set from a previous page view.) However, if a change is made to the custom visitor segment value in the middle of a session, the value attributed to that page and any following pages is the most recently defined value.
Using the same scenario, if the user first selects "Bird Lover," visits 4 pages, and then on page 6 of the session chooses "Dog Lover," and visits 4 more pages, the content reports will show 5 pages associated with "Bird Lover" and 5 pages associated with "Dog Lover," even though the user's session would be attributed to "Bird Lover."The following illustration shows a detailed break-down of how a custom visitor segment is applied to both user session and page view calculations across 3 sessions from a single visitor. Each session is described below.
__utmv value is applied to the session because none has been found and no _setVar() method is called on this page. __utmv cookie value is set to "Bird Lover." A GIF request also supplies the "Bird Lover" string via the _utmcc parameter.The user begins the session with Page 1. The __utmv cookie is read and the session data is attributed to "Bird Lover." Additionally, because the customized user segment value for that user was not altered on any pages for the session, all page 5 views in the session are attributed to the "Bird Lover" segment.
On page 2 of the third session, the customized user segment for the user is changed to "Dog Lover." However, the session data for the user is still applied to "Bird Lover" because it is the first association for the session, as defined in the __utmv cookie. While the data for Page 1 is attributed to "Bird Lover," all subsequent pages for the session are attributed to "Dog Lover."