When and how does Asynchronous Google Analytics execute? - javascript

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?

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 command array. Command arrays are simply JavaScript arrays that conform to a certain format. The first element in a command 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 commands 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 command 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"

Related

node js can array be modified during for loop, by external function?

Lets say that my program is currently in a for loop, but for some reason its taking too long. Lets say that somehow the algorithm is so slow that it takes about 500ms to loop through maybe 100 elements in the array.
Is it possible that while the program is inside the array, perhaps doing work with element number 50, that I get incoming commands from the network, telling me to add more elements or delete elements from the array?
A practical example is a matchmaking queue server. Every time a user joins, the server will try to match players as best as it can by calling a function, but it may be possible that a player who is in the queue, who hasn't been "reached" yet by the function, to simply leave and exit. Would this somehow corrupt the for loop search?
Summary: I have an array I want to loop through, can it have its elements changed (add/delete) during the for loop caused by external functions? Or will those functions be called after the program is done with the for loop? (Like a database being locked while a query is being worked into it to prevent these same problems)
JavaScript runs on a single event loop. So long as it is running your function, it won't run any other function.
If you get an event saying that an item should be added to the array, then the event handler for that event can't fire until the currently running function is finished.

Google Apps Script update properties with Trigger

I am creating a script to collect specific datas from Gmail to Google SpreadSheet. Because there are too many mails the execution time is over 6 minutes so i want to use triggers to automaticaly run the script every 6 minutes. My idea is too use Script Properties to store the ID of the last appended thread in order to get it back at the next script execution and keep gathering datas from other threads.
The problem is: when i manualy start the script the Properties are well updated but when the Trigger does it the Properties are not updated. Do you have any explanations ?
Well, never mind, i have found the problem, so for any one having the same one:
Don't try to test your script with an ui.alert when using a Trigger, it was firring an error and prevented the correct execution of the instructions after that.

Monitor changes to an array

I'm using a third party javascript library in my application and I need to be able to monitor a particular array property for changes. So for example, the third party library adds an object to widget.loadingQueue anytime a widget is being loaded. I want to be able to monitor this property and display a loading indicator anytime there are elements in the array. The library is open source, but if possible I'd like to do this without having to go through their source and find every time they add something to the array and add in a method call.
If they add to the array by calling push(), you can simply create a custom push() method on the array that runs your handling code and calls Array.prototype.push().
This is how Google Analytics' _gaq array works.

Using ajax to load paired html and js files and keeping order

I have a page that uses Ajax to load in sections of a page.
Each section consists of a html files and a JavaScript file that defines events for just that bit of html.
What I am trying to do is figure a method of managing the file loading that can bind keep the two files bound together.
Here is pseudo-code of how I am attempting to make it work now but I don't know if there is a more organized way.
Pseudo-code: (using jQuery)
Cycle through list of html/js file pairs that need loaded.
Add an object to the an array that uniquely identifies the pair of files. This object will eventually hold the container for the html and the js object
Start loading of html. When html returns, append content to the page and record the id in the array object
This is easy because I use the content for the jQuery get callback directly.
Start loading the js. When the file loads, the js executes and updates the object in the array with a reference to the files return value.
*This is the hard part. jQuery.getScript() automatically executes the script when it completes, so I cant use the return value because its already created. Since I cant use the ajax response I have to have the js file already know the object it will be adding itself to
So, I was hoping there was some js lib already available that does some data-binding between pairs of html and js.
Also, I wasn't sure how to structure the object manager.
Each object in the js files are going to have the same events bound that get called when you move to that section.
Sorry this is kind of a loaded question.
I think my AJAX library might help:
http://depressedpress.com/javascript-extensions/dp_ajax/
One of the most useful features is the ability to define a single "request" (which fires a single handler) with multiple "calls" (individual HTTP calls). You can thus define a single request to contain both the call for the script and a second call for the HTML. When both calls are complete the defined handler will be called and get the results passed to it in the correct order.
The request pool used to process the requests can be instantiated with multiple background objects (allowing multi-threading) so that while the requests get all your data before calling your handlers they're not forced to waste time by single threading (although you can do that as well if you like).
Here's a simple example of how it might be used:
// The handler function
function AddUp(Nums) { alert(Nums[1] + Nums[2] + Nums[3]) };
// Create the pool
myPool = DP_AJAX.createPool();
// Create the request
myRequest = DP_AJAX.createRequest(AddUp);
// Add the calls to the request
myRequest.addCall("GET", "http://www.mysite.com/Add.htm", [5,10]);
myRequest.addCall("GET", "http://www.mysite.com/Add.htm", [4,6]);
myRequest.addCall("GET", "http://www.mysite.com/Add.htm", [7,13]);
// Add the request to the pool
myPool.addRequest(myRequest);
"myRequest" defines the handler. The addCall() methods add multiple calls. Once all three are results will be sent to the handler as an array of three responses (in the same order that the calls were added).
You could calls your files in pairs or even call them all bundled into a single request - up to you.
Hope this helps!

Google Analytics Events - when are they send?

My "application" is a single web page (myPage.html) displaying items on a Google Map. So it is not a scenario where the user navigates among several pages.
Therefor I am using events to track the interactions. However, I find it strange to believe what I see in the statistics.
I have checked the events for syntax and it seems to be OK.
_gaq.push(['_trackEvent', 'MyApp', 'ChangeTav', newTab]);
_gaq.push(['_trackEvent', 'MyApp', 'Load', 'itemType', loadTime]);
....
What I wonder about, I do add Arrays to to _gaq object, but when are these actually send? What happens if the user closes the browser? And is there a way for me to "force" sending these values?
The other way around, user stays on the page for some time, values get added to _gaq, but how I do make sure they not getting lost?
-- Update --
With the push I simply add an Array to an Array, or is this wrong? So in this particular moment nothing happens, I did not see any callbacks or overridden methods. Please correct if I am missing something.
-- Update 2 --
http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html
It seems to be different whether I use sync or async. For async (I am using) ...
To push an API call onto the queue, you must convert it from the traditional JavaScript syntax into a command array. Command arrays are simply JavaScript arrays that conform to a certain format. The first element in a command 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.
When I do init with var _gaq = _gaq || []; it becomes an array. However, I do never see values being removed from the queue (_gaq), so I'd assume they are never send.
--- OK, here we go ---
http://code.google.com/apis/analytics/docs/gaJS/gaJSApi_gaq.html#_gaq.push
This function is named push so that an array can be used in the place of _gaq before Analytics has completely loaded. While Analytics is loading, commands will be pushed/queued onto the array. When Analytics finishes loading, it replaces the array with the _gaq object and executes all the queued commands. Subsequent calls to _gaq.push resolve to this function, which executes commands as they are pushed.
I believe, it's send as soon as you call it. Provided that the _gaq has been initialized. Depends on where you put the GA init code.
The .push method you are referring to is only for arrays. With Objects, you can define it yourself, you are not overriding anything.
Arrays in js can't have custom indexes / labels, if they do, they are objects.
If you run
var a= {"a":"b"};
var b = ['a','b'];
b.push("c");
a.push("c","d");
You will get an error , function a.push is not defined.
a is an object, b is an array. The push function only exists in the prototype of Array.
To observe when exactly the stuff is tracked, you can use the Net panel in Firebug and check for a _utm.gif file being requested.

Categories