简体   繁体   English

在Google Analytics(分析)中记录JavaScript错误

[英]Logging javascript errors within 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. 我正在尝试获取有关Web应用程序中发生的JavaScript错误的信息,并使用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) {
        }
    });
});

This is successfully firing the events to Analytics for example: 例如,这可以成功将事件触发到Analytics(分析):

谷歌分析

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. 但是,当我单击“ JavaScript错误”时,它不会给我任何其他信息,例如应该与上面的代码一起发送的错误消息。

Any ideas why this isn't sending the error message along with the error? 为什么不将错误消息与错误一起发送的任何想法?

EDIT 编辑

It turns out I was declaring the window.onerror function incorrectly. 原来我在错误地声明了window.onerror函数。 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. 异常跟踪正是为此用例构建的。

In you case the problem is with declaring window.onerror() inside $(document).ready() , it will only catch errors after jQuery initialisation. 在您的情况下,问题在于在$(document).ready()声明window.onerror() $(document).ready() ,它将仅在jQuery初始化后捕获错误。 You need to declare it as early as possible. 您需要尽早声明它。

Add following into <head> : 将以下内容添加到<head>

<script>
(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','_watchdog');

_watchdog('create', 'UA-xxxxxxx-x', 'auto');

window.onerror = function(msg) {
  _watchdog('send', 'exception', { 'exDescription': msg });

  _watchdog('send', 'event', {
    eventCategory: 'javascript',
    eventAction: 'error',
    eventLabel: msg,
    transport: 'beacon'
  });
}
</script>

After that you will start receiving events with 'error' type under 'Javascript' category. 之后,您将开始在“ Javascript”类别下接收“ error”类型的事件。

Unfortunately, in Event Tracking , your error messages will be limited by 500 Bytes , so you will not be able to understand a problem properly, however you will know that something is going wrong :) 不幸的是,在事件跟踪中 ,您的错误消息将受到500字节的限制 ,因此您将无法正确理解问题,但是您会知道出了点问题:)

For mo detailed errors use Exception Tracking 对于更详细的错误,请使用异常跟踪

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

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