Roles opt-out AND path filter functionality: a snippet

Last modified: September 16, 2008 - 16:25

This sample script is built for Google Analytics 5.x-1.5+ and 6.x-1.1+:

You might have some special requirements for visibility of tracking code that cannot be configured with the standard UI. The following examples preserve the standard path filter functionality and allows you to opt-out roles from tracking. Upon site registration all users join the authenticated users. So, using the standard UI, it is impossible to track authenticated users while excluding users with one or more custom roles.

More sample PHP snippets that can be used in the Page specific tracking settings textarea (PHP mode) can be found on the handbook page Overview-approach to block visibility. However, they do not preserve path filtering feature. These snippets do.

A typical use case for the following example could be:

  1. You'd like to track authenticated users but exclude users with one or more roles (eg. webmaster).
  2. You'd like to preserve the path settings filter functionality for good reasons like privacy.

Drupal 6.x

<?php
// These are the roles that should not get the Google Analytics
// tracker code on pages they visit. Replace 'staff' etc. with
// your own roles you want to exclude.
$hide_roles = array(
 
'administrative user',
 
'staff',
 
'webmaster',
 
'editor'
);

// Page specific tracking settings:
//   0 = Add to every page except the listed pages (default).
//   1 = Add to the listed pages only.
$visibility = 0;

// Inclusion/exclusion list of pages.
$paths = array(
 
'admin',
 
'admin/*',
 
'user/*/*',
 
'node/add*',
 
'node/*/*'
);

// ****************************************************
// Don't touch the code below this lines if you don't
// know what you are doing!
// ****************************************************

// Assume all roles will have Google Analytics code.
$show_ga = TRUE;

// Check if the user have a role assigned that shouldn't
// be tracked. If so, set show_ga to FALSE.
global $user;
foreach(
$user->roles as $role){
  if(
in_array($role, $hide_roles)) {
   
$show_ga = FALSE;
  }
}

// Match path if necessary.
$pages = implode("\n", $paths);
$path = drupal_get_path_alias($_GET['q']);
// Compare with the internal and path alias (if any).
$page_match = drupal_match_path($path, $pages);
if (
$path != $_GET['q']) {
 
$page_match = $page_match || drupal_match_path($_GET['q'], $pages);
}
// When $visibility has a value of 0, the block is displayed on
// all pages except those listed in $pages. When set to 1, it
// is displayed only on those pages listed in $pages.
$page_match = !($visibility xor $page_match);

// If no roles were hit that aren't allowed to embed the GA code,
// this will still be true. Otherwise it will be false.
return ($show_ga && $page_match) ? TRUE : FALSE;
?>

Drupal 5.x

<?php
// These are the roles that should not get the Google Analytics
// tracker code on pages they visit. Replace 'staff' etc. with
// your own roles you want to exclude.
$hide_roles = array(
 
'administrative user',
 
'staff',
 
'webmaster',
 
'editor'
);

// Page specific tracking settings:
//   0 = Add to every page except the listed pages (default).
//   1 = Add to the listed pages only.
$visibility = 0;

// Inclusion/exclusion list of pages.
$paths = array(
 
'admin',
 
'admin/*',
 
'user/*/*',
 
'node/add*',
 
'node/*/*'
);

// ****************************************************
// Don't touch the code below this lines if you don't
// know what you are doing!
// ****************************************************

// Assume all roles will have Google Analytics code.
$show_ga = TRUE;

// Check if the user have a role assigned that shouldn't
// be tracked. If so, set show_ga to FALSE.
global $user;
foreach(
$user->roles as $role){
  if(
in_array($role, $hide_roles)) {
   
$show_ga = FALSE;
  }
}

// Match path if necessary.
$pages = implode("\n", $paths);
$path = drupal_get_path_alias($_GET['q']);
// Compare with the internal and path alias (if any).
$page_match = _googleanalytics_match_path($path, $pages);
if (
$path != $_GET['q']) {
 
$page_match = $page_match || _googleanalytics_match_path($_GET['q'], $pages);
}
// When $visibility has a value of 0, the block is displayed on
// all pages except those listed in $pages. When set to 1, it
// is displayed only on those pages listed in $pages.
$page_match = !($visibility xor $page_match);

// If no roles were hit that aren't allowed to embed the GA code,
// this will still be true. Otherwise it will be false.
return ($show_ga && $page_match) ? TRUE : FALSE;
?>

 
 

Drupal is a registered trademark of Dries Buytaert.