我应该将 Google Analytics JS 放在 <head> 中吗?还是在 &lt;body&gt; 的末尾? [英] Should I put the Google Analytics JS in the &lt;head&gt; or at the end of &lt;body&gt;?

查看:29
本文介绍了我应该将 Google Analytics JS 放在 <head> 中吗?还是在 &lt;body&gt; 的末尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google 给了我一段 javascript 并告诉我将它包含在 <head> 中.

Google gives me a piece of javascript and tells me to include it in the <head>.

我可以把它放在的末尾,否则会产生一些副作用吗?

Can I put it at the end of <body> or will some side effects occur if I do that?

推荐答案

将它放在 <head> 部分的末尾有助于确保即使用户没有跟踪您的指标让页面完成加载.

Putting it at the end of the <head> section helps ensure the your metrics are tracked even when a user doesn't let the page finish loading.

在他们添加对处理页面部分加载的支持之前,他们曾经告诉你把它放在页面的底部.

They used to tell you to put it at the bottom of the page, before they added support for handling partial loading of pages.

直接来自 Google:

Directly from Google:

的主要优点之一异步代码段是你可以将它放在 HTML 的顶部文档.这增加了跟踪信标的可能性将在用户离开前发送这一页.习惯上放置 部分中的 JavaScript 代码,我们建议放置代码段在该部分的底部以获得最佳性能.

One of the main advantages of the asynchronous snippet is that you can position it at the top of the HTML document. This increases the likelihood that the tracking beacon will be sent before the user leaves the page. It is customary to place JavaScript code in the <head> section, and we recommend placing the snippet at the bottom of the section for best performance.

查看Google Analytics 帮助:添加跟踪代码直接发送到您的网站

这篇关于我应该将 Google Analytics JS 放在 <head> 中吗?还是在 &lt;body&gt; 的末尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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