You are here

yandex_metrics.module in Yandex.Metrics 8.3

The main code of Yandex.Metrics Counter module.

File

yandex_metrics.module
View source
<?php

/**
 * @file
 * The main code of Yandex.Metrics Counter module.
 */
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;

/**
 * Implements hook_page_bottom().
 * Adds Yandex.Metrics counter code to the site footer.
 */
function yandex_metrics_page_bottom(array &$page_bottom) {
  $yandex_metrics_counter_code = Drupal::config('yandex_metrics.settings')
    ->get('counter_code');
  if (!empty($yandex_metrics_counter_code) && yandex_metrics_show_counter() && yandex_metrics_show_counter_for_role()) {
    $page_bottom['yandex_metrics'] = [
      '#type' => 'inline_template',
      '#template' => '<div class="ym-counter">{{ counter_code|raw }}</div>',
      '#context' => [
        'counter_code' => $yandex_metrics_counter_code,
      ],
      '#attached' => [
        'library' => [
          'yandex_metrics/counter',
        ],
      ],
    ];
  }
}

/**
 * Returns FALSE if we need to disable counter on page.
 * @return bool
 */
function yandex_metrics_show_counter() {
  $pages = Drupal::config('yandex_metrics.settings')
    ->get('visibility.path.pages');
  $visibility = Drupal::config('yandex_metrics.settings')
    ->get('visibility.path.visibility');
  $urls_equal = FALSE;
  if (!empty($pages)) {
    $pages_in_lowcase = Unicode::strtolower($pages);
    $current_path = Drupal::service('path.alias_manager')
      ->getAliasByPath(\Drupal::service('path.current')
      ->getPath());
    $current_path = Unicode::strtolower($current_path);

    // In D8 alias is with leading slash?
    // @todo: Make sure we follow D8 standards of aliases.
    $current_path = ltrim($current_path, "/");

    // Compare internal and path alias.
    $path_match = \Drupal::service('path.matcher')
      ->matchPath($current_path, $pages_in_lowcase);
    if ($path_match) {
      $urls_equal = TRUE;
    }
    else {

      // If path alias doesn't equal with current_path() then compare internal and current_path().
      $path_match = \Drupal::service('path.matcher')
        ->matchPath(\Drupal::service('path.current')
        ->getPath(), $pages_in_lowcase);
      if ($current_path != \Drupal::service('path.current')
        ->getPath() && $path_match) {
        $urls_equal = TRUE;
      }
    }
  }
  if (!$visibility && $urls_equal) {
    return FALSE;
  }
  elseif (!$visibility && !$urls_equal) {
    return TRUE;
  }
  elseif ($visibility && $urls_equal) {
    return TRUE;
  }
  elseif ($visibility && !$urls_equal) {
    return FALSE;
  }
}

/**
 * Returns FALSE if we need to disable counter for role.
 * @return bool
 */
function yandex_metrics_show_counter_for_role() {
  $user = \Drupal::currentUser();
  $visibility = (bool) Drupal::config('yandex_metrics.settings')
    ->get('visibility.role.visibility');
  $enabled = (bool) $visibility;
  $roles = Drupal::config('yandex_metrics.settings')
    ->get('visibility.role.roles');
  $has_active_role = FALSE;
  foreach ($roles as $key => $value) {
    if ($key === $value) {
      $has_active_role = TRUE;
      break;
    }
  }
  if ($has_active_role) {

    // One or more roles are selected.
    foreach ($user
      ->getRoles() as $rid) {

      // Is the current user a member of one of these roles?
      if (isset($roles[$rid]) && $rid === $roles[$rid]) {

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

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

/**
 * Implements hook_help().
 */
function yandex_metrics_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.yandex_metrics':
      $output = '';
      $output .= '<h3>' . t('About the module') . '</h3>';
      $output .= '<p>' . t('The Yandex.Metrics Counter module allows to install <a href="@yandex_metrika" target="_blank">Yandex.Metrics</a> counter code on the site pages.', array(
        '@yandex_metrika' => 'http://metrika.yandex.ru/',
      )) . '</p>';
      $output .= '<h3>' . t('Usage') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Installing counter') . '</dt>';
      $output .= '<dd>' . t('Yandex.Metrics counter is JavaScript code that monitors user behavior on your website. You should <a href="@create_counter" target="_blank">create</a> and <a href="@install_counter">install</a> the counter to work with Yandex.Metrics service.', array(
        '@create_counter' => 'http://metrika.yandex.ru/',
        '@install_counter' => Url::fromRoute('yandex_metrics.default')
          ->toString(),
      )) . '</dd>';
      $output .= '</dl>';
      return $output;
    case 'yandex_metrics.default':
      $output = '<p>' . t('Yandex.Metrics counter is JavaScript code that monitors user behavior on your website. You should <a href="@create_counter" target="_blank">create</a> and install the counter code to work with Yandex.Metrics service.', array(
        '@create_counter' => 'http://metrika.yandex.ru/',
      )) . '</p>';
      return $output;
  }
}

Functions

Namesort descending Description
yandex_metrics_help Implements hook_help().
yandex_metrics_page_bottom Implements hook_page_bottom(). Adds Yandex.Metrics counter code to the site footer.
yandex_metrics_show_counter Returns FALSE if we need to disable counter on page.
yandex_metrics_show_counter_for_role Returns FALSE if we need to disable counter for role.