I'm using magento C.E 1.7. Recently I migrated to universal analytics from google analytics.
After migration, except transaction data, other details are tracked fine.
I have added the following script in head.phtml for universal analytics.
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXX-2', 'mysite.com');
ga('send', 'pageview');
ga('require', 'ecommerce', 'ecommerce.js');
ga('ecommerce:send');
</script>
In admin side too, I have saved the universal analytics tracking code.
What am I doing wrong? Why can't I track transaction data? Can anyone help on this?
Hi I have the same problem today I have written a solution but first remove the custom script in your head.phtml...
You need to create new file in your template folder or edit the default one:
MAGENTOROOT/app/design/frontend/YOURTHEME/template/googleanalytics/ga.phtml
And add this in the file which will override the base/default Magento ga.phtml
<?php if (!Mage::helper('core/cookie')->isUserNotAllowSaveCookie()): ?>
<?php $accountId = Mage::getStoreConfig(Mage_GoogleAnalytics_Helper_Data::XML_PATH_ACCOUNT) ?>
<!-- BEGIN GOOGLE ANALYTICS CODEs -->
<script type="text/javascript">
//<![CDATA[
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
<?php echo $this->_getPageTrackingCode($accountId) ?>
<?php echo $this->_getOrdersTrackingCode() ?>
//]]>
</script>
<?php endif; ?>
This is not the best way to do it but for simplicity I will use this if you want more clean solution you need to create module and add the rewrite code there.
Ok first copy the content of this file:
MAGENTOROOT/app/code/core/Mage/GoogleAnalytics/Block/Ga.php
and create new file in code/local which will override the code/core one:
MAGENTOROOT/app/code/local/Mage/GoogleAnalytics/Block/Ga.php
In the new file that you have created modify this two functions to match this code:
_getPageTrackingCode($accountId)
protected function _getPageTrackingCode($accountId)
{
$pageName = trim($this->getPageName());
$optPageURL = '';
if ($pageName && preg_match('/^\/.*/i', $pageName)) {
$optPageURL = ", '{$this->jsQuoteEscape($pageName)}'";
}
// if you can think of better way to get the host name
// let me know in the comments.
$hostName = $_SERVER['SERVER_NAME'];
return "
ga('create', '".$this->jsQuoteEscape($accountId)."', 'auto');
ga('send', 'pageview' ".$optPageURL.");
";
}
_getOrdersTrackingCode()
protected function _getOrdersTrackingCode()
{
$orderIds = $this->getOrderIds();
if (empty($orderIds) || !is_array($orderIds)) {
return;
}
$collection = Mage::getResourceModel('sales/order_collection')
->addFieldToFilter('entity_id', array('in' => $orderIds))
;
$result = array("
// Transaction code...
ga('require', 'ecommerce', 'ecommerce.js');
");
foreach ($collection as $order) {
if ($order->getIsVirtual()) {
$address = $order->getBillingAddress();
} else {
$address = $order->getShippingAddress();
}
$result[] = "
ga('ecommerce:addTransaction', {
id: '".$order->getIncrementId()."', // Transaction ID
affiliation: '".$this->jsQuoteEscape(Mage::app()->getStore()->getFrontendName())."', // Affiliation or store name
revenue: '".$order->getBaseGrandTotal()."', // Grand Total
shipping: '".$order->getBaseShippingAmount()."', // Shipping cost
tax: '".$order->getBaseTaxAmount()."', // Tax
});
";
foreach ($order->getAllVisibleItems() as $item) {
$result[] = "
ga('ecommerce:addItem', {
id: '".$order->getIncrementId()."', // Transaction ID.
sku: '".$this->jsQuoteEscape($item->getSku())."', // SKU/code.
name: '".$this->jsQuoteEscape($item->getName())."', // Product name.
category: '', // Category or variation. there is no 'category' defined for the order item
price: '".$item->getBasePrice()."', // Unit price.
quantity: '".$item->getQtyOrdered()."' // Quantity.
});
";
}
$result[] = "ga('ecommerce:send');";
}
return implode("\n", $result);
}