You are here

function commerce_google_tag_manager_page_attachments in Commerce Google Tag Manager 8.2

Same name and namespace in other branches
  1. 8 commerce_google_tag_manager.module \commerce_google_tag_manager_page_attachments()

Pass module configuration to javascript.

Implements hook_page_attachments().

File

./commerce_google_tag_manager.module, line 15
Module file for the commerce_google_tag_manager module.

Code

function commerce_google_tag_manager_page_attachments(array &$page) {
  $page['#attached']['library'][] = 'commerce_google_tag_manager/commerce_google_tag_manager';
  $page['#attached']['drupalSettings']['commerceGoogleTagManager'] = [
    'eventsUrl' => Drupal::service('url_generator')
      ->generateFromRoute('commerce_google_tag_manager.events'),
    'dataLayerVariable' => Drupal::config('google_tag.settings')
      ->get('data_layer') ?: 'dataLayer',
  ];

  // Add cache metadata to the render array.
  $cache = new CacheableMetadata();
  $cache
    ->addCacheableDependency(Drupal::config('google_tag.settings'));
  $cache
    ->applyTo($page);
}