GoogleAnalyticsEventTracking.php in Google Analytics Event Tracking 8
File
src/GoogleAnalyticsEventTracking.php
View source
<?php
namespace Drupal\google_analytics_et;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Path\AliasManagerInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\google_analytics_et\Entity\GoogleAnalyticsEventTrackerInterface;
class GoogleAnalyticsEventTracking {
protected $renderer;
protected $entityTypeManager;
protected $currentPath;
protected $currentRouteMatch;
protected $aliasManager;
protected $pathMatcher;
protected $languageManager;
public function __construct(RendererInterface $renderer, EntityTypeManagerInterface $entity_type_manager, CurrentPathStack $current_path, CurrentRouteMatch $current_route_match, AliasManagerInterface $alias_manager, PathMatcherInterface $path_matcher, LanguageManagerInterface $language_manager) {
$this->renderer = $renderer;
$this->entityTypeManager = $entity_type_manager;
$this->currentPath = $current_path;
$this->currentRouteMatch = $current_route_match;
$this->aliasManager = $alias_manager;
$this->pathMatcher = $path_matcher;
$this->languageManager = $language_manager;
}
public function addAttachments(&$attachments) {
if ($this::isGaAttached($attachments)) {
$et_attached = FALSE;
$renderer = $this->renderer;
$trackers = $this->entityTypeManager
->getStorage('google_analytics_event_tracker')
->loadMultiple();
foreach ($trackers as $tracker) {
if ($this
->isTrackerActive($tracker)) {
if (!$et_attached) {
$attachments['#attached']['library'][] = 'google_analytics_et/google_analytics_et';
}
$et_attached = TRUE;
if (empty($attachments['#attached']['drupalSettings'])) {
$attachments['#attached']['drupalSettings'] = [];
}
$attachments['#attached']['drupalSettings']['googleAnalyticsEt'][] = $tracker
->getJsSettings();
$renderer
->addCacheableDependency($attachments, $tracker);
}
}
}
}
public function isTrackerActive(GoogleAnalyticsEventTrackerInterface $tracker) {
return $this
->pathMatch($tracker) && $this
->languageMatch($tracker) && $this
->contentTypeMatch($tracker);
}
protected function pathMatch(GoogleAnalyticsEventTrackerInterface $tracker) {
if (!$tracker
->status()) {
return FALSE;
}
$paths = rtrim($tracker
->get('paths'));
if (empty($paths)) {
return TRUE;
}
$path = $this->currentPath
->getPath();
$path_alias = $this->aliasManager
->getAliasByPath($path);
$path_alias = Unicode::strtolower($path_alias);
$path_match = $this->pathMatcher
->matchPath($path_alias, $paths) || $this->pathMatcher
->matchPath($path, $paths);
return $tracker
->get('path_negate') xor $path_match;
}
protected function languageMatch(GoogleAnalyticsEventTrackerInterface $tracker) {
$languages = $tracker
->get('languages');
if (empty($languages)) {
return TRUE;
}
foreach ($languages as $id => $language) {
if ($id == $this->languageManager
->getCurrentLanguage()
->getId()) {
return TRUE;
}
}
return FALSE;
}
protected function contentTypeMatch(GoogleAnalyticsEventTrackerInterface $tracker) {
$content_types = $tracker
->get('content_types');
if (empty($content_types)) {
return TRUE;
}
if ($node = $this->currentRouteMatch
->getParameter('node')) {
foreach ($content_types as $key => $content_type) {
if ($content_type == $node
->getType()) {
return TRUE;
}
}
}
return FALSE;
}
public static function isGaAttached($attachments) {
if (!empty($attachments['#attached']['html_head'])) {
foreach ($attachments['#attached']['html_head'] as $attachment) {
if (!empty($attachment[1]) && $attachment[1] == 'google_analytics_tracking_script') {
return TRUE;
}
}
}
return FALSE;
}
}