### Eclipse Workspace Patch 1.0
#P drupal-contrib-6--1
Index: modules/google_analytics/googleanalytics.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.admin.inc,v
retrieving revision 1.2.2.28
diff -u -r1.2.2.28 googleanalytics.admin.inc
--- modules/google_analytics/googleanalytics.admin.inc	29 Apr 2008 23:06:56 -0000	1.2.2.28
+++ modules/google_analytics/googleanalytics.admin.inc	1 May 2008 11:50:12 -0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: googleanalytics.admin.inc,v 1.2.2.28 2008/04/29 23:06:56 hass Exp $
+// $Id: googleanalytics.admin.inc,v 1.2.2.25 2008/04/23 20:41:24 hass Exp $
 
 /**
  * @file
@@ -33,28 +33,43 @@
     '#description' => t('This will enable Legacy Google Analytics (urchin.js) instead of most recent (ga.js).<br />Please note that Legacy Google Analytics will not receive feature updates and is not compatible with new features.'),
   );
 
-  // Render the role overview.
-  $form['roles'] = array(
+  // Standard tracking configurations.
+  $form['user_vis_settings'] = array(
     '#type' => 'fieldset',
-    '#title' => t('User role tracking'),
+    '#title' => t('User specific tracking settings'),
     '#collapsible' => TRUE,
-    '#description' => t('Define what user roles should be tracked by Google Analytics.'),
+  );
+  $form['user_vis_settings']['googleanalytics_custom'] = array(
+    '#type' => 'radios',
+    '#title' => t('Custom tracking settings'),
+    '#options' => array(
+      t('Users cannot control whether or not they add tracking.'),
+      t('Add tracking by default, but let individual users hide it.'),
+      t('Hide tracking by default, but let individual users add it.')
+    ),
+    '#description' => t('Allow individual users to customize the visibility of tracking in their account settings.'),
+    '#default_value' => variable_get('googleanalytics_custom', 0),
   );
 
-  $form['roles']['googleanalytics_track__user1'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Admin (user 1)'),
-    '#default_value' => variable_get('googleanalytics_track__user1', FALSE)
+  // Render the role overview.
+  $form['role_vis_settings'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Role specific tracking settings'),
+    '#collapsible' => TRUE,
   );
 
   $roles = user_roles();
-  foreach ($roles as $rid => $rname) {
-    $form['roles']['googleanalytics_track_'. $rid] = array(
-      '#type' => 'checkbox',
-      '#title' => check_plain($rname),
-      '#default_value' => variable_get('googleanalytics_track_'. $rid, FALSE),
-    );
-  }
+  $role_options = array();
+  foreach ($roles as $rid => $name) {
+    $role_options[$rid] = $name;
+  }
+  $form['role_vis_settings']['googleanalytics_roles'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Add tracking for specific roles'),
+    '#default_value' => variable_get('googleanalytics_roles', array()),
+    '#options' => $role_options,
+    '#description' => t('Add tracking only for the selected role(s). If you select no roles, the tracking will be added to all users.'),
+  );
 
   // Page specific visibility configurations.
   $form['page_vis_settings'] = array(
Index: modules/google_analytics/googleanalytics.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.install,v
retrieving revision 1.4.2.12
diff -u -r1.4.2.12 googleanalytics.install
--- modules/google_analytics/googleanalytics.install	23 Apr 2008 20:41:24 -0000	1.4.2.12
+++ modules/google_analytics/googleanalytics.install	1 May 2008 11:50:12 -0000
@@ -2,17 +2,6 @@
 // $Id: googleanalytics.install,v 1.4.2.12 2008/04/23 20:41:24 hass Exp $
 
 function googleanalytics_install() {
-  $result = db_query('SELECT * FROM {role} ORDER BY name');
-
-  while ($role = db_fetch_object($result)) {
-    $ga_role = 'googleanalytics_track_'. $role->rid;
-
-    // check if variable is not already set from a previous install
-    if (strpos(variable_get($ga_role, 'new'), 'new') !== FALSE) {
-      variable_set($ga_role, FALSE);
-    }
-  }
-
   variable_set('googleanalytics_visibility', 0);
 
   // Remove tracking from all administrative pages, see http://drupal.org/node/34970.
@@ -23,7 +12,6 @@
     'node/*/*',
   );
   variable_set('googleanalytics_pages', implode("\n", $pages));
