class AnalyticsSubscriber in Googalytics - Google Analytics 8
Class DefaultCommandSubscriber.
Hierarchy
- class \Drupal\ga_tokens\EventSubscriber\AnalyticsSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of AnalyticsSubscriber
1 string reference to 'AnalyticsSubscriber'
- ga_tokens.services.yml in modules/
ga_tokens/ ga_tokens.services.yml - modules/ga_tokens/ga_tokens.services.yml
1 service uses AnalyticsSubscriber
File
- modules/
ga_tokens/ src/ EventSubscriber/ AnalyticsSubscriber.php, line 16
Namespace
Drupal\ga_tokens\EventSubscriberView source
class AnalyticsSubscriber implements EventSubscriberInterface {
/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Token service.
*
* @var \Drupal\Core\Utility\Token
*/
protected $token;
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
AnalyticsEvents::COLLECT => [
[
'onCollectGlobalProperties',
],
],
];
}
/**
* DefaultCommandSubscriber constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config factory service.
* @param \Drupal\Core\Utility\Token $token
* The Token service.
*/
public function __construct(ConfigFactoryInterface $configFactory, Token $token) {
$this->configFactory = $configFactory;
$this->token = $token;
}
/**
* Add global dimensions and metrics.
*
* @param \Drupal\ga\Event\CollectEvent $event
* The AnalyticsEvents::COLLECT event.
*/
public function onCollectGlobalProperties(CollectEvent $event) {
$config = $this->configFactory
->get('ga_tokens.global');
$dimensions = $config
->get('dimensions') ?: [];
foreach ($dimensions as $index => $dimension) {
$value = $this->token
->replace($dimension['value']);
$event
->addCommand(new Dimension($index, $value));
}
$metrics = $config
->get('metrics') ?: [];
foreach ($metrics as $index => $metric) {
$value = $this->token
->replace($metric['value']);
$event
->addCommand(new Metric($index, $value));
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AnalyticsSubscriber:: |
protected | property | The config factory service. | |
AnalyticsSubscriber:: |
protected | property | Token service. | |
AnalyticsSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
AnalyticsSubscriber:: |
public | function | Add global dimensions and metrics. | |
AnalyticsSubscriber:: |
public | function | DefaultCommandSubscriber constructor. |