You are here

function _google_analytics_visibility_pages in Google Analytics 8.2

Same name and namespace in other branches
  1. 8.3 google_analytics.module \_google_analytics_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 _google_analytics_visibility_pages()
google_analytics_page_attachments in ./google_analytics.module
Implements hook_page_attachments().

File

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

Code

function _google_analytics_visibility_pages() {
  static $page_match;

  // Cache visibility result if function is called more than once.
  if (!isset($page_match)) {
    $config = \Drupal::config('google_analytics.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;
}