Magento – magento orders are not tracked by google after upgrading universal analytics

google analyticsmagento-1.7

After upgrading Googles Universal Analytics, magento orders are not tracked / converted. We have checked the following options

  1. added the track informations in
    app/design/frontend/base/default/page/html/head.phtml
  2. Sytem/Configuration-> Sales->Google Api -> Enabled and Account
    number is given and
  3. Ecommerce Tracking is on status in Google Analytics. But the orders
    not tracked by google.

we are using magento 1.7. Shall we need to add any other things?

Best Answer

I have added the code in success.phtml file, it is workging fine.

$order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$total = $order->getGrandTotal();
$shipping = $order->getShippingAmount();
$tax = $order->getTaxAmount();
$store = Mage::app()->getStore();
$name = $store->getName();

$items = $order->getAllItems();
$item_details = array() ;
foreach($items as $item) {
$det = array() ;
$det['sku'] = $item->getSku() ;
$det['name'] = $item->getName() ;
$det['category'] = get_category_name($item->getProductId()) ;
$det['price'] = $item->getPrice() ;
$det['quantity'] = number_format($item->getQtyOrdered(), 0) ; 
array_push($item_details, $det) ; 

}

function get_category_name($productId) { 
$product = Mage::getModel('catalog/product')->load($productId);
$category_name = "" ;
$cats = $product->getCategoryIds();

$cnt = 0 ;
foreach ($cats as $category_id) {
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
$cnt++ ;
if($cnt == count($cats)) 
$category_name.=$_cat->getName() ;
else 
$category_name.=$_cat->getName()."," ; 
} 
return $category_name ; 
}

function getItemJs(&$transId, &$item) {
return <<<HTML
ga('ecommerce:addItem', {
'id': '$transId',
'name': '{$item['name']}',
'sku': '{$item['sku']}',
'category': '{$item['category']}',
'price': '{$item['price']}',
'quantity': '{$item['quantity']}'
});
HTML;
}

?>
<script type="text/javascript">

ga('require', 'ecommerce', 'ecommerce.js'); // Load GA ecommerce plug-in.
ga('ecommerce:addTransaction', {
'id': '<?php echo $this->getOrderId(); ?>', // Transaction ID. Required
'affiliation': '<?php echo $name ?>', // Affiliation or store name
'revenue': '<?php echo $total; ?>', // Grand Total
'shipping': '<?php echo $shipping; ?>', // Shipping
'tax': '<?php echo $tax; ?>' // Tax
});
// to get items
<?php
foreach ($item_details as &$item) {
echo getItemJs($this->getOrderId(), $item);
}
?>
ga('ecommerce:send');
</script>
Related Topic