Google Analytics records all incoming sales, Magento removes all cancelled sales.
The discrepancy between Magento reported sales and Google Analytics e-commerce sales grows over time.
Google Analytics is only an indicator of activity, not a record of your successful sales. In the sales department, you're going to find that Google Analytics is an Ego Massager and Soul Salve.
While Google Analytics has a lot of useful features for everything else, it is not a reliable accounting system for tracking the website financials.
It also doesn't track reduction in revenue via issued credit memos or any other transactions done in the Admin backend..
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>
Best Answer
I have checked and found that
module-google-analytics
is responsible for sending order analytics to Google.It has
events.xml
atmagento2Root/vendor/magento/module-google-analytics/etc/frontend
with following content.So, it is calling observer on order success but only for front-end.
If you move this file form
frontend
to global, it will work for admin as well!So new location for
events.xml
will be:Note:
If above observer not work, you can also use
checkout_submit_all_after
, ORsales_order_place_after
.I have not tested above code due to unavailability of key.
If worked, do proper override in custom module.