You are here

public function VisiblityTracker::getVisibilityRoles in Google Analytics 4.x

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.

1 call to VisiblityTracker::getVisibilityRoles()
VisiblityTracker::getUserVisibilty in src/Helpers/VisiblityTracker.php
Tracking visibility check for an user object.

File

src/Helpers/VisiblityTracker.php, line 108

Class

VisiblityTracker
Defines the Path Matcher class.

Namespace

Drupal\google_analytics\Helpers

Code

public function getVisibilityRoles($account) {
  $enabled = $visibility_user_role_mode = $this->config
    ->get('visibility.user_role_mode');
  $visibility_user_role_roles = $this->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;
}