简体   繁体   中英

Google analytics, _gaq.push(callback), the callback is never executed

I'm using google analytics and tracking data accross iframe, according to the documentation at enter link description here

So in the outer page (containig the iframe) I'm using the _gaq.push() method to push and execute a callback that embeds the iframe at runtime.

But that callback is never executed.

Furthermore, in the outer page I have two tracking script, and the callback has to be executed by the second tracking script.

The code that pushes the callback is the following

_gaq.push(['ga_code_second_tracking_script._setAccount', '<ga_code_of_second_tracking_script>']);
_gaq.push(function(){
                    console.log('IN PUSH');
                    var tracker = _gat._getTracker('<ga_code_of_second_tracking_script>');
                    embedIframe();                
})

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM