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

查看:127
本文介绍了Google Analytics(分析)异步设计模式的名称是什么,它在哪里使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google Analytics(分析)异步代码使用非常独特的设计模式用于执行JavaScript代码。

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

代码取决于库,它不知道库是否加载。如果库没有加载,则它将所有命令排队到Array对象中。当库加载时,只需创建_gaq对象,并按照包含的顺序执行所有命令。然后覆盖推送功能,以便将来的命令立即执行。

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.

他们还加载了一个参数 async = true 。这对于实际的页面加载时间几乎没有影响。

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

命令看起来就像它的同步版本,但是第一个字符串是一个函数名,下一个参数是功能参数。您还可以将函数推送到此数组中,并且函数也将依次执行一个空的上下文。所以如果你需要和图书馆做同步的事情,你可以在_gaq中推一个函数去做。

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.

我认为这是一个非常聪明的解决方案,但我从未见过之前。有没有人知道这种设计模式的名称,除了Google Analytics(分析)跟踪代码之外还有哪些用途?

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.

有趣的是,虽然我没有由于谷歌已经采用Google Analytics(分析),所以被不同平台广泛采用,正在考虑使用异步果汁(Disqus想到)。

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.)

博客文章是最多的,我已经阅读过异步Google Analytics(分析)语法的深入检查,并且包含了如何复制模式的相当详细的说明:

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:

从博客文章: p>

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);

它还注意到,即使没有很多浏览器支持 async 属性,注入方法使脚本在大多数浏览器中异步加载,并包含一个有用的图表:

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:

这篇关于Google Analytics(分析)异步设计模式的名称是什么,它在哪里使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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