在Google Analytics中记录javascript错误 [英] Logging javascript errors within Google Analytics

查看:64
本文介绍了在Google Analytics中记录javascript错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取有关我的网络应用程序中发生的JavaScript错误的信息,并使用Google Analytics中的事件记录这些错误。我这样做如下:

I'm trying to get information on JavaScript errors that are occurring within my web application and logging these using events within Google Analytics. I'm doing this as follows:

$(document).ready(function() {
    var sendAnalyticsEvent = function(category, action, label) {
        if (window.ga) {
            window.ga('send', 'event', category, action, label);
        }
    };

    window.onerror = function(error) {
        try {
            sendAnalyticsEvent('JavaScript Error', error.message, error.filename + ':  ' + error.lineno);
        } catch(e) {
        }
    };

    $(document).ajaxError(function(e, request, settings) {
        try {
            sendAnalyticsEvent('Ajax Error', settings.url, e.result);
        } catch(e) {
        }
    });
});

例如,这已成功向Google Analytics触发事件:

This is successfully firing the events to Analytics for example:

但是,当我点击JavaScript错误时,它不会向我提供任何其他信息,因为例如,应该使用上面的代码发送的错误消息。

However when I click into 'JavaScript Error' it doesn't give me any other information, for example the error message that should have been sent with the code above.

为什么不发送错误消息以及错误的任何想法?

Any ideas why this isn't sending the error message along with the error?

编辑

事实证明我正在声明window.onerror功能不正确。以下工作正常:

It turns out I was declaring the window.onerror function incorrectly. The following works correctly:

window.onerror = function (message, source, lineno, colno, error) {
    try {
        sendAnalyticsEvent('js error', message, source + ':  ' + lineno);
    } catch(e) {
    }
};


推荐答案

您将错误消息作为事件标签发送维度,但该报告正在查看事件类别维度。如果您点击事件标签,您应该会看到它。

You're sending the error message as the Event Label dimensions, but that report is looking at the Event Category dimension. If you click on Event Label, you should see it.

话虽如此,为什么不使用异常跟踪而非事件跟踪来衡量这一点?完全针对此用例构建了异常跟踪。

That being said, why not use exception tracking instead of event tracking to measure this? Exception tracking was built exactly for this use case.

这篇关于在Google Analytics中记录javascript错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