Google Analytics会两次发送未捕获的异常

时间:2014-05-08 13:32:50

标签: android exception google-analytics

为了将未捕获的异常的堆栈跟踪包含到Google Analytics中,我将默认的ExceptionParser替换为自定义异常。 现在我使用stackstrace +另一个报告获取异常,但没有stacktrace(默认值)。我做错了什么?

public class AnalyticsHelper
{
    /**
    * This method is called in my Application#onCreate(..)
    **/
    public static void initAnalytics(Context c)
    {
        GoogleAnalytics analytics = GoogleAnalytics.getInstance(c);
        analytics.setAppOptOut(PreferenceManager.getDefaultSharedPreferences(c).getBoolean(PreferencesActivity.KEY_ANAYLTICS_OPT_OUT, false));

        // setting uncaught exception handler
        ExceptionReporter reporter = new ExceptionReporter(analytics.getInstance(c).newTracker(R.xml.tracker), Thread.getDefaultUncaughtExceptionHandler(), c);
        reporter.setExceptionParser(new ExceptionParser()
        {
            @Override
            public String getDescription(String p_thread, Throwable throwable)
            {
                StringWriter trace = new StringWriter();
                throwable.printStackTrace(new PrintWriter(trace));
                String stacktrace = trace.toString();

                String message = (throwable.getMessage() == null) ? "no message" : throwable.getMessage();
                return "Message: " + message
                        + ", Stacktrace: " + stacktrace;
            }
        });

        Thread.setDefaultUncaughtExceptionHandler(reporter);
    }
}

tracker.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
           tools:ignore="TypographyDashes">
    <integer name="ga_sessionTimeout">300</integer>
    <bool name="ga_reportUncaughtExceptions">true</bool>
    <bool name="ga_autoActivityTracking">false</bool>
    <integer name="ga_dispatchPeriod">5</integer>
    <string name="ga_trackingId">XXXXXX</string>
</resources>

1 个答案:

答案 0 :(得分:3)

来自Google Analytics document

  

要使用EasyTracker自动发送应用中所有未捕获的异常,请将此行添加到您的analytics.xml文件中:

     

<bool name="ga_reportUncaughtExceptions">true</bool>

     

使用自动异常测量发送异常后,EasyTracker会将异常传递给Thread的默认异常处理程序。

通过将ga_reportUncaughtExceptions设置为truetacker会自动发送异常,因此您可以获得一个没有堆栈跟踪的额外报告。删除它,您将只收到一份报告。