summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhass2008-05-03 08:33:19 (GMT)
committer hass2008-05-03 08:33:19 (GMT)
commit681fdfe9b174176cf4c3c51590761f313b577630 (patch)
tree5eb792842fc54724ff7a5ce25d192e416966cf51
parent726433eb7ebd1114a871ec4bd7cbfd9854e941d2 (diff)
#248075: Tracking based on user and role specific settings
-rw-r--r--googleanalytics.admin.inc43
-rw-r--r--googleanalytics.install47
-rw-r--r--googleanalytics.module100
3 files changed, 142 insertions, 48 deletions
diff --git a/googleanalytics.admin.inc b/googleanalytics.admin.inc
index 4279791..f78a63c 100644
--- a/googleanalytics.admin.inc
+++ b/googleanalytics.admin.inc
@@ -33,28 +33,43 @@ function googleanalytics_admin_settings_form(&$form_state) {
'#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(
diff --git a/googleanalytics.install b/googleanalytics.install
index 4473653..a41f236 100644
--- a/googleanalytics.install
+++ b/googleanalytics.install
@@ -2,17 +2,6 @@
// $Id$
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 @@ function googleanalytics_install() {
'node/*/*',
);
variable_set('googleanalytics_pages', implode("\n", $pages));
-
}
function googleanalytics_uninstall() {
@@ -33,13 +21,14 @@ function googleanalytics_uninstall() {
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 @@ function googleanalytics_update_6001() {
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;
+}
diff --git a/googleanalytics.module b/googleanalytics.module
index 7575f72..34dfad6 100644
--- a/googleanalytics.module
+++ b/googleanalytics.module
@@ -19,7 +19,7 @@ function googleanalytics_help($path, $arg) {
}
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 @@ function googleanalytics_init() {
// 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 @@ function googleanalytics_footer($main = 0) {
$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 @@ function googleanalytics_footer($main = 0) {
}
/**
+ * 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 @@ function _googleanalytics_cache($location) {
}
/**
+ * 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;
-
- 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;
+function _googleanalytics_visibility_user($account) {
+
+ $enabled = FALSE;
+
+ // Is current user a member of a role that should be tracked?
+ if (_googleanalytics_visibility_roles($account)) {
+
+ // 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;
}
/**