Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the name of Google Analytics async design pattern and where is it used?

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?

like image 385
Eduardo Avatar asked Aug 05 '11 23:08

Eduardo


People also ask

What are Google Analytics used for?

Google Analytics is a platform that collects data from your websites and apps to create reports that provide insights into your business.

What is Google Analytics default tracking?

Google Analytics collects the following information through the default implementation: Number of users. Session statistics. Approximate geolocation.

How many sites use Google Analytics?

If you have a website, you want to know how people interact with it. The most popular way to do this is by using Google Analytics. According to BuiltWith, over 28 million sites—including 74% of the most popular 10,000—were using the tool as of April 22, 2022.


1 Answers

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

like image 197
Yahel Avatar answered Sep 28 '22 11:09

Yahel