You are here

function _matomo_visibility_roles in Matomo Analytics 8

Same name and namespace in other branches
  1. 7.2 matomo.module \_matomo_visibility_roles()

Tracking visibility check for user roles.

Based on visibility setting this function returns TRUE if Matomo 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 _matomo_visibility_roles()
matomo_form_user_form_alter in ./matomo.module
Implements hook_form_FORM_ID_alter().
_matomo_visibility_user in ./matomo.module
Tracking visibility check for an user object.

File

./matomo.module, line 613
Drupal Module: Matomo.

Code

function _matomo_visibility_roles($account) {
  $config = \Drupal::config('matomo.settings');
  $enabled = $visibility_user_role_mode = $config
    ->get('visibility.user_role_mode');
  $user_role_roles = $config
    ->get('visibility.user_role_roles');
  if (count($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, $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;
}