Closed (fixed)
Project:
Google Analytics
Version:
6.x-1.x-dev
Component:
Documentation
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
19 May 2008 at 20:43 UTC
Updated:
28 Sep 2008 at 19:51 UTC
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
Comment #1
hass commentedI'm not yet sure if
$show_ga = TRUE;is correct... and i haven't tested$visibility = 1;Comment #2
hass commentedComment #3
hass commentedThere was a small bug...
Comment #4
Shai commentedHass,
this looks really great.
I'll test later tonight. Looks great though. Very nice, very flexible and strong.
Shai
Comment #5
Shai commentedHass,
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
Comment #6
hass commentedFor D5 i have backported
drupal_match_pathas_googleanalytics_match_path():Comment #7
hass commentedNew 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.
Comment #8
hass commentedI'm marking this now as fixed.
Comment #9
Anonymous (not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.