-
 }
 
 function googleanalytics_uninstall() {
@@ -33,13 +21,14 @@
   variable_del('googleanalytics_legacy_version');
   variable_del('googleanalytics_codesnippet');
   variable_del('googleanalytics_segmentation');
-  variable_del('googleanalytics_track__user1');
   variable_del('googleanalytics_trackfiles');
   variable_del('googleanalytics_trackfiles_extensions');
   variable_del('googleanalytics_cache');
   variable_del('googleanalytics_last_cache');
   variable_del('googleanalytics_site_search');
   variable_del('googleanalytics_js_scope');
+  variable_del('googleanalytics_custom');
+  variable_del('googleanalytics_roles');
   variable_del('googleanalytics_visibility');
   variable_del('googleanalytics_pages');
 }
@@ -86,3 +75,35 @@
 
   return $ret;
 }
+
+/**
+ * Upgrade role settings and per user tracking settings 
+ * of "User 1" and remove outdated tracking variables.
+ */
+function googleanalytics_update_6002() {
+  $ret = array();
+
+  // Upgrade enabled/disabled roles to new logic (correct for upgrades from 5.x-1.4 and 6.x-1.0).
+  $roles = array();
+  foreach (user_roles() as $rid => $name) {
+    if (variable_get('googleanalytics_track_'. $rid, FALSE)) {
+      // Role ID is activated for user tracking.
+      $roles[$rid] = $rid;
+    }
+  }
+  variable_set('googleanalytics_roles', $roles);
+
+  // Upgrade disabled tracking of "user 1" to new logic.
+  if (!$track_user1 = variable_get('googleanalytics_track__user1', 1)) {
+    variable_set('googleanalytics_custom', 1);
+
+    // Load user 1 object, set appropiate value and save new user settings back.
+    $account = user_load(array('uid' => 1));
+    $account = user_save($account, array('googleanalytics' => array('custom' => 0)), 'account');
+  }
+
+  // Delete outdated tracking settings.
+  $result = db_query("DELETE FROM {variable} WHERE name LIKE 'googleanalytics_track_%'");
+
+  return $ret;
+}
Index: modules/google_analytics/googleanalytics.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.module,v
retrieving revision 1.16.2.39
diff -u -r1.16.2.39 googleanalytics.module
--- modules/google_analytics/googleanalytics.module	29 Apr 2008 22:31:32 -0000	1.16.2.39
+++ modules/google_analytics/googleanalytics.module	1 May 2008 11:50:12 -0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: googleanalytics.module,v 1.16.2.39 2008/04/29 22:31:32 hass Exp $
+// $Id: googleanalytics.module,v 1.16.2.37 2008/04/23 20:50:17 hass Exp $
 
 /*
  * Drupal Module: GoogleAnalytics
@@ -19,7 +19,7 @@
 }
 
 function googleanalytics_perm() {
-  return array('administer google analytics', 'use PHP for tracking visibility');
+  return array('administer google analytics', 'administer own tracking configuration', 'use PHP for tracking visibility');
 }
 
 function googleanalytics_menu() {
@@ -45,7 +45,7 @@
   // 1. Check if the GA account number has a value.
   // 2. Track page views based on visibility value.
   // 3. Check if we should track the currently active user's role.
-  if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_track($user)) {
+  if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_visibility_user($user)) {
     // Use the old version of Google Analytics?
     $legacy_version = variable_get('googleanalytics_legacy_version', FALSE);
     $scope = variable_get('googleanalytics_js_scope', 'footer');
@@ -80,7 +80,7 @@
 
   $id = variable_get('googleanalytics_account', '');
 
-  if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_track($user)) {
+  if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_visibility_user($user)) {
 
     // Use the old version of Google Analytics?
     $legacy_version = variable_get('googleanalytics_legacy_version', FALSE);
@@ -175,6 +175,35 @@
 }
 
 /**
+ * Implementation of hook_user().
+ *
+ * Allow users to decide if tracking code will be added to pages or not.
+ */
+function googleanalytics_user($type, $edit, &$account, $category = NULL) {
+  switch ($type) {
+    case 'form':
+      if ($category == 'account' && user_access('administer own tracking configuration') && ($custom = variable_get('googleanalytics_custom', 0)) != 0 && _googleanalytics_visibility_roles($account)) {
+        $form['googleanalytics'] = array(
+          '#type' => 'fieldset',
+          '#title' => t('Google Analytics configuration'),
+          '#weight' => 3,
+          '#collapsible' => TRUE,
+          '#tree' => TRUE
+        );
+        $form['googleanalytics']['custom'] = array(
+          '#type' => 'checkbox',
+          '#title' => t('User tracking'),
+          '#default_value' => isset($account->googleanalytics['custom']) ? $account->googleanalytics['custom'] : ($custom == 1)
+        );
+
+        return $form;
+      }
+      break;
+
+  }
+}
+
+/**
  * Implementation of hook_requirements().
  */
 function googleanalytics_requirements($phase) {
@@ -241,35 +270,64 @@
 }
 
 /**
+ * Tracking visibility check for an user object.
  *
  * @param $account
  *   A user object containing an array of roles to check.
  * @return boolean
- *   A decision on if the current user is being tracked by GAnalytics.
+ *   A decision on if the current user is being tracked by Google Analytics.
  */
