$siteName = 'your site name';
$account= 'your analytics account (UA-)';
$app = JFactory::getApplication();
$order_id = $app->getUserState('com_hikashop.order_id');
$orderClass = hikashop_get('class.order');
$this->order = $orderClass->loadFullOrder($order_id);
$order_total = round($this->order->order_full_price, 2);
$shipping_price = round($this->order->order_shipping_price, 2);
$revenue = $order_total - $shipping_price;
$order_total_no_vat = round($this->order->order_subtotal_no_vat, 2);
$tax = $order_total - $order_total_no_vat;
$js = '
<!-- Google Analytics ecommerce -->
<script>
(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,"script","//www.google-analytics.com/analytics.js","ga");
ga("create", "' . $account . '", "auto");
ga("send", "pageview");'.$extra_required.'
ga("require", "ecommerce", "ecommerce.js");
ga("ecommerce:addTransaction", {
"id": "' . $order_id .'",
"affiliation": "' . str_replace(array('\\','"'), array('\\\\', '\\\"'), $siteName) . '",
"revenue": "' . $revenue . '",
"shipping": "' . $shipping_price . '",
"tax": "' . round($tax, 2) . '"
});
';
foreach($this->order->products as $product) {
$js .=
'ga("ecommerce:addItem", {
"id": "' . $order_id . '",
"name": "' . str_replace(array('\\','"'), array('\\\\', '\\\"'), strip_tags($product->order_product_name)) . '",
"sku": "' . str_replace(array('\\','"'), array('\\\\', '\\\"'), $product->order_product_code) . '",
"category": "",
"price": "' . ($product->order_product_price + $product->order_product_tax) . '",
"quantity": "' . (int)$product->order_product_quantity . '"
});
';
}
$js .= '
ga("ecommerce:send");
</script>
<!-- End Google Analytics -->
';
echo $js;
Place this code in the end.php in your template html directory.
There is only one problem with this: it doesn't save the category of the products. Even in the original plugin there is only an empty "" for this. Is there a way to have this information saved and used on the checkout process?