Google Analytics(分析)代码:它可以在文档之前执行吗? [英] Google Analytics code: can it go before in the document?

查看:151
本文介绍了Google Analytics(分析)代码:它可以在文档之前执行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google建议将分析代码放在< body> 标签之前。我正在尝试将电子商务整合到我的网站中,并且从页脚以外的其他位置调用 pageTracker._addTrans 会更容易。

Google recommends to put the analytics code just before the <body> tag. I'm trying to integrate ecommerce in my site and it'd be easier to call pageTracker._addTrans from other places than the footer.

如果我改变了,可以吗?

Will it be ok if I change

...
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try{
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>

更类似于

<body>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try{
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
} catch(err) {}</script>
...
<p>Thanks for your purchase!</p>
<script type="text/javascript">
try{
pageTracker._addTrans(...);
pageTracker._trackTrans();
} catch(err) {}</script>
...
<script type="text/javascript">
try{
pageTracker._trackPageview();
} catch(err) {}</script>
</body>


推荐答案

它当然可以在网站的其他代码,但JavaScript会阻止页面其余部分的加载。这就是为什么他们经常推荐如果你必须加载JavaScript文件,并且将它们加载到其他所有内容之前(或者在< head> )中并不重要,脚本标签在页面的底部。

It can certainly go before the rest of the site's code, but javascript will block the loading of the rest of the page. Which is why they often recommend if you have to load javascript files and it's not important to load them ahead of everything else (or in the <head>), to put the script tags at the bottom of the page.

这篇关于Google Analytics(分析)代码:它可以在文档之前执行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