You are here

function _matomo_visibility_pages in Matomo Analytics 8

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

Tracking visibility check for pages.

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

1 call to _matomo_visibility_pages()
matomo_page_attachments in ./matomo.module
Implements hook_page_attachments().

File

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

Code

function _matomo_visibility_pages() {
  static $page_match;

  // Cache visibility result if function is called more than once.
  if (!isset($page_match)) {
    $config = \Drupal::config('matomo.settings');
    $visibility_request_path_mode = $config
      ->get('visibility.request_path_mode');
    $visibility_request_path_pages = $config
      ->get('visibility.request_path_pages');

    // Match path if necessary.
    if (!empty($visibility_request_path_pages)) {

      // Convert path to lowercase. This allows comparison of the same path
      // with different case. Ex: /Page, /page, /PAGE.
      $pages = mb_strtolower($visibility_request_path_pages);
      if ($visibility_request_path_mode < 2) {

        // Compare the lowercase path alias (if any) and internal path.
        $path = \Drupal::service('path.current')
          ->getPath();
        $path_alias = mb_strtolower(\Drupal::service('path_alias.manager')
          ->getAliasByPath($path));
        $page_match = \Drupal::service('path.matcher')
          ->matchPath($path_alias, $pages) || $path != $path_alias && \Drupal::service('path.matcher')
          ->matchPath($path, $pages);

        // When $visibility_request_path_mode has a value of 0, the tracking
        // code is displayed on all pages except those listed in $pages. When
        // set to 1, it is displayed only on those pages listed in $pages.
        $page_match = !($visibility_request_path_mode xor $page_match);
      }
      elseif (\Drupal::moduleHandler()
        ->moduleExists('php')) {
        $page_match = php_eval($visibility_request_path_pages);
      }
      else {
        $page_match = FALSE;
      }
    }
    else {
      $page_match = TRUE;
    }
  }
  return $page_match;
}