防止重复使用Google Analytics(GA)和Google网站优化工具(GWO)的ga.js.

时间:2012-03-29 04:43:28

标签: javascript google-analytics google-website-optimizer

Google Analytics代码与Google网站优化工具(GWO)*相同;两者都来自http://www.google-analytics.com/ga.js。但是,在GA和GWO管理面板上,Google要求我们包含(即复制粘贴)其各自的跟踪代码,这些代码都包含对 ga.js 的相同引用。也许只是为了简化指令?所以,当我们想要同时启用GA跟踪和A / B测试时,我看到多个页面都有对 ga.js 的冗余引用。

我的问题是我们可以做以下事情吗?希望这对那些不熟悉JavaScript但需要配置GA和GWO的人有用。

<head>
<script>
var _gaq = _gaq || [];
_gaq.push(['gwo._setAccount', 'UA-xxxxxxx-x']);
_gaq.push(['gwo._trackPageview', '/yyyyyyyyyy/test']);
</script>
<!-- All other meta tags. -->

<!-- Let this script serves both for the regular GA tracking and GWO A/B test. -->
<script>
(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>
</head>

* GWO脚本通常用于A / B测试。

1 个答案:

答案 0 :(得分:0)

是的,您可以将函数snipet包含一次,但是您缺少配置的GA部分。