This is a try to archive what have been discussed in http://drupal.org/node/248075#comment-845212

// 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 opt out.
$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*',
  '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", array($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;

Comments

hass’s picture

I'm not yet sure if $show_ga = TRUE; is correct... and i haven't tested $visibility = 1;

hass’s picture

Status: Active » Needs review
hass’s picture

There was a small bug...

// 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 opt out.
$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*',
  '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;
Shai’s picture

Hass,

this looks really great.

I'll test later tonight. Looks great though. Very nice, very flexible and strong.

Shai

Shai’s picture

Title: How to disable tracking of disabled roles » Roles Opt-Out Sample PHP Script With Path Settings Functionality Preserved

Hass,

I just noticed that D5 doesn't support drupal_match_path.

Previous issue title was: "How to disable tracking of disabled roles."

I've changed it to: "Roles Opt-Out Sample PHP Script With Path Settings Functionality Preserved"

This stuff is really hard to title, but I was confused by "How to disable tracking of disabled roles."

Shai

hass’s picture

For D5 i have backported drupal_match_path as _googleanalytics_match_path():

// 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 opt out.
$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*',
  '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;
hass’s picture

New handbook page for this is at http://drupal.org/node/261997.

Could we get a review and test of the above code? Additional we should write a simpletest for this special case in near future.

hass’s picture

Status: Needs review » Fixed

I'm marking this now as fixed.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.