You are here

function _google_analytics_visibility_roles in Google Analytics 8.2

Same name and namespace in other branches
  1. 8.3 google_analytics.module \_google_analytics_visibility_roles()

Tracking visibility check for user roles.

Based on visibility setting this function returns TRUE if JS code should be added for the current role and otherwise FALSE.

Parameters

object $account: A user object containing an array of roles to check.

Return value

bool TRUE if JS code should be added for the current role and otherwise FALSE.

2 calls to _google_analytics_visibility_roles()
google_analytics_form_user_form_alter in ./google_analytics.module
Implements hook_form_FORM_ID_alter().
_google_analytics_visibility_user in ./google_analytics.module
Tracking visibility check for an user object.

File

./google_analytics.module, line 612
Drupal Module: Google Analytics.

Code

function _google_analytics_visibility_roles($account) {
  $config = \Drupal::config('google_analytics.settings');
  $enabled = $visibility_user_role_mode = $config
    ->get('visibility.user_role_mode');
  $visibility_user_role_roles = $config
    ->get('visibility.user_role_roles');
  if (count($visibility_user_role_roles) > 0) {

    // One or more roles are selected.
    foreach (array_values($account
      ->getRoles()) as $user_role) {

      // Is the current user a member of one of these roles?
      if (in_array($user_role, $visibility_user_role_roles)) {

        // Current user is a member of a role that should be tracked/excluded
        // from tracking.
        $enabled = !$visibility_user_role_mode;
        break;
      }
    }
  }
  else {

    // No role is selected for tracking, therefore all roles should be tracked.
    $enabled = TRUE;
  }
  return $enabled;
}