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

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

问题描述

Google Analytics async 代码使用非常独特的设计模式用于 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 将其用于 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:

来自博文:

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天全站免登陆