Tag Archives: Drupal

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; Stuporglue.org</div>

<script type="text/javascript">
<!--//--><![CDATA[//><!--
 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">
<!--//--><![CDATA[//><!--
try{
 var pageTracker = _gat._getTracker("XX-1234567-8");
<?php
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;
}
?>
pageTracker._trackPageview();
} catch(err) {}
//--><!]]>
</script>

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.

Enjoy!

Posted in Computers, Programming | Tagged , , , , | Leave a comment

Segmenting Drupal Users By Role in Google Analytics

We use Google Analytics and Drupal at work. We have been tracking our website usage for quite a while now, but we had no idea what percentage of our visitors were staff. We have 13 main locations, so IP filtering could’ve gotten us most of the way, but staff members do work from home and on the road quite a bit too. In the end I decided that the best route would be to use Google Analytics _setCustomVar funcitons to record the data we wanted.

Staff members at work have the role “staff”, so that’s what I wanted to select on.

I decided to set two custom vars. The first one would be a session level scope which should roughly identify if the user is logged in to the website. The second is a visitor level var which will track the user even if they log out or reboot their computer.

The way I made this happen was by putting the Google Analytics code in the footer block of the website, below the copyright notice. First set the The standard Google Analytics javascript goes in first, then we set our block to use PHP, and use PHP to conditionally add the custom vars we want.

<script type="text/javascript">
 <!--//--><![CDATA[//><!--
 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">
 <!--//--><![CDATA[//><!--
 try{
 var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
 <?php
 global $user;
 if($user->uid && in_array('staff',$user->roles)){
 // Identify this session as a logged in staff member session
 $js_string .= 'pageTracker._setCustomVar(2,"LoggedInStaff","True",2);' . "\n";  

 // 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"; 

 print $js_string;
 }
 ?>
 pageTracker._trackPageview();
 } catch(err) {}
 //--><!]]>
</script>

It ended up being very simple. Check if the $user object has a uid, if so it’be cause they’re logged in. Then check if the role we are interested in is in the user’s roles. If that’s the case, then print the conditional JavaScript.

Wait a few hours for some data to show up in Google Analytics and you can create custom segments based on these custom vars we just set up!

Posted in Programming, Projects | Tagged , , , | Leave a comment