Hi, I have the same problem, today I wrote a solution, but first I will remove the user script in head.phtml ...
First add ga.phtml
You need to create a new file in your template folder or change it by default:
MAGENTOROOT/app/design/frontend/YOURTHEME/template/googleanalytics/ga.phtml
And add this to a file that will override base / default Magento ga.phtml
<?php if (!Mage::helper('core/cookie')->isUserNotAllowSaveCookie()): ?> <?php $accountId = Mage::getStoreConfig(Mage_GoogleAnalytics_Helper_Data::XML_PATH_ACCOUNT) ?> <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; ?>
Above the default GoogleAnalytics block
This is not the best way to do this, but for simplicity I will use it if you want to get a cleaner solution needed to create a module and add a rewrite code there.
Ok first copy the contents of this file:
MAGENTOROOT/app/code/core/Mage/GoogleAnalytics/Block/Ga.php
and create a new file in code / local that will override the code / kernel:
MAGENTOROOT/app/code/local/Mage/GoogleAnalytics/Block/Ga.php
In the new file that you created, modify these 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); }