You are here

function uc_googleanalytics_page_attachments_alter in Ubercart 8.4

Implements hook_page_attachments_alter().

File

uc_googleanalytics/uc_googleanalytics.module, line 23
Adds Google Analytics Javascript to the checkout completion page.

Code

function uc_googleanalytics_page_attachments_alter(&$page) {

  // Check to see if we are at the order completion page.
  if (uc_googleanalytics_display()) {

    // If so, then if we can load the order...
    $session = \Drupal::service('session');
    if ($session
      ->has('ucga_order_id') && ($order = Order::load($session
      ->get('ucga_order_id')))) {

      // Build the GA tracking code.
      $script = uc_googleanalytics_ecommerce_js($order);

      // Add the code to the footer.
      drupal_add_js($script, [
        'type' => 'inline',
        'scope' => 'footer',
        'preprocess' => FALSE,
      ]);
    }

    // Clean out the session variable.
    if ($session
      ->has('ucga_order_id')) {
      $session
        ->remove('ucga_order_id');
    }
  }
}