diff --git a/facebook_pixel.admin.inc b/facebook_pixel.admin.inc index 5c9935b31f6c670ecc4c0e0ef22d249eb3eea69d..37b86122f3109027bd98dc0e63ed8a53d77d08bb 100755 --- a/facebook_pixel.admin.inc +++ b/facebook_pixel.admin.inc @@ -25,5 +25,19 @@ function facebook_pixel_admin_settings_form($form_state) { '#default_value' => variable_get('facebook_pixel_exclude_admin_paths', 1), ); + // Role specific visibility configurations. + $roles = user_roles(); + $role_options = array(); + foreach ($roles as $rid => $name) { + $role_options[$rid] = $name; + } + $form['facebook_pixel_roles'] = array( + '#type' => 'checkboxes', + '#title' => t('Remove Facebook Pixel for specific roles'), + '#default_value' => variable_get('facebook_pixel_roles', array()), + '#options' => $role_options, + '#description' => t('Remove script only for the selected role(s). If none of the roles are selected, all roles will have the Facebook Pixel. Otherwise, any roles selected here will NOT have the script.'), + ); + return system_settings_form($form); } diff --git a/facebook_pixel.module b/facebook_pixel.module index f55dff180a2b8bd08804fdb483722b37a884326c..6efbe28aff27ce763e3aab1bbc70c7f64b40ac95 100755 --- a/facebook_pixel.module +++ b/facebook_pixel.module @@ -125,7 +125,12 @@ function facebook_pixel_add_event($event) { * @see facebook_pixel_add_event() */ function facebook_pixel_preprocess_page(&$variables) { - if (variable_get('facebook_pixel_exclude_admin_paths', 1) && path_is_admin(current_path())) { + global $user; + + if ( + variable_get('facebook_pixel_exclude_admin_paths', 1) && path_is_admin(current_path()) + || !facebook_pixel_user_visibility($user) + ) { return; } @@ -216,3 +221,28 @@ function facebook_pixel_data_ViewContent($nid) { ); return drupal_json_encode($data); } + +/** + * Utility function to perform tracking visibility check for a user. + * + * @param object $user + * a user object containing an array of roles to check. + * @return boolean + * whether or not the current user should be served the pixel. + */ +function facebook_pixel_user_visibility($user) { + $visible = TRUE; + $roles = variable_get('facebook_pixel_roles', array()); + if (array_sum($roles) > 0) { + // One or more roles are selected for tracking. + foreach (array_keys($user->roles) as $rid) { + // Is the current user a member of a role selected in settings? + if (isset($roles[$rid]) && $rid == $roles[$rid]) { + // Current user is a member of a role that is selected in settings. + $visible = FALSE; + break; + } + } + } + return $visible; +}