-function _googleanalytics_track($account) {
-  // By default we don't track users.
-  $track = FALSE;
+function _googleanalytics_visibility_user($account) {
+
+  $enabled = FALSE;
+
+  // Is current user a member of a role that should be tracked?
+  if (_googleanalytics_visibility_roles($account)) {
 
-  foreach (array_keys($account->roles) as $role) {
-    // Add the tracking code if user is member of one role that should be tracked.
-    if (variable_get('googleanalytics_track_'. $role, FALSE)) {
-      $track = TRUE;
+    // Use the user's block visibility setting, if necessary.
+    if (($custom = variable_get('googleanalytics_custom', 0)) != 0) {
+      if ($account->uid && isset($account->googleanalytics['custom'])) {
+        $enabled = $account->googleanalytics['custom'];
+      }
+      else {
+        $enabled = ($custom == 1);
+      }
     }
+    else {
+      $enabled = TRUE;
+    }
+
   }
 
-  // Handle behavior for administrative user 1.
-  if ($account->uid == 1 && variable_get('googleanalytics_track__user1', FALSE)) {
-    // Enable tracking of user 1 if tracking for "authenticated user" is disabled.
-    $track = TRUE;
+  return $enabled;
+}
+
+/**
+ * Based on visibility setting this function returns TRUE if GA code should
+ * be added for the current role and otherwise FALSE.
+ */
+function _googleanalytics_visibility_roles($account) {
+
+  $enabled = FALSE;
+  $roles = variable_get('googleanalytics_roles', array());
+
+  if (array_sum($roles) > 0) {
+    // One or more roles are selected for tracking.
+    foreach (array_keys($account->roles) as $rid) {
+      // Is the current user a member of one role enabled for tracking?
+      if (isset($roles[$rid]) && $rid == $roles[$rid]) {
+        // Current user is a member of a role that should be tracked.
+        $enabled = TRUE;
+        break;
+      }
+    }
   }
-  elseif ($account->uid == 1 && !variable_get('googleanalytics_track__user1', FALSE)) {
-    // User 1 is a member of "authenticated user". Disable user tracking
-    // if user 1 shouldn't be tracked, but "authenticated user" should.
-    $track = FALSE;
+  else {
+    // No role is selected for tracking, therefor all roles should be tracked. 
+    $enabled = TRUE;
   }
 
-  return $track;
+  return $enabled;
 }
 
 /**
