Google Analytics custom segments

Submitted by Nikeev on Mon, 05/23/2016 - 16:53

There are a lot of projects with different site users types and we often need to have different segments in our analytics tools. First of all we need it for more detailed tracking of our visitors and better traffic sources performance reports. We faced with such problem in our marketplace of guides and business interpreters abroad - GloberLand.

For example, we have a website with freelancers search. If so, there are 3 types of users we could see:

  • Freelancers;
  • Clients;
  • Other visitors (anonymous).

It's easy for us to check user role on site itself, but could be a problem to determine users inside Google Analytics. As far as GA doesn't know anything about visitors types on our site. So we should pass this data to Analytics.

Let's begin. In Google Analytics control panel we should open our counter and go to Admin tab. Next click on PROPERTY - Custom Definitions - Custom Dimensions. On new page click + New Cutom Dimension.

Настройка кастомного параметра

Define the Name as UserType and set a Scope parameter as User. It will allow us to have our user type to be set for all user sessions. After it's done, you'll get setup code and dimension id. Something like dimension1, or another number if you already have custom dimensions.

Now we could configure our Google Analytics tracker code. Inside your GA trigger script add following:

  var userType = jQuery.cookie('userType');
  if (userType == null || (userType != siteUserType && userType == 'Other')) {
    jQuery.cookie('userType', siteUserType, {
      path: '/',
      expires: 730
    ga('set', 'dimension1', siteUserType);

Variable siteUserType is not defined here because you should make it available from your CMS or custom site engine you use. In our example it could have 3 variations: Freelancer, Client, Other.

Let me explain why there are also cookies usage and user type additional check. Logged in user at any time could log out and became anonymous. Without this check Google Analytics dimension will be updated by our script. That is bad, because all previous page views as Freelancer could be replaced as  Other. But we know that it is not correct and user is still Freelancer. So we don't let "downgrade" user account dimension type on user logout.

Once it's done, you should configure our new segments in Google Analytics interface. So, go to Reporting tab, open some analytics report and click on top block  + Add Segment. In default segments list choose + New Segment and set the name Client with such condition:

Настройка сегмента

Similarly do the same actions with other user types you have. Now you could use your new custom segments for better understanding site visitors and more effectively rule you acquisitions channels.

Hope this article was useful. Feel free to leave your notes and ideas in comments below.