在Magento中加入Google Analytics的电商信息分析代码

来源:互联网 发布:exm网络意思 编辑:程序博客网 时间:2024/06/10 07:13


参考:https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce?hl=zh-CN

Magento在与Google数据集成方面,下了很多功夫。如可以生成Google Analytics代码,可以在Receipt页生成Google Analytics代码来在GA中显示电商信息(当然,前提是需要把GA的配置文件设置为电商模式)。但根据我对Magento原生的Javascript GA跟踪代码测试,仅能跟踪出简单的PV量及Goal,跟踪一些ECommerce数据,我试了半天都不成。被逼无奈,将Magento1.4.2自带的GA生成码修改成Google推荐的异步代码模板。

原有代码

<script type="text/javascript">//<![CDATA[    (function() {        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);    })();    var _gaq = _gaq || [];_gaq.push(function() {    // the global variable is created intentionally    pageTracker = _gat._getTracker('UA-xxxxxxxx-1');    pageTracker._trackPageview();});//]]></script>

Google推荐

<script type="text/javascript">  var _gaq = _gaq || [];  _gaq.push(['_setAccount', 'UA-xxxxxxxx-1']);  _gaq.push(['_trackPageview']);  (function() {    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);  })();</script>

现将\app\code\core\Mage\GoogleAnalytics\Block内的GA代码修改一下即可满足要求。

1.修改146行的_toHtml方法:

 protected function _toHtml()    {        if (!Mage::helper('googleanalytics')->isGoogleAnalyticsAvailable()) {            return '';        }        $accountId = Mage::getStoreConfig(Mage_GoogleAnalytics_Helper_Data::XML_PATH_ACCOUNT);        return '<!-- BEGIN GOOGLE ANALYTICS CODE --><script type="text/javascript">//<![CDATA[    var _gaq = _gaq || [];' . $this->_getPageTrackingCode($accountId) . '' . $this->_getOrdersTrackingCode() . '    (function() {        var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;        ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';        var s=document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga,s);    })();//]]></script><!-- END GOOGLE ANALYTICS CODE -->';    }
 2.修改86行的_getPageTrackingCode方法
protected function _getPageTrackingCode($accountId)    {        $optPageURL = trim($this->getPageName());        if ($optPageURL && preg_match('/^\/.*/i', $optPageURL)) {            $optPageURL = "'{$this->jsQuoteEscape($optPageURL)}'";        }        // the code compatible with google checkout shortcut (it requires a global pageTracker variable)        return "     _gaq.push(['_setAccount','{$this->jsQuoteEscape($accountId)}']);     _gaq.push(['_trackPageview']);";    }

修改完毕后,在Receipt页的跟踪代码将会变成这样

<script type="text/javascript">//<![CDATA[    var _gaq = _gaq || [];     _gaq.push(['_setAccount','UA-xxxxxxxx-1']);     _gaq.push(['_trackPageview']);_gaq.push(['_addTrans', '100000051', 'xxxxxx.com', '35.0400', '2.1000', '8.9900', 'los angles', 'California', 'US']);_gaq.push(['_addItem', '100000051', '350CP-CA-EL', 'California Labor Law Posters', 'Posters', '23.9500', '1.0000']);_gaq.push(['_trackTrans']);    (function() {        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';        var s=document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga,s);    })();//]]></script>

等半天-1天的时间,在GA中的转化->电子商务->Overview中就有数据了。