You are here

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

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.

File

src/Helpers/VisiblityTracker.php, line 138

Class

VisiblityTracker
Defines the Path Matcher class.

Namespace

Drupal\google_analytics\Helpers

Code

public function getVisibilityPages() {
  static $page_match;

  // Cache visibility result if function is called more than once.
  if (!isset($page_match)) {
    $visibility_request_path_mode = $this->config
      ->get('visibility.request_path_mode');
    $visibility_request_path_pages = $this->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 = $this->currentPath
          ->getPath();
        $path_alias = mb_strtolower($this->aliasManager
          ->getAliasByPath($path));
        $page_match = $this->pathMatcher
          ->matchPath($path_alias, $pages) || $path != $path_alias && $this->pathMatcher
          ->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);
      }
      else {
        $page_match = FALSE;
      }
    }
    else {
      $page_match = TRUE;
    }
  }
  return $page_match;
}