You are here

public function DefaultCommandSubscriber::onCollectDefaultCommands in Googalytics - Google Analytics 8

Add default events according to configuration.

Parameters

\Drupal\ga\Event\CollectEvent $event: The AnalyticsEvents::COLLECT event.

File

src/EventSubscriber/DefaultCommandSubscriber.php, line 105

Class

DefaultCommandSubscriber
Class DefaultCommandSubscriber.

Namespace

Drupal\ga\EventSubscriber

Code

public function onCollectDefaultCommands(CollectEvent $event) {
  $config = $this->configFactory
    ->get('ga.settings');
  if ($config
    ->get('add_default_commands')) {

    // Check user role restrictions.
    if (!$this
      ->checkRole($config
      ->get('user_roles.mode'), $config
      ->get('user_roles.roles'), $this->currentUser
      ->getRoles())) {
      return;
    }

    // Initialize tracker or set tracker options.
    if ($tracking_id = $config
      ->get('tracking_id')) {

      // Add options which can be provided when initializing the tracker.
      $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 a trackingId isn't provided for initializing a tracker, these
      // options can be provided via set commands instead.
      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());
    }

    // Enable Plugins.
    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')) {

      // Note: 'allowLinker' must be set when creating the tracker for this
      // plugin to have an effect.
      $event
        ->addCommand(new RequirePlugin('linker'));
      if ($domains = $config
        ->get('plugins.linker.domains')) {
        $event
          ->addCommand(new AutoLink($domains));
      }
    }
  }
}