新的谷歌Analytics(分析)的代码看起来像一个下面:新的谷歌Analytics(分析)代码到外部文件
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-0000000-00']);
_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>
如何在全新的谷歌Analytics(分析)异步跟踪代码移动到外部JavaScript文件?
我特别想问“var _gaq = _gaq || []; [...]”部分,因为我知道可以移动其余的例如
的index.html
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-0000000-00']);
_gaq.push(['_trackPageview']);
</script>
<script src="include.js" type="text/javascript"></script>
include.js
function includeGA()
{
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);
}
$(document).ready(function()
{
includeGA();
});
我已经试着放置“VAR _gaq = _gaq || []; [... ]“代码到各个位置,但没有任何工作。
感谢您的编辑。这是我的第一篇文章。 – 2012-10-29 14:29:31
问题是如果外部文件中的这个小JS代码值多一个请求...我认为它不是。 – 2014-07-28 08:27:04
那么您的代码提案是否可以使用有效的解决方案? – 2016-04-16 10:48:10