The task of asynchronous analytical analysis is to load a more intensive script that checks the user's browser to get all kinds of information to identify them, so it can report to the analytics server. However, since all analytics data are not critical for the usability of the page, Google wants to launch it with the convenience of a browser.
In theory, they could advise the programmer to add an asynchronous fragment to the bottom of the page as the last element of the body. However, to allow the programmer to capture user interface events for sending to analytics, they want to make the _gaq variable to be used at an early stage. For example, you might have a button: <button onclick="_gaq.push(...)">Track</button> . By making _gaq available at an early stage, a small bit of code in the asynchronous fragment will queue these messages, and the heavier ga.js will send them to the server later.
Now some details: ga.js loaded by adding a new <script> element to the head of the document using the async attribute set. IE and WebKit will asynchronously load <script> tags inserted from scripts. Firefox and Opera will evaluate the async attribute and also load the script asynchronously. In any case, ga.js loads asynchronously, with the convenience of a browser.
Finally, once ga.js is executed without blocking the page display due to asynchronous loading, it can do the hard work of collecting all user data and any messages in the _gaq queue and sending them to the server.
Summary: This approach uses a small built-in script that initializes some key variables, such as _gaq , so that your page can access until the full ga.js script is ready. This small script also dynamically adds the <script src="ga.js"> to the document so that most browsers will load and execute it asynchronously without blocking page rendering or evaluating critical scripts.
ide
source share