You are here

function commerce_google_tag_manager_page_build in Commerce Google Tag Manager 7.2

Same name and namespace in other branches
  1. 7 commerce_google_tag_manager.module \commerce_google_tag_manager_page_build()

Implements hook_page_build().

File

./commerce_google_tag_manager.module, line 20
Adds the required Javascript to the checkout completion page to allow e-commerce statistics tracking through Google Tag Manager.

Code

function commerce_google_tag_manager_page_build(&$page) {
  if (!isset($_SESSION['commerce_google_tag_manager']) || !is_array($_SESSION['commerce_google_tag_manager'])) {
    return;
  }
  $data =& $_SESSION['commerce_google_tag_manager'];

  // Allow other modules to alter aggregated event-data just before pushing to
  // the DataLayer.
  drupal_alter('commerce_google_tag_manager_commerce_data_aggregated', $data);
  foreach ($data as $key => $script) {

    // Data escaped via drupal_json_encode().
    $page['page_top']['#attached']['js'][] = array(
      'type' => 'inline',
      'data' => $script,
    );
    unset($data[$key]);
  }
}