javascript 是否有理由将谷歌分析放在头脑中而不是身体上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3694356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:58:33  来源:igfitidea点击:

Is there a reason to put google analytics in head and not in body?

javascriptjquery-uigoogle-analytics

提问by Yosef

Does exist reason to put google analytics in head and not in the end of body? (I working on big website that its works in this way)

是否有理由将谷歌分析放在头上而不是放在身体的最后?(我在大型网站上工作,它以这种方式工作)

Option 1:

选项1:

<head>
<script src="http://www.google-analytics.com/ga.js"></script>
</head>

Option 2 - in bottom of body :

选项 2 - 在身体底部:

<body>
//html code

<script src="http://www.google-analytics.com/ga.js"></script>
</body>

Edit1: Also the same question with jquery ui

编辑 1:jquery ui也有同样的问题

Edit2: add ga.js in the end of script (fix)

Edit2:在脚本末尾添加 ga.js(修复)

Thanks

谢谢

采纳答案by enricog

I suggest to use the asynchronous google analytics code.

我建议使用异步谷歌分析代码。

Asynchronous Google Analytics

异步谷歌分析

If you use the non-asynchronous code and put it into the head section, it may block the load of your website if the ga code would be slow to load, because it waits until the scripts are loaded. And because google analytics is an external script you may have no influence on the load performance (normally it should not matter, but it can happen that even google has server problems).

如果您使用非异步代码并将其放入 head 部分,如果 ga 代码加载缓慢,它可能会阻止您网站的加载,因为它会等待脚本加载。并且因为 google 分析是一个外部脚本,您可能对负载性能没有影响(通常应该无关紧要,但即使 google 也可能出现服务器问题)。

So, no i don't see a real reason to do it that way.

所以,不,我没有看到这样做的真正理由。

回答by Yahel

Embedding the ga.js code the way you describe (with a hardcoded <script>tag) is indeed blocking, and if you load the script like that, the best practice is considered to be loading it at the just before the </body>tag. But this is not the recommended practice if you're using the new asynchronous code. Google explicitly recommends placing the new asynchronous code in the <head>.

以您描述的方式(使用硬编码<script>标签)嵌入 ga.js 代码确实是阻塞的,如果您像这样加载脚本,最佳实践被认为是在</body>标签之前加载它。但是,如果您使用新的异步代码,这不是推荐的做法。Google 明确建议将新的异步代码放在<head>.

The new asynchoronous code is non-blocking in two ways. First, it queues up the variables for the page in a global _gaq variable. That way, the data is prepared either way.

新的异步代码在两个方面是非阻塞的。首先,它将页面的变量排列在全局 _gaq 变量中。这样,无论哪种方式都可以准备数据。

Then, as described in this SO answer, using javascript directly to write out the script as in the new async code is non-blocking (this direct inject method is the way to achieve asynchronous-ness, even in browsers that don't directly observe the asyncattribute). The rest of the site can continue to load if for some reason Google's servers are down or slow to respond. And that's only if the user doesn't have ga.js cached already, as many do, since ga.js is used on many, many popular websites.

然后,如this SO answer中所述,直接使用javascript写出脚本,就像在新的异步代码中一样是非阻塞的(这种直接注入方法是实现异步性的方法,即使在不直接观察的浏览器中也是如此该async属性)。如果由于某种原因 Google 的服务器关闭或响应缓慢,则站点的其余部分可以继续加载。只有在用户没有缓存 ga.js 的情况下才会如此,因为 ga.js 已在许多流行的网站上使用。

The benefit of all this is that the earlier the ga.js loads and is able to transmit the _gaq object to Google, the more likely you'll be to capture ALL of your potential data, like the data of the users who click very quickly on your page. This is particularly important for 'big' websites that tend to have lots of regular users who follow quick-clicking habits.

所有这一切的好处是 ga.js 越早加载并能够将 _gaq 对象传输给 Google,您就越有可能捕获所有潜在数据,例如点击速度非常快的用户的数据在您的页面上。这对于“大”网站来说尤其重要,这些网站往往有很多遵循快速点击习惯的普通用户。

If you're skeptical, test it out using a page load inspector like the webkit developer tools. I've tested it extensively and found no evidence of significant blocking when using the async code in the </head>as described.

如果您持怀疑态度,请使用页面加载检查器(如 webkit 开发人员工具)进行测试。我已经对其进行了广泛的测试,并且在使用所述异步代码时没有发现明显阻塞的证据</head>

回答by Oded

There is no good reason for it. Google themselves recommend putting the tag at the bottom of the body in order to avoid loading it early on and slowing down page loading.

没有充分的理由。谷歌自己建议将标签放在正文的底部,以避免过早加载并减慢页面加载速度。

It was probably done that way because someone is used to putting <script>tags in the header.

这样做可能是因为有人习惯于<script>在标题中放置标签。

回答by Kris van der Mast

It's recommended to put such scrips as low as possible in the html for performance reasons. Scripts that need to get loaded interrupt other downloads in the browser. I suggest that you take a look at this article: Best Practices for Speeding Up Your Web Site.

出于性能原因,建议将此类脚本尽可能放在 html 中。需要加载的脚本会中断浏览器中的其他下载。我建议你看看这篇文章:加速你的网站的最佳实践