简体   繁体   English

analytics.js 事件并不总是发送

[英]analytics.js event not always sent

I implemented analytics.js (new version) in my ASPX webapplication.我在我的 ASPX 网络应用程序中实现了 analytics.js(新版本)。 I got pageview working just fine, but events is very often not being send.我的综合浏览量工作得很好,但经常没有发送事件。

In my example, I tried pushing 3 different buttons, but only one of them fired the event.在我的示例中,我尝试按下 3 个不同的按钮,但其中只有一个触发了该事件。 I added an alert-box to each event, to verify that it actually is fired, and these all show.我为每个事件添加了一个警报框,以验证它是否确实被触发,这些都显示出来了。

提琴手 This is my js, placed just before the </head>这是我的js,放在</head>

(function(i, s, o, g, r, a, m) {
    i['GoogleAnalyticsObject'] = r;
    i[r] = i[r] || function() {
        (i[r].q = i[r].q || []).push(arguments)
    }, i[r].l = 1 * new Date();
    a = s.createElement(o), m = s.getElementsByTagName(o)[0];
    a.async = 1;
    a.src = g;
    m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-XXXXXXXX-1', {
    'cookieDomain': 'none'
});
ga('send', 'pageview', {
    'page': '/team/main',
    'title': 'Logged in'
});

$(document).ready(function() {
    $(".team_button").on("click", function() {
        ga('send', 'event', 'button', 'click', 'Team select button', {
            'page': '/team/'
        })
    });
    $(".calendar_month_prev").on("click", function() {
        ga('send', 'event', 'button', 'click', 'Calendar: Prev month', {
            'page': '/team/'
        })
    });
    $(".calendar_month_next").on("click", function() {
        ga('send', 'event', 'button', 'click', 'Calendar: Next month', {
            'page': '/team/'
        })
    });
    $(".calendar_day_selected").on("click", function() {
        ga('send', 'event', 'button', 'click', 'Calendar: Same day reload', {
            'page': '/team/'
        })
    });
    $(".calendar_day_active").on("click", function() {
        ga('send', 'event', 'button', 'click', 'Calendar: Select day', {
            'page': '/team/'
        })
    });
});

When a new page is being loaded, pending requests on the current page are canceled -- in this case the analytics tracking pixel request.当加载新页面时,当前页面上的待处理请求将被取消——在这种情况下是分析跟踪像素请求。 'async' is referring more to the loading of the analytics javascript code then the processing of analytics data. “异步”更多地指的是加载分析 javascript 代码,然后是分析数据的处理。

With ga.js, a common approach is to stop propagation of the click event, send the event, and then delay for a small amount (150ms) before following the link.对于 ga.js,一种常见的方法是停止点击事件的传播,发送事件,然后在跟随链接之前延迟一小段时间(150 毫秒)。

With analytics.js, instead of a delay, you can use hitCallback to run code after the analytics data has been sent.使用 analytics.js,您可以使用hitCallback在发送分析数据后运行代码,而不是延迟。 See Setting the Hit Callback in the Google Analytics docs请参阅 Google Analytics 文档中的设置命中回调

Okay, here is how I fixed it, if anyone else has the same problem:好的,这是我修复它的方法,如果其他人有同样的问题:

I made a function, to call the analytics tracking, where I delayed the page for just a fraction of a second:我创建了一个函数来调用分析跟踪,在那里我将页面延迟了几分之一秒:

function TrackEvent(link, category, action, label, page) {
    try {
        ga("send", "event", category, action, label, { 'page': page });
    } catch (err) { consol.log(err); }

    setTimeout(function () {
        document.location.href = link.href;
    }, 25);
}

Then, each of my onClick binds calls this function, like this:然后,我的每个 onClick 绑定都会调用此函数,如下所示:

$(document).ready(function () {
    $(".calendar_month_prev").on("click",function(){ TrackEvent(this, 'button', 'click', 'Calendar: Prev month', '/team/main'); return false;});
    $(".calendar_month_next").on("click",function(){ TrackEvent(this, 'button', 'click', 'Calendar: Next month', '/team/main'); return false;});
    $(".calendar_day_active").on("click",function(){ TrackEvent(this, 'button', 'click', 'Calendar: Select day', '/team/main'); return false;});
});

One could probably just add this to the a href onClick, if this is preferred :)如果您愿意,可以将其添加到 a href onClick 中:)

I would suggest executing a dummy anchor click after successful event registration :我建议在成功注册事件后执行虚拟锚点点击:

a.onclick = function(e){
            var anchor = this;
            _gaq.push(['_trackEvent', 'Category','event','label']);
            _gaq.push(function() {
                var a = document.createElement('a');
                a.href = anchor.href;
                a.target = anchor.target;
                a.click();
            });
            return false;
        }

Since push works as a queue, your event request will never be cancelled..由于推送作为队列工作,您的事件请求将永远不会被取消..

As pointed out by Mike, you should use a hit callback in this case.正如 Mike 所指出的,在这种情况下,您应该使用命中回调。 In addition, you also need to take into account that users may block Google Analytics using some privacy protection tool such as Ghostery, in which case the hit callback will never be executed.此外,您还需要考虑到用户可能会使用某些隐私保护工具(例如 Ghostery)来阻止 Google Analytics,在这种情况下,将永远不会执行命中回调。 Therefore you need to implement this very carefully so that your site continues to work even for these users.因此,您需要非常小心地实施这一点,以便您的网站即使对这些用户也能继续工作。 The following article explains how to do this properly:以下文章解释了如何正确执行此操作:

http://veithen.github.io/2015/01/24/outbound-link-tracking.html http://veithen.github.io/2015/01/24/outbound-link-tracking.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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