You are here

commerce_google_tag_manager.module in Commerce Google Tag Manager 8.2

Module file for the commerce_google_tag_manager module.

File

commerce_google_tag_manager.module
View source
<?php

/**
 * @file
 * Module file for the commerce_google_tag_manager module.
 */
use Drupal\Core\Cache\CacheableMetadata;

/**
 * Pass module configuration to javascript.
 *
 * Implements hook_page_attachments().
 */
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);
}

Functions

Namesort descending Description
commerce_google_tag_manager_page_attachments Pass module configuration to javascript.