You are here

function ga_page_attachments in Googalytics - Google Analytics 8

Implements hook_page_attachments().

File

./ga.module, line 14
Contains ga.module.

Code

function ga_page_attachments(array &$attachments) {

  // Don't execute until gacsp module is uninstalled.
  if (\Drupal::moduleHandler()
    ->moduleExists('gacsp')) {
    return;
  }

  /** @var \Drupal\ga\Event\CollectEvent $event */
  $event = \Drupal::service('event_dispatcher')
    ->dispatch(AnalyticsEvents::COLLECT, new CollectEvent());
  $commands = $event
    ->getDrupalSettingCommands();
  if (!empty($commands)) {
    $attachments['#attached']['library'][] = 'ga/analytics';
    $attachments['#attached']['drupalSettings']['ga']['commands'] = $commands;
  }
}