function uc_googleanalytics_ecommerce_js in Ubercart 8.4
Same name and namespace in other branches
- 6.2 uc_googleanalytics/uc_googleanalytics.module \uc_googleanalytics_ecommerce_js()
- 7.3 uc_googleanalytics/uc_googleanalytics.module \uc_googleanalytics_ecommerce_js()
Builds the e-commerce JS passed to Google Analytics for order tracking.
Parameters
\Drupal\uc_order\OrderInterface $order: The fully loaded order object to convert into GA JS.
Return value
string The JS that should be added to the page footer.
1 call to uc_googleanalytics_ecommerce_js()
- uc_googleanalytics_page_attachments_alter in uc_googleanalytics/
uc_googleanalytics.module - Implements hook_page_attachments_alter().
File
- uc_googleanalytics/
uc_googleanalytics.module, line 89 - Adds Google Analytics Javascript to the checkout completion page.
Code
function uc_googleanalytics_ecommerce_js(OrderInterface $order) {
$script = '';
// Calculate order tax and shipping totals.
$order->tax_total = 0;
$order->shipping_total = 0;
foreach ($order->line_items as $line_item) {
if ($line_item['type'] == 'tax') {
$order->tax_total += $line_item['amount'];
}
elseif ($line_item['type'] == 'shipping') {
$order->shipping_total += $line_item['amount'];
}
}
// Build the transaction arguments.
$country = \Drupal::service('country_manager')
->getCountry($order->billing_country);
$trans = [
'order_id' => $order
->id(),
'store' => uc_store_name(),
'total' => $order
->getTotal(),
'tax' => $order->tax_total,
'shipping' => $order->shipping_total,
'city' => $order->billing_city,
'state' => $country->zones[$order->billing_zone_name],
'country' => $country->name,
];
// Allow modules to alter the transaction arguments.
\Drupal::moduleHandler()
->alter('ucga_trans', $trans, $order);
// Put the arguments into an array that is safe to implode directly.
$args = [
'"' . $trans['order_id'] . '"',
Json::encode($trans['store']),
'"' . $trans['total'] . '"',
'"' . $trans['tax'] . '"',
'"' . $trans['shipping'] . '"',
Json::encode($trans['city']),
Json::encode($trans['state']),
Json::encode($trans['country']),
];
// Add the transaction line to the JS.
$script .= '_gaq.push(["_addTrans", ' . implode(', ', $args) . ']);';
// Loop through the products on the order.
foreach ($order->products as $product) {
$product->category = '';
// Try to find a category (term) for the product. Since products most often
// only have one category, the first one returned (in the
// $node->taxonomy_catalog) is chosen.
if (\Drupal::moduleHandler()
->moduleExists('taxonomy')) {
$node = Node::load($product->nid);
if (isset($node->taxonomy_catalog[LANGUAGE_NOT_SPECIFIED][0]['tid'])) {
$term = taxonomy_term_load($node->taxonomy_catalog[LANGUAGE_NOT_SPECIFIED][0]['tid']);
$product->category = $term->name;
}
}
if (empty($product->category)) {
$product->category = t('No category');
}
// Build the item arguments.
$item = [
'order_id' => $order
->id(),
'sku' => $product->model,
'name' => $product->title,
'category' => $product->category,
'price' => $product->price,
'qty' => $product->qty,
];
// Allow modules to alter the item arguments.
\Drupal::moduleHandler()
->alter('ucga_item', $item, $product, $trans, $order);
// Put the arguments into an array that is safe to implode directly.
$args = [
'"' . $item['order_id'] . '"',
Json::encode($item['sku']),
Json::encode($item['name']),
Json::encode((string) $item['category']),
'"' . $item['price'] . '"',
'"' . $item['qty'] . '"',
];
// Add the item line to the JS.
$script .= '_gaq.push(["_addItem", ' . implode(', ', $args) . ']);';
}
// Add the function to submit the transaction to GA.
$script .= '_gaq.push(["_trackTrans"]);';
return $script;
}