最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - When and how does Asynchronous Google Analytics execute? - Stack Overflow

programmeradmin0浏览0评论

I'm switching over our site to use Asynchronous Google Analytics, and I'm curious about it's method of pushing events to the _gaq array. Near as I can tell, events are placed into a waiting pattern in _gaq while the ga.js script is being asynchronously downloaded. Do they fire once the script is downloaded, and how are post-document load events tracked?

One example is a user clicking a link 10 seconds after page load - according to documentation the event should be placed into the _gaq. How does the analytics script detect this?

I'm switching over our site to use Asynchronous Google Analytics, and I'm curious about it's method of pushing events to the _gaq array. Near as I can tell, events are placed into a waiting pattern in _gaq while the ga.js script is being asynchronously downloaded. Do they fire once the script is downloaded, and how are post-document load events tracked?

One example is a user clicking a link 10 seconds after page load - according to documentation the event should be placed into the _gaq. How does the analytics script detect this?

Share Improve this question asked Mar 4, 2010 at 20:16 Mike RobinsonMike Robinson 25.2k8 gold badges63 silver badges83 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

The general part is best described by the Google Analytics Async doc.

To push an API call onto the queue, you must convert it from the traditional JavaScript syntax into a mand array. Command arrays are simply JavaScript arrays that conform to a certain format. The first element in a mand array is the name of the tracker object method you want to call. It must be a string. The rest of the elements are the arguments you want to pass to the tracker object method. These can be any JavaScript value.

I'll try to explain the juicy details: _gaq is just a plain JavaScript Array, and all arrays have the push method to add an entry to the end of the array. So before the Analytics script is loaded, all mands will be pushed to the array. At the end of the Analytics script, it replaces the _gaq.push method with a custom method and executes all entries in the _gaq array. The new _gaq.push method will run the tracking method instantly. So when you run the push method 10 seconds after page load, the mand should be executed.

It's always difficult to un-obfuscate the google analytics code, but if I were to solve this problem, upon loading the script, I would redefine the push function on the array after processing everything in the backlog queue (_gaq). This way, the redefined push function would simply act as a proxy to the actual function calls on the pageTracker object.

Here's a simple example of redefining the push method on an array object:

var foo = [];
foo.push("one");                      // pushes "one" onto the array
foo.push("two");                      // pushes "two" onto the array
foo.push = function(a) { alert(a) };  // redefines `push`
foo.push("three");                    // alerts "three"
alert(foo);                           // alerts "one,two"
发布评论

评论列表(0)

  1. 暂无评论