Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Analytics sends uncaught exceptions twice

In order to include the stacktrace of uncaught exceptions into google analytics, I replaced the default ExceptionParser with a custom one. Now I get Exceptions with their stackstrace + another report with the exception without stacktrace (the default one). What am I doing wrong?

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>
like image 306
danijoo Avatar asked Sep 23 '26 02:09

danijoo


1 Answers

From the Google Analytics document:

To automatically send all uncaught exceptions in your app using EasyTracker, add this line to your analytics.xml file:

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

After sending an exception using automatic exception measurement, EasyTracker will pass the exception on to the Thread's default exception handler.

By setting the ga_reportUncaughtExceptions to true, the tacker will automatically send the exception, thus you got one extra report without stacktrace. Remove it and you will get one report only.

like image 124
goofyz Avatar answered Sep 24 '26 18:09

goofyz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!