在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中就有数据了。
- 在Magento中加入Google Analytics的电商信息分析代码
- Google Analytics加入数据实时分析功能
- magento使用google analytics
- google的分析(analytics)js代码分析以及重写
- google的分析(analytics)js代码分析以及重写
- google的分析(analytics)js代码分析以及重写
- google的分析(analytics)js代码分析以及重写
- 如何在web中安装 Google Analytics 数据分析
- Google Analytics中analytics.js的使用
- Google Analytics中analytics.js的使用
- 在magento的xml文件中加入图片images,或者一些其他的html代码
- Google Analytics(分析)跟踪代码:电子商务
- 在Blog中使用Google Analytics的感受
- 在Blog中使用Google Analytics的感受
- 在Blog中使用Google Analytics的感受
- 在Blog中使用Google Analytics的感受
- 在Blog中使用Google Analytics的感受
- 在Blog中使用Google Analytics的感受
- Hibernate 实体类注解
- 工作三周总结-2012.07.26
- Android NDK, Revision 8b 发布
- interface(接口)与abstract class(抽象类)的区别;实例比较;
- ( ! ) Warning: Invalid argument supplied for foreach() in F:\xampp\htdocs\zendfrm\application\views\
- 在Magento中加入Google Analytics的电商信息分析代码
- ext.grid导出excel
- C语言嵌入式系统编程修炼之屏幕操作
- 最短的IE判定var ie=!-[1,]分析
- TS包大小
- 把NDK集成到Eclipse
- 休闲娱乐_(我看出来了,你可以吗)(测试你的左脑和右脑)
- Thread系列1-浅析 Java Thread.join()
- 公有云中的网络