You are here

public function GoogleAnalyticsEventTracker::isActive in Google Analytics Event Tracking 8

Same name and namespace in other branches
  1. 8.2 src/Entity/GoogleAnalyticsEventTracker.php \Drupal\google_analytics_et\Entity\GoogleAnalyticsEventTracker::isActive()

Returns whether the tracker is effective in the current page context.

Return value

boolean

Overrides GoogleAnalyticsEventTrackerInterface::isActive

File

src/Entity/GoogleAnalyticsEventTracker.php, line 91

Class

GoogleAnalyticsEventTracker
Defines the Google Analytics event tracker entity.

Namespace

Drupal\google_analytics_et\Entity

Code

public function isActive() {
  if (!$this
    ->status()) {
    return FALSE;
  }
  $paths = rtrim($this->paths);

  // If no paths provided this tracker will match all paths.
  if (empty($paths)) {
    return TRUE;
  }
  $path = \Drupal::service('path.current')
    ->getPath();
  $path_alias = \Drupal::service('path.alias_manager')
    ->getAliasByPath($path);
  $path_alias = Unicode::strtolower($path_alias);
  $matcher = \Drupal::service('path.matcher');
  $path_match = $matcher
    ->matchPath($path_alias, $this->paths) || $matcher
    ->matchPath($path, $paths);

  // When $tracker->path_negate has a value of 0, the asset is
  // added on all pages except those listed in $tracker->paths.
  // When set to 1, it is added only on those pages listed in
  // $tracker->paths.
  return !($this->path_negate xor $path_match);
}