Google Analytics异步设计模式的名称是什么,在哪里使用?[英] What's the name of Google Analytics async design pattern and where is it used?

本文是小编为大家收集整理的关于Google Analytics异步设计模式的名称是什么,在哪里使用?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

google Analytics async async ya>代码使用非常独特的设计模式对于JavaScript代码执行.

代码取决于库,它不知道库是否已加载.如果库还没有加载,则将所有命令排队到数组对象中.当库加载时,它只会创建_gaq对象,并在其中包括的序列中执行所有命令.然后,它覆盖推送函数,以便立即执行未来命令.

这个想法是使命令在排队时非常快.该代码仅在库加载后才真正评估.

他们还加载了库中的参数async=true.这几乎不会影响实际的页面加载时间.

命令看起来就像它的同步版本一样,但是第一个字符串是函数名称,下一个参数是该函数参数.您还可以将功能推入此数组,并且功能将在零上下文中按顺序执行.因此,如果您需要与库同步执行某些操作,则可以在_gaq内部推动功能来执行此操作.

我认为这是一个非常聪明的解决方案,但我以前从未见过.有人知道此设计模式的名称还是Google Analytics(分析跟踪代码)使用的位置?

推荐答案

我将其称为"异步函数排队",但它不是一个吸引人的名称,当然不是设计模式的正式名称.

有趣的是,尽管我以前从未看到过这种特殊模式,因为Google将其用于Google Analytics(分析),但它被不同的平台广泛采用,希望纳入异步果汁(Disqus)(Disqus). p>

这个博客文章 - 对我已阅读的异步Google分析语法的深入研究,并包括一个相当详细的解释,即如何复制该模式:

来自博客文章:

var GoogleAnalyticsQueue = function () {

    this.push = function () {
        for (var i = 0; i < arguments.length; i++) try {
            if (typeof arguments[i] === "function") arguments[i]();
            else {
                // get tracker function from arguments[i][0]
                // get tracker function arguments from arguments[i].slice(1)
                // call it!  trackers[arguments[i][0]].apply(trackers, arguments[i].slice(1));
            }
        } catch (e) {}
    }

    // more code here…
};

// get the existing _gaq array
var _old_gaq = window._gaq;

// create a new _gaq object
window._gaq = new GoogleAnalyticsQueue();

// execute all of the queued up events - apply() turns the array entries into individual arguments
window._gaq.push.apply(window._gaq, _old_gaq);

它还指出,即使没有多少浏览器支持async属性,但使用的注射方法在大多数浏览器中都会使脚本负载异步,并且包含一个有用的图表:

在此处输入图像说明

其他推荐答案

它所做的只是将数据推入全局数组

var _qaq = _qaq || [];
_qaq.push(stuff);

基本上允许您在库加载之前缓冲数据.

这种模式不多使用的主要原因是其他库通常需要资源来加载,然后才能完成任何操作.开始缓冲jQuery命令是没有意义的.

这不是一种模式,它只是将数据存储在全球范围中,并说处理此操作是某种elses工作,我不在乎您在处理时不在乎.

但是,这是处理您不知道何时加载或准备就绪的事实的一种聪明方法,常见的替代方案是一个domready块.

其他推荐答案

可以在此处提供javascript加载阶层的很好的文章 http://frightlybit.com/js/js/lazy-loading-loading-ashyncronous-javascript/a>

,就我记得而言,ga.js async语法受到 Steve Soders 的启发.您可以查看 controljs ,他的项目之一

本文地址:https://www.itbaoku.cn/post/627716.html

问题描述

Google Analytics async code uses a very distinct design pattern for javascript code execution.

The code depends on a library and it doesn't know if the library has loaded or not. If the library didn't load yet it just queues all the commands into an Array object. When the library loads it just creates the _gaq object and executes all commands in the sequence it was included. It then overwrites the push function so future commands are executed right away.

The idea is to make the commands run very fast when they are queued. The code is only really evaluated later when the library is loaded.

They also load the library with a parameters async=true. This causes almost no impact on the actual page loading time.

The commands look just like the sync versions of it, but the first string is a function name and the next parameters are that function parameters. You can also push functions into this array and the functions will be executed in sequence as well with a null context. So if you need to do something synchronous with the library you can push a function to do this inside _gaq.

I think this is a very clever solution but I have never seen it before. Does anyone know the name of this design pattern or where it's used besides the Google Analytics tracking code?

推荐答案

I've referred to it as "asynchronous function queuing", but its not quite a catchy name, and certainly not the formal name of the design pattern.

What's interesting is that, though I hadn't seen this particular pattern used before, since Google adopted it for Google Analytics, its been adopted widely by different platforms looking to nab the asynchronous juice (Disqus comes to mind.)

This blog post is the most in-depth examination of the async Google Analytics syntax I've read, and includes a fairly detailed explanation of how the one can replicate the pattern:

From the blog post:

var GoogleAnalyticsQueue = function () {

    this.push = function () {
        for (var i = 0; i < arguments.length; i++) try {
            if (typeof arguments[i] === "function") arguments[i]();
            else {
                // get tracker function from arguments[i][0]
                // get tracker function arguments from arguments[i].slice(1)
                // call it!  trackers[arguments[i][0]].apply(trackers, arguments[i].slice(1));
            }
        } catch (e) {}
    }

    // more code here…
};

// get the existing _gaq array
var _old_gaq = window._gaq;

// create a new _gaq object
window._gaq = new GoogleAnalyticsQueue();

// execute all of the queued up events - apply() turns the array entries into individual arguments
window._gaq.push.apply(window._gaq, _old_gaq);

It also notes that, even though not many browsers support the async attribute, the method of injection used makes the script load asynchronously in most browsers, and includes a helpful chart:

enter image description here

其他推荐答案

All it's doing is pushing data into a global array

var _qaq = _qaq || [];
_qaq.push(stuff);

It basically allows you to buffer up data to deal with before the library has loaded.

The main reason this pattern isn't used much is that other libraries generally need the resources to load before they can do anything. It wouldn't make any sense to start buffering jQuery commands.

It's not a pattern it's simply storing data in global scope and saying it's some-one elses job to process this, I don't care when you process it.

However it is a clever way to deal with the fact you don't know when something is loaded or ready, the common alternative is a DOMReady block.

其他推荐答案

A good writeup of javascript loading stratgies is available here http://friendlybit.com/js/lazy-loading-asyncronous-javascript/

And as far as I recall, ga.js async syntax has been inspired by Steve Souders. You can look at ControlJS , one of his projects