You are here

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
ga_tokens.analytics_subscriber in modules/ga_tokens/ga_tokens.services.yml
Drupal\ga_tokens\EventSubscriber\AnalyticsSubscriber

File

modules/ga_tokens/src/EventSubscriber/AnalyticsSubscriber.php, line 16

Namespace

Drupal\ga_tokens\EventSubscriber
View 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

Namesort descending Modifiers Type Description Overrides
AnalyticsSubscriber::$configFactory protected property The config factory service.
AnalyticsSubscriber::$token protected property Token service.
AnalyticsSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
AnalyticsSubscriber::onCollectGlobalProperties public function Add global dimensions and metrics.
AnalyticsSubscriber::__construct public function DefaultCommandSubscriber constructor.