Universal Analytics failed to track transaction in magento

I am using magento CE 1.7. I recently switched to universal analytics from Google Analytics.

After the transfer, in addition to transaction data, other data is tracked in order.

I 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> 

The admin side also stores the universal analytics tracking code.

What am I doing wrong? Why can't I track transaction data? Can anyone help with this?

+6
source share
3 answers

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) ?> <!-- 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; ?> 

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); } 
+17
source

It is important to note that Daniel replied that the cookie domain is not being set correctly.

Change this line:

 ga('create', '".$this->jsQuoteEscape($accountId)."', '".$hostName."'); 

To:

 ga('create', '".$this->jsQuoteEscape($accountId)."', {'cookieDomain':'".Mage::getStoreConfig('web/cookie/cookie_domain', Mage::app()->getStore() )."'}); 

It correctly inserts the cookie domain from your Magento configuration compared to the value of $ hostName, which is not set and results in an empty one. ''

If you use a CDN on a subdomain, it becomes more important to preserve the traffic of this cookie.

0
source

I had the same problem. Then I installed the extension below and now everything works fine. https://magento.mdnsolutions.com/extensions/mdn-google-universal-analytics.html

Hope this helps.

0
source

All Articles