You are here

function _uc_googleanalytics_ecommerce_legacy_script in Ubercart 5

1 call to _uc_googleanalytics_ecommerce_legacy_script()
uc_googleanalytics_footer in uc_googleanalytics/uc_googleanalytics.module
Implementation of hook_footer().

File

uc_googleanalytics/uc_googleanalytics.module, line 85
Adds the required HTML and Javascript to the checkout complete page to allow e-commerce statistics tracking through Google Analytics.

Code

function _uc_googleanalytics_ecommerce_legacy_script($order) {
  $script = '';
  $UTM_T = '';
  $UTM_I = '';
  $tax = 0;
  $shipping_cost = 0;

  // Finding name of country; if not found, use the country code.
  if ($country_data = uc_get_country_data(array(
    'country_id' => $order->billing_country,
  ))) {
    $country = $country_data[0]['country_name'];
  }
  else {
    $country = $order->billing_country;
  }
  foreach ($order->products as $product) {
    $category = '';

    // Try to find a category (term) for the product. Since products most often
    // only have one category, the first one returned (based on tid) is chosen.
    if (module_exists('taxonomy')) {
      $terms = taxonomy_node_get_terms($product->nid);
      if (count($terms)) {
        $term = array_shift($terms);
        $category = $term->name;
      }
    }
    if (empty($category)) {
      $category = t('No category');
    }

    // using the model field as SKU
    $UTM_I .= 'UTM:I|' . drupal_to_js($product->order_id) . '|' . drupal_to_js($product->model) . '|' . drupal_to_js($product->title) . '|' . drupal_to_js($category) . '|' . drupal_to_js($product->price) . '|' . drupal_to_js($product->qty) . " \n";
  }
  foreach ($order->line_items as $line_item) {
    if ($line_item['type'] == 'tax') {
      $tax += $line_item['amount'];
    }
    if ($line_item['type'] == 'shipping') {
      $shipping_cost += $line_item['amount'];
    }
  }
  $UTM_T = 'UTM:T|' . drupal_to_js($order->order_id) . '|' . drupal_to_js(variable_get('uc_store_name', 'Ubercart')) . '|' . drupal_to_js($order->order_total) . '|' . drupal_to_js($tax) . '|' . drupal_to_js($shipping_cost) . '|' . drupal_to_js($order->billing_city) . '|' . drupal_to_js($order->billing_postal_code) . '|' . drupal_to_js($country) . " \n";
  $script .= "<form style=\"display:none;\" name=\"utmform\">\n";
  $script .= "<textarea id=\"utmtrans\">\n";
  $script .= $UTM_T;
  $script .= $UTM_I;
  $script .= "</textarea>\n";
  $script .= "</form>\n";
  return $script;
}