Last updated March 25, 2011. Created by hass on May 23, 2008.
Edited by EvanDonovan, Shai, aclight. Log in to edit this page.
This sample script is built for Google Analytics 5.x-1.5+ and 6.x-1.1+ and is no longer required if you are using Google Analytics 6.x-3.2 or 7.x-1.1 or later. This became a build in feature with this releases.
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:
- You'd like to track
authenticated usersbut exclude users with one or more roles (eg.webmaster). - You'd like to preserve the path settings filter functionality for good reasons like privacy.
Drupal 6.x, 7.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;
?>
Comments
A single version for Drupal 5 users
May I suggest using this single version instead - particularly if you have drupal 5. That way when you upgrade your new install won't white screen because the _googleanalytics_match_path function doesn't exist.
<?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).
if (function_exists('_googleanalytics_match_path')) {
$page_match = _googleanalytics_match_path($path, $pages);
if ($path != $_GET['q']) {
$page_match = $page_match || _googleanalytics_match_path($_GET['q'], $pages);
}
} else {
$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;
?>
Location of Code
Hi,
Thank you for the above D6 code - it's exactly what I'm looking for.
One question - where would I insert this code? The custom javascript code block within my Google Analytics config page? If so, as a Code snippet (before) or Code snippet (after)?
Thank you.
--
Edit... Np. Worked it out.
Thank you.
I'm also confused as to where
I'm also confused as to where to place this code. Can someone clarify this?
- Alan Tutt
Exceptional Personal Development for Exceptional People
http://www.PowerKeysPub.com
Worth noting that for the
Worth noting that for the admin user (uid=1) not to be tracked, it needs one of the hidden roles to be assigned to it.
Does this code need to be changed for 6.x-3.x ?
Does this code need to be changed for 6.x-3.x ?
The answer to this is "No", as provided in the reply, http://drupal.org/node/931402#comment-3559300
Furthermore, I had interpreted the user permissions to "use php for tracking visibility" as only being needed for privileged users to configure the module. This is confirmed as a side issue in the thread, http://drupal.org/node/936684#comment-3566276
I was confused in this because I recently watched the video http://www.brianstevenson.com/drupal/screencasts/installing-googleanalytics which contains a screen showing the user permissions for this checked for all roles. I was going to add a screen capture but see files cannot be attached to comments.
Never mind, I will open a separate issue in the module queue, http://drupal.org/node/943612