Thursday, August 9, 2012

Magento : Ecommerce Tracking google analytics

Ecommerce Tracking google analytics magento

Code for sales conversion tracking in magento.

Add below code to the end of the success.phtml file in Magento

<?php
$order = Mage::getModel('sales/order')->load(Mage::getSingleton('checkout/session')->getLastOrderId());
$subtotal = $order->getSubtotal();
$order_id = $order->getId(); //the id of the order
//$order->getIncrementId();//the increment id of the order
$gtotal = $order->getGrandTotal();//grand total of the order
$address = $order->getBillingAddress()->getData();
$city = $address['city'];
$state = $address['region'];
$country = $address['country_id'];
?>
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-17916440-1']);
  _gaq.push(['_trackPageview']);
  _gaq.push(['_addTrans',
    '<?php echo $order_id; ?>',           // order ID - required
    'steigerhoutstunter.nl',  // affiliation or store name
    '<?php echo $gtotal;?>',          // total - required
    '0',           // tax
    '0',              // shipping
    '<?php echo $city;?>',       // city
    '<?php echo $state;?>',     // state or province
    '<?php echo $country;?>'             // country
  ]);

<?php
$items = $order->getAllItems();
$itemcount=count($items);
$name=array();
$unitPrice=array();
$sku=array();
$ids=array();
$qty=array();
foreach ($items as $itemId => $item)
{?>

 _gaq.push(['_addItem',
    '<?php echo $order_id; ?>',                // order ID - required
    '<?php echo $item->getSku(); ?>',          // SKU/code - required
    '<?php echo $item->getName(); ?>',         // product name
    'category name',                           // category or variation
    '<?php echo $item->getPrice(); ?>',        // unit price - required
    '<?php echo $item->getQtyToInvoice(); ?>'  // quantity - required
  ]);
<?php
}
?>
  _gaq.push(['_trackTrans']); //submits transaction to the Analytics servers
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>

4 comments:

  1. The information of this blog is really useful to every magento developers.I am very grateful to read this blog for providing the useful information.
    Magento Web Designing Bangalore

    ReplyDelete
  2. This is a great post. I like this topic.This site has lots of advantage. It helps me in many ways.Thanks for posting this again.
    magento development company in bangalore 

    ReplyDelete