弹出窗口不能与UA和GTM一起使用

时间:2014-11-11 21:40:01

标签: javascript jquery google-analytics popup

我们最近将我们的网站更新为Universal Analytics和GTM。我们有一个弹出窗口可以退出页面并根据选择发送事件。现在,如果我们尝试标记事件,则弹出窗口不起作用。如果我们将弹出窗口的事件注释掉,但我们没有跟踪它。下面是脚本最初的原理和工作方式,然后是带有更改的脚本,现在可以阻止弹出窗口工作。

在脚本工作之前:

<script>
var goodExit               = false;
var through_onbeforeunload = false;
var count_onbeforeunload   = 1;

function unloadStatus() {
    if(through_onbeforeunload) {
        through_onbeforeunload = false;
         _gaq.push(['_trackEvent', 'onbeforeunload', 'returned from popup']);
    }
}

$(document).ready(function(){

    setInterval("unloadStatus()", 500);

    window.onbeforeunload = function() {
        through_onbeforeunload = true;
         _gaq.push(['_trackEvent', 'onbeforeunload', 'saw popup', '', count_onbeforeunload++]);

        if(!goodExit) {
            var my_string    = '--------------------------------------------\n';
            my_string       += 'YOUR APPLICATION HAS NOT YET BEEN SUBMITTED!\n';
            my_string       += '--------------------------------------------\n';
            my_string       += 'You are minutes away from completing your application.\n';
            my_string       += 'If you exit this page your information will not be saved.\n';
            my_string       += '\n';
            my_string       += 'CLICK THE STAY ON THIS PAGE BUTTON\n';
            my_string       += 'TO CONTINUE THE APPLICATION PROCESS.\n';
            my_string       += '--------------------------------------------';
            return my_string;
        }
    }

    window.onunload = function() {
        if(through_onbeforeunload) {
         _gaq.push(['_trackEvent', 'onbeforeunload', 'left through popup']);
        }
    }

    $('.js-app-submit').click(function() {
        goodExit              = true;
        window.onbeforeunload = null;
    });
});
</script>

下面当我们实施UA和GTM时,我们将gaq.push更改为ga('发送','事件......并且一切都停止了工作。

<script>
var goodExit               = false;
var through_onbeforeunload = false;
var count_onbeforeunload   = 1;

function unloadStatus() {
    if(through_onbeforeunload) {
        through_onbeforeunload = false;
         ga(['send', 'event', 'onbeforeunload', 'returned from popup']);
        }
}

$(document).ready(function(){

    setInterval("unloadStatus()", 500);

    window.onbeforeunload = function() {
        through_onbeforeunload = true;
        ga(['send', 'event', 'onbeforeunload', 'saw popup', count_onbeforeunload++]);

        if(!goodExit) {
            var my_string    = '--------------------------------------------\n';
            my_string       += 'YOUR APPLICATION HAS NOT YET BEEN SUBMITTED!\n';
            my_string       += '--------------------------------------------\n';
            my_string       += 'You are minutes away from completing your application.\n';
            my_string       += 'If you exit this page your information will not be saved.\n';
            my_string       += '\n';
            my_string       += 'CLICK THE STAY ON THIS PAGE BUTTON\n';
            my_string       += 'TO CONTINUE THE APPLICATION PROCESS.\n';
            my_string       += '--------------------------------------------';
            return my_string;
        }
    }

    window.onunload = function() {
        if(through_onbeforeunload) {
            ga(['send', 'event', 'onbeforeunload', 'left through popup']);
        }
    }

    $('.js-app-submit').click(function() {
        goodExit              = true;
        window.onbeforeunload = null;
    });
});
</script>

如果我们注释掉Google标签,则弹出窗口有效但无法跟踪任何内容。不幸的是,它存在于页面上,分析在GTM中,但我们通过直接在网站上添加分析进行测试,但仍然无效。

任何帮助/建议,将不胜感激。

1 个答案:

答案 0 :(得分:1)

用于发送事件的语法不正确。不应该有任何方括号,例如对于unloadStatus函数中的第一个事件,它应该是:

ga('send', 'event', 'onbeforeunload', 'returned from popup'); 

其余的一样。