Google Analytics 代码:它可以放在文档的前面吗?
Google 建议将分析代码放在 标记之前。我正在尝试将电子商务集成到我的网站中,从页脚以外的其他位置调用
pageTracker._addTrans
会更容易。
可以吗
...
<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>
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>
To something more like
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它当然可以位于网站其余代码之前,但 javascript 会阻止页面其余部分的加载。这就是为什么如果您必须加载 javascript 文件并且在其他所有内容之前加载它们并不重要(或在
中),他们经常建议将脚本标记放在底部页面。
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.使用 jQuery 时,我已将 Google Analytics 代码包含在 ready 事件中,并且效果也很好。
I have included Google Analytics code inside the ready event when using jQuery and it works fine too.
仅建议将跟踪器代码放置在结束正文标记之前。您也可以将其放置在头部或其他地方。
Google 推荐这样做,因为在页脚中其他脚本出现问题的机会更小。但是如果你把 try {} catch (e) {} 放在周围,它将被保存在任何地方。只需检查 firebug 是否会触发跟踪 Ajax 请求。
It is only a recomendation to place the tracker code before the closing body tag. You can also place it in the head or somewhere else.
Google recomends this because in the footer the chance it makes problems by other scripts is more less. But if you put try {} catch (e) {} arround it will be save every where. Just check with firebug if the track Ajax request will be fired.
仅出于网页加载性能的考虑,将代码放置在正文末尾之前是一个很好的做法!
It's a good practice to place the code before the end of the body only for a matter of performance loading of the web page!
该代码可以放置在任何可接受 JavaScript 代码的地方。以下是他们的帮助文档的链接,介绍了它应该放在哪里以及可以放在哪里:
http://www.google.com/support/analytics/bin/answer.py?hl=zh_CN&answer=55488&utm_id=ad
希望这有帮助,
谢谢!
This code can be placed anywhere JavaScript code is acceptable. here is a link to their help documentation on where this should go, and where it can go:
http://www.google.com/support/analytics/bin/answer.py?hl=en_US&answer=55488&utm_id=ad
Hope this helps,
Thanks!