什么是最快的异步谷歌分析片段?

时间:2021-08-20 15:14:43

i would to use fastest asynchronous google analytics snippet, but i'm not a good programmer and so i don't know what is the best:

我会使用最快的异步谷歌分析片段,但我不是一个优秀的程序员,所以我不知道什么是最好的:

A)

<!-- Google Analytics -->
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
<script async src='//www.google-analytics.com/analytics.js'></script>
<!-- End Google Analytics -->

https://developers.google.com/analytics/devguides/collection/analyticsjs/tracking-snippet-reference#async-snippet-minified

B)

<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.src = ('https:' == document.location.protocol ? 'https://ssl' :   'http://www') + '.google-analytics.com/ga.js';
ga.setAttribute('async', 'true');
document.documentElement.firstChild.appendChild(ga);
})();
</script>

About these codes i would like to know if is a problem to put the code inside not <script> but inside <script type="text/javascript"> and if is a problem to put not before </head> (like suggested for "normal" snippet)

关于这些代码,我想知道将代码置于

I hope you can help me and sorry for my english :) Thanks a lot!

我希望你能帮助我,对不起我的英语:)非常感谢!

2 个解决方案

#1


0  

The would guess the first one. It uses async and doesn't depend on DOM modification while loading the page.

猜猜第一个。它使用异步,并且在加载页面时不依赖于DOM修改。

#2


0  

For HTML5, it does not matter if you use <script> or <script type="text/javascript">.

对于HTML5,使用

As for where to put in your code, normally javascript should be at the bottom of the page after all of your css/html has loaded. Javascript is loaded synchronously, so each file load will stop the rest of the page from loading until that specific file is fully loaded. The async tag is not fully supported by all browsers, so I would not rely on that. Instead, you should use an asynchronous loader (like requirejs or LAB.js).

至于你的代码放在哪里,通常javascript应该在你的所有css / html加载后位于页面的底部。 Javascript是同步加载的,因此每个文件加载都会阻止页面的其余部分加载,直到特定文件完全加载为止。所有浏览器都不完全支持async标记,因此我不会依赖它。相反,您应该使用异步加载器(如requirejs或LAB.js)。

As for where you should put the google analytics script, in my experience it doesn't matter too much. It sounds like you're prematurely optimizing -- https://*.com/a/385529/5780021. Per google's instructions, I believe it's supposed to go in the header in order to improve their statistics around page load speeds.

至于你应该把谷歌分析脚本放在哪里,根据我的经验,它并不重要。听起来你过早地进行了优化 - https://*.com/a/385529/5780021。根据谷歌的说明,我相信它应该放在标题中,以改善他们围绕页面加载速度的统计数据。

#1


0  

The would guess the first one. It uses async and doesn't depend on DOM modification while loading the page.

猜猜第一个。它使用异步,并且在加载页面时不依赖于DOM修改。

#2


0  

For HTML5, it does not matter if you use <script> or <script type="text/javascript">.

对于HTML5,使用

As for where to put in your code, normally javascript should be at the bottom of the page after all of your css/html has loaded. Javascript is loaded synchronously, so each file load will stop the rest of the page from loading until that specific file is fully loaded. The async tag is not fully supported by all browsers, so I would not rely on that. Instead, you should use an asynchronous loader (like requirejs or LAB.js).

至于你的代码放在哪里,通常javascript应该在你的所有css / html加载后位于页面的底部。 Javascript是同步加载的,因此每个文件加载都会阻止页面的其余部分加载,直到特定文件完全加载为止。所有浏览器都不完全支持async标记,因此我不会依赖它。相反,您应该使用异步加载器(如requirejs或LAB.js)。

As for where you should put the google analytics script, in my experience it doesn't matter too much. It sounds like you're prematurely optimizing -- https://*.com/a/385529/5780021. Per google's instructions, I believe it's supposed to go in the header in order to improve their statistics around page load speeds.

至于你应该把谷歌分析脚本放在哪里,根据我的经验,它并不重要。听起来你过早地进行了优化 - https://*.com/a/385529/5780021。根据谷歌的说明,我相信它应该放在标题中,以改善他们围绕页面加载速度的统计数据。