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>
It depends on which version of Magento you are using. The new Universal Analytics Code is only available since version 1.9.1.0.
1.9.0.1 on the other hand only had the old code, which made it a pain to add any multiple event tracking.
So, if you're on 1.9.1.0, use the built in Google API. Anything prior to that I would consider adding the universal code yourself.
Best Answer
Got it , it was a conflict with payment method module (klarna 5.2) which override block/ga.php. just some xml configuration solve the problem or upgrade to klarna 5.3. Now it's working!