【问题标题】:Google analytics, _gaq.push(callback), the callback is never executed谷歌分析,_gaq.push(callback),回调永远不会执行
【发布时间】:2014-02-02 00:08:18
【问题描述】:

根据enter link description here 的文档,我正在跨 iframe 使用谷歌分析和跟踪数据

所以在外部页面(包含 iframe)中,我使用 _gaq.push() 方法推送并执行在运行时嵌入 iframe 的回调。

但该回调永远不会执行。

此外,在外页我有两个跟踪脚本,回调必须由第二个跟踪脚本执行。

推送回调的代码如下

_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();                
})

【问题讨论】:

    标签: javascript iframe callback google-analytics push


    【解决方案1】:

    你犯了一个简单的错误,试图在Array.prototype 的 push 方法中插入一个回调函数,默认情况下不存在。我的意思是,Array.prototype.push() 不接受回调函数。因为_gaqArray,所以它不起作用。您所做的只是在调用_gaq.push(function(){...}); 时将刚刚定义的函数添加到数组_gaq 中,这样它就不会触发任何函数调用。你可以自己定义一个函数like this person here is trying to do

    为了简单起见,我在这里添加代码:

    _gaq.push = function (){
        //Callback Function Call here...
        return Array.prototype.push.apply(this,arguments);
    }
    

    每次使用_gaq.push()函数时,该方法都会执行回调。

    您似乎误解了 Google Developer Docs 中的文档。它指出,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. 您只是在推送一个函数对象,而不是将该函数用作回调。请明确这一点。如果函数返回任何应该被 GA 使用的值,例如,返回可以推送到 GA 的字符串数组的函数,则返回值被推送。

    为了确定,您在问题中指的是analytics.js(文档的链接),而_gaq 对象从未在该实现中使用(_gaq 仅用于较旧的@ 987654335@实现)。

    即使是简单的function(){alert('1');} 在推送到_gaq 时也会执行。同样,这不是回调,只是在推送该函数时执行的函数调用。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多