You're making a simple mistake of trying to insert a callback function to the push method of Array.prototype
, which does not exist by default. I mean, Array.prototype.push()
does not take callback functions. Since _gaq
is an Array
, it won't work. What you're doing, is simply adding the function you just defined to the array _gaq
when you call _gaq.push(function(){...});
so it does not trigger any function calls. What you can do is define a function yourself like this person here is trying to do.
For the sake of simplicity, let me add the code here:
_gaq.push = function (){
//Callback Function Call here...
return Array.prototype.push.apply(this,arguments);
}
This method will execute the callback every time you use the _gaq.push()
function.
You seem to have misunderstood the documentation at Google Developer Docs. It states, In addition to pushing command arrays, you can also push function objects. This can be especially useful for tracker methods that return values. These functions can reference both _gat and _gaq.
You are only pushing a function object, and not using the function as a callback. Please be clear on this point. And if the function returns any values that should be used by GA, for example, functions that return an array of strings which can be pushed to GA, the return values are pushed.
And just to be sure, you are referring to analytics.js
in your question (the link for the documentation), while the _gaq
object is never used in that implementation (_gaq
is only used in the older ga.js
implementation).
Even a simple function(){alert('1');}
executes when pushed to _gaq
. Again, this is NOT a callback, just a function call executed upon pushing that function.