Keith GreerAdd Google Analytics ecommerce:addTransaction to your Magento Store

A quick and way way to record transactions to Google Universal Analytics.

First add this piece of PHP to your success page at the end of the checkout process. Update the template in magento/app/design/frontend/your/theme/template/checkout/success.phtml. This will retreve the order details…

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

Then add the following JavaScript, it first loads the ecommerce.js plugin for Universal Analytics – you don’t need to include this if it has already been called elsewhere. ten the following lines add the details of the transaction and finally it sends the transaction details back to Google Analytics.

<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
});
ga('ecommerce:send');

</script>

Coming soon: How to record individual cart line items as well as the transaction details so product popularity can be monitored.