Google Analytics 代码:它可以放在文档的前面吗?

发布于 2024-08-11 03:08:50 字数 1355 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暮色兮凉城 2024-08-18 03:08:50

它当然可以位于网站其余代码之前,但 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.

比忠 2024-08-18 03:08:50

使用 jQuery 时,我已将 Google Analytics 代码包含在 ready 事件中,并且效果也很好。

$(document).ready(function () {
  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"));

  try{
    var pageTracker = _gat._getTracker("UA-xxxxxx-x");
  } catch(err) {};

  try{
    pageTracker._addTrans(...);
    pageTracker._trackTrans();
  } catch(err) {};

  try{
    pageTracker._trackPageview();
  } catch(err) {};
});

I have included Google Analytics code inside the ready event when using jQuery and it works fine too.

$(document).ready(function () {
  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"));

  try{
    var pageTracker = _gat._getTracker("UA-xxxxxx-x");
  } catch(err) {};

  try{
    pageTracker._addTrans(...);
    pageTracker._trackTrans();
  } catch(err) {};

  try{
    pageTracker._trackPageview();
  } catch(err) {};
});
过期以后 2024-08-18 03:08:50

仅建议将跟踪器代码放置在结束正文标记之前。您也可以将其放置在头部或其他地方。

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.

掩饰不了的爱 2024-08-18 03:08:50

仅出于网页加载性能的考虑,将代码放置在正文末尾之前是一个很好的做法!

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!

不再让梦枯萎 2024-08-18 03:08:50

该代码可以放置在任何可接受 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文