Google Analytics的两个单独的脚本标记? [英] Two separate script tags for Google Analytics?

查看:97
本文介绍了Google Analytics的两个单独的脚本标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道为什么Google Analytics需要两个独立的脚本标记吗?



具体而言,他们的指示建议用户将以下代码段嵌入到网页中以供跟踪:

 <! -  Google Analytics  - > 
< 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>
尝试{
var pageTracker = _gat._getTracker(UA-8720817-1);
pageTracker._trackPageview();
} catch(err){}< / script>

为什么用户不能使用这样的脚本块:

 <! -  Google Analytics  - > 
< 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));
尝试{
var pageTracker = _gat._getTracker(UA-8720817-1);
pageTracker._trackPageview();
} catch(err){}< / script>


解决方案

< script> 标签按顺序执行。如果前一个执行没有执行,则< script> 块不能执行。

第一个< script> 标签负责创建Google < script> 标签,它将加载外部js。在第一个< script> 完成执行后,DOM如下所示:

 <脚本>< /脚本> <! - 第一个脚本标记 - > 
< script>< / script> <! - Google Injected Script - >
< script>< / script> <! - 第二个脚本标签 - >

这保证了第二个< script> 标记不会执行,直到 .js 完成加载。如果将第一个和第二个< script> 结合起来,则会导致 _gat 变量未定义在第一个脚本完成执行之前,Google注入脚本不会开始加载)。


Does anyone know why Google Analytics requires two separate script tags?

Specifically, their instructions advise users to embed the following snippet of code into a web page for tracking purposes:

<!-- Google Analytics -->
<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-8720817-1");
pageTracker._trackPageview();
} catch(err) {}</script>

Why couldn't users use only one script block like this:

<!-- Google Analytics -->
<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"));
try {
var pageTracker = _gat._getTracker("UA-8720817-1");
pageTracker._trackPageview();
} catch(err) {}</script>

解决方案

<script> tags are executed in sequence. A <script> block cannot execute if the previous one isn't done executing.

The first <script> tag is in charge of creating the Google <script> tag which will load the external js. After the first <script> is finished executing, the DOM looks like the following:

<script></script> <!-- First Script Tag -->
<script></script> <!-- Google Injected Script -->
<script></script> <!-- Second Script Tag -->

This guarantees that the second <script> tag will not execute until the .js is done loading. If the first and second <script> would be combined, this would cause the _gat variable to be undefined (since the Google injected script will not start loading until the first script is done executing).

这篇关于Google Analytics的两个单独的脚本标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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