DefaultCommandSubscriber.php in Googalytics - Google Analytics 8
File
src/EventSubscriber/DefaultCommandSubscriber.php
View source
<?php
namespace Drupal\ga\EventSubscriber;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\ga\AnalyticsCommand\Create;
use Drupal\ga\AnalyticsCommand\Linker\AutoLink;
use Drupal\ga\AnalyticsCommand\Pageview;
use Drupal\ga\AnalyticsCommand\RequirePlugin;
use Drupal\ga\AnalyticsCommand\Set;
use Drupal\ga\AnalyticsEvents;
use Drupal\ga\CommandRegistryService;
use Drupal\ga\Event\CollectEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class DefaultCommandSubscriber implements EventSubscriberInterface {
protected $commandRegistry;
protected $configFactory;
protected $currentUser;
protected $userStorage;
public static function getSubscribedEvents() {
return [
AnalyticsEvents::COLLECT => [
[
'onCollectDefaultCommands',
],
[
'onCollectRegisteredCommands',
],
],
];
}
public function __construct(ConfigFactoryInterface $configFactory, CommandRegistryService $commandRegistry, AccountInterface $currentUser, EntityTypeManagerInterface $entityTypeManager) {
$this->configFactory = $configFactory;
$this->commandRegistry = $commandRegistry;
$this->currentUser = $currentUser;
$this->userStorage = $entityTypeManager
->getStorage('user');
}
public function onCollectRegisteredCommands(CollectEvent $event) {
foreach ($this->commandRegistry
->getCommands() as $command) {
$event
->addCommand($command);
}
}
public function onCollectDefaultCommands(CollectEvent $event) {
$config = $this->configFactory
->get('ga.settings');
if ($config
->get('add_default_commands')) {
if (!$this
->checkRole($config
->get('user_roles.mode'), $config
->get('user_roles.roles'), $this->currentUser
->getRoles())) {
return;
}
if ($tracking_id = $config
->get('tracking_id')) {
$fieldsObject = [];
if ($config
->get('sample_rate') !== 100) {
$fieldsObject['sampleRate'] = $config
->get('sample_rate');
}
if ($config
->get('site_speed_sample_rate') !== 1) {
$fieldsObject['site_speed_sample_rate'] = $config
->get('site_speed_sample_rate');
}
if ($config
->get('force_ssl')) {
$fieldsObject['forceSSL'] = TRUE;
}
if ($config
->get('plugins.linker.enable')) {
$fieldsObject['allowLinker'] = TRUE;
}
if ($config
->get('track_user_id') && $this->currentUser
->isAuthenticated()) {
$account = $this->userStorage
->load($this->currentUser
->id());
$fieldsObject['userId'] = $account
->uuid();
}
if ($config
->get('anonymize_ip')) {
$fieldsObject['anonymizeIp'] = TRUE;
}
$event
->addCommand(new Create($tracking_id, 'auto', NULL, $fieldsObject));
}
else {
if ($config
->get('force_ssl')) {
$event
->addCommand(new Set('forceSSL', TRUE));
}
if ($config
->get('track_user_id') && $this->currentUser
->isAuthenticated()) {
$account = $this->userStorage
->load($this->currentUser
->id());
$event
->addCommand(new Set('userId', $account
->uuid()));
}
if ($config
->get('anonymize_ip')) {
$event
->addCommand(new Set('anonymizeIp', TRUE));
}
}
if ($config
->get('send_pageview')) {
$event
->addCommand(new Pageview());
}
if ($config
->get('plugins.linkid')) {
$event
->addCommand(new RequirePlugin('linkid'));
}
if ($config
->get('plugins.displayfeatures')) {
$event
->addCommand(new RequirePlugin('displayfeatures'));
}
if ($config
->get('plugins.linker.enable')) {
$event
->addCommand(new RequirePlugin('linker'));
if ($domains = $config
->get('plugins.linker.domains')) {
$event
->addCommand(new AutoLink($domains));
}
}
}
}
private function checkRole($mode, array $limitRoles, array $userRoles) {
switch ($mode) {
case 'disabled':
return TRUE;
case 'include':
$userRoleMode = TRUE;
break;
case 'exclude':
$userRoleMode = FALSE;
break;
default:
throw new \InvalidArgumentException("Mode must be one of 'include' or 'exclude'");
}
$userRoleCommon = array_intersect($limitRoles, $userRoles);
if ($userRoleMode ^ !empty($userRoleCommon)) {
return FALSE;
}
return TRUE;
}
}