You are here

CommerceGoogleTagManagerBaseAction.php in Commerce Google Tag Manager 7.2

This file contains the base class for CommerceGoogleTagManager actions.

File

includes/CommerceGoogleTagManagerBaseAction.php
View source
<?php

/**
 * @file
 * This file contains the base class for CommerceGoogleTagManager actions.
 */
abstract class CommerceGoogleTagManagerBaseAction extends RulesActionHandlerBase {

  /**
   * Returns the commerce event's name to be sent.
   *
   * @return string
   */
  public abstract function getCommerceEventName();

  /**
   * Get the default Rule's info.
   *
   * @return array
   */
  protected static function getDefaultsInfo() {
    return array(
      'group' => t('Commerce Google-TagManager'),
      'name' => get_called_class(),
    );
  }

  /**
   * Builds and pushes the current commerce data.
   *
   * @param array $commerceData
   */
  protected function pushCommerceData(array $commerceData) {
    $script = 'var dataLayer = dataLayer || []; ';
    $data = array(
      'event' => $this
        ->getCommerceEventName(),
      'ecommerce' => $commerceData,
    );
    $context = array(
      'event' => $this
        ->getCommerceEventName(),
    );

    // Allow other modules to alter this data before sending to DataLayer
    drupal_alter('commerce_google_tag_manager_commmerce_data', $data, $context);

    // Add the data line to the JS array.
    $_SESSION['commerce_google_tag_manager'][] = $script . 'dataLayer.push(' . drupal_json_encode($data) . ');';
  }

}

Classes

Namesort descending Description
CommerceGoogleTagManagerBaseAction @file This file contains the base class for CommerceGoogleTagManager actions.