本文介绍了为什么Google建议在< head>之后的*脚本之后放置Google Analytics异步代码*?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Google建议在HTML中的分析异步跟踪代码之前放入js脚本?



以下是引用:


解决方案

异步分析代码片段的工作是加载更密集的脚本,用户的浏览器可以找到各种信息来识别它们,因此可以向分析服务器报告。但是,由于所有这些分析数据对于页面的可用性都不是至关重要的,因此Google希望在浏览器方便的时候运行它。

从理论上讲,他们可以建议程序员将异步片段添加到页面的最底部,作为正文的最后一个元素。但是,为了让程序员捕获UI事件以发送到分析,他们希望尽早使用 _gaq 变量。例如,您可能有一个按钮:< button onclick =_ gaq.push(...)>音轨< /按钮> 。通过尽早使用 _gaq ,异步片段中的一小部分代码将排队这些消息,而较重的 ga.js 将在稍后将它们发送到服务器。



现在,一些: ga.js 通过添加一个新的<脚本> 元素添加到具有 async 属性集的文档头中。 IE和WebKit将异步加载从脚本插入的< script> 标签。 Firefox和Opera将遵循异步属性,并且异步加载脚本。无论哪种方式,在浏览器方便的时候, ga.js 是异步加载的。



最后,一旦 ga.js 被执行,不会因为异步加载而阻塞页面呈现,它可以完成收集所有用户数据和 _gaq 队列并将它们发送到服务器。



摘要:此方法使用一个小型的内联脚本,您可以在完整的 ga.js 脚本准备就绪之前访问 _gaq 等关键变量。这个小脚本还会动态地向文档添加一个< script src =ga.js> 标签,以便大多数浏览器可以异步下载并执行它,而不会阻止页面的呈现或评估关键脚本。


Why does google recommend putting js scripts in before the analytics asynchronous tracking code in your html?http://code.google.com/apis/analytics/docs/tracking/asyncMigrationExamples.html

Here's the quote:

解决方案

The asynchronous analytics snippet's job is to load a more intensive script that inspects the user's browser for all sorts of information to identify them, so it can report back to the analytics server. However, since all this analytics data is not crucial to the usability of the page, Google wishes to run it at the browser's convenience.

In theory, they could advise the programmer to add the asynchronous snippet to the very bottom of the page, as the last element of the body. However, in order to allow the programmer to capture UI events to send to analytics, they want to make the the _gaq variable for use early on. For example, you might have a button: <button onclick="_gaq.push(...)">Track</button>. By making _gaq available early on, the small bit of code in the asynchronous snippet will queue up these messages and the heavier ga.js will send them off to the server later.

Now, some implementation details: ga.js is loaded by adding a new <script> element to the document head with the async attribute set. IE and WebKit will asynchronously load <script> tags inserted from scripts. Firefox and Opera will honor the async attribute and also load the script asynchronously. Either way, ga.js is asynchronously loaded, at the browser's convenience.

Finally, once ga.js is executed, without blocking the page rendering due to the asynchronous loading, it can do the heavy work of collecting all of the user's data and any messages in the _gaq queue and send them to the server.

Summary: This approach uses a small inline script that initializes some key variables like _gaq that your page can access before the full ga.js script is ready. This small script also dynamically adds a <script src="ga.js"> tag to the document in such a way that most browsers will download and execute it asynchronously, without blocking the rendering of the page or the evaluation of critical scripts.

这篇关于为什么Google建议在&lt; head&gt;之后的*脚本之后放置Google Analytics异步代码*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 20:20