弹出窗口不适用于UA和GTM

popup not working with UA and GTM

本文关键字:UA GTM 适用于 不适用 窗口      更新时间:2023-09-26

我们最近将我们的网站更新为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>

如果我们评论掉谷歌标签,弹出窗口会起作用,但不会被跟踪。不幸的是,这在页面上存在,分析在GTM中,但我们通过直接在网站上添加分析进行了测试,但它仍然不起作用。

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

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

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

其余部分也是如此。