我想找到一种方法来从document.ready()函数添加谷歌分析代码。以下代码不起作用。来自jQuery的document.ready()的谷歌分析不起作用
$(document).ready(function() {
var _gaq = window._gaq || [];
_gaq.push(['e._setAccount', 'UA-XXXZZ-1']);
_gaq.push(['e._setDomainName', 'company.com']);
_gaq.push(['e._trackPageview']);
_gaq.push(['a._setAccount', 'UA-XXXYY-1']);
_gaq.push(['a._setDomainName', 'company.com']);
_gaq.push(['a._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);
})();
});
解决方案是什么?我可以把创建链接到ga.js文件的代码片段放在head标签中,但它能解决问题吗?
由于
为什么要将它添加到document.ready函数中?只需将其粘贴到页面上的脚本块中即可完成工作! –
谷歌分析具有特定的安装说明。如果你遵循它们,它的作品。没有必要将代码放在'$ document.ready()' – 2013-07-19 08:35:55
我想用Google analytics进行A-B内容测试,并且A-B测试脚本必须在GA脚本之前触发。但是,由于CMS限制,所有页面特定的脚本都放置在站点范围的脚本(即GA代码)之后。所以我需要找到一种方法来在加载头标签中的所有其他脚本之后运行GA代码。 – lekso