Google Analytics Custom Segmentation by Role in Drupal

At the company I work for, I was already using Google Analytics to track visitors to the website, but I wanted a little more information. I wanted to see how many visitors were staff, versus other visitors.

Google Analytics tracking drupal users by role, one week view

Put the Pieces on the Table

The first piece we needed was already in place. We are using Drupal for our website, and every staff member needs to log in from time to time for various staff only resources. Regardless of any other permissions they may have, everyone who is staff is made part of the role “staff”.

Google lets you add up to 5 custom variables to your Google Analytics tracking with the _setCustomVar() function. There are 4 parameters: Slot Number, Variable Name, Variable Value, Variable Scope.

The Variable Scope can be set to 1,2 or 3. 1 is for visitor level scope. A variable with scope 1 will be tracked even if the user has logged out of the site. 2 is session level scope. A variable with scope 2 will be tracked as long as their current session lasts — usually till they close their browser. 3 is page-level scope, which I did not use.

I decided to set two variables, a Visitor scope variable, and a Session scope variable. This way I could not only tell when users were logged in, but compare logged in staff visits to logged out staff visits. I needed to only set these variables when the user is logged in however, which takes us to the next step…

Putting the Puzzle Together

Granted, this isn’t really a hard puzzle, each piece is really simple…which is probably why when I searched for how to do it initially I didn’t find a tutorial .

All we do now is add a piece of PHP to our footer to print the JavaScript if the user belongs to the ‘staff’ role. Here’s our whole footer block including our the Google Analytics JavaScript code.

<div>Corporate Office | Michael Moore , President | Fridley, MN 55432</div>
<div>123-456-7890 (voice, TTY) | 123-456-7890 (fax) | <a href='/contact'>Complete contact information</a></div>
<div>Copyright &copy;</div>

<script type="text/javascript">
 var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "' type='text/javascript'%3E%3C/script%3E"));
<script type="text/javascript">
 var pageTracker = _gat._getTracker("XX-1234567-8");
global $user;
if($user->uid && in_array('staff',$user->roles)){
 // Identify this as a staff computer, even if they're not logged in so we can see how many staff use the website while not logged in
 $js_string = 'pageTracker._setCustomVar(1,"StaffComputer","True",1);' . "\n"; 

 // Identify this session as a logged in staff member session
 $js_string .= 'pageTracker._setCustomVar(2,"LoggedInStaff","True",2);' . "\n"; 

 print $js_string;
} catch(err) {}

Stepping Back and Looking at the Results

The last thing to do is to set up your segmentation in Google Analytics. In the left column of Analytics you should see Advanced Segments in the My Customizations area. Once you have clicked that, choose Create a new custom segment.  The custom variables you defined in the steps above should be listed under the Visitors area of the Dimensions section. Drag the custom key and/or values desired to your segment creator, then save it.

If you are tracking company computers for logged off users as I am, you will need to wait a few days or weeks for all of the appropriate cookies to be set. The cookies aren’t set unless the user is logged in, and not all of our staff members log in every day.


This entry was posted in Computers, Programming and tagged , , , , . Bookmark the permalink.

2 Responses to Google Analytics Custom Segmentation by Role in Drupal

  1. Alex says:

    Very good post!

    First, Sorry for my bad english (I’m spanish).

    A question: Where did you put this code? only in footer of Staff’s pages?
    I work in a project that web has a lot of role users. How can i track separately all role users ?

    Thanks a lot!,

Leave a Reply

Your email address will not be published. Required fields are marked *