6

My application uses Google Analytics to track exceptions and crashes (among other thigs). I use this function to get the stacktrace:

public static void sendErrorReportViaGoogleAnalytics(Exception e) {

    e.printStackTrace();
    Tracker myTracker = EasyTracker.getTracker();
    myTracker.sendException(getDescription(e), false);
}

public static String getDescription(Exception t) {

    final StringBuilder result = new StringBuilder();
    result.append(t.toString());
    result.append(',');
    String oneElement;

    for (StackTraceElement element : t.getStackTrace()) {
        oneElement = element.toString();
        result.append(oneElement);
        result.append(",");
    }

    return result.toString();
}

This works fine, when talking about exceptions, I just call sendErrorReportViaGoogleAnalytics() in the catch part of my exception handling codes, but when it comes to crashes, I only get one line of the stacktrace, like

Binary XML file line #11: Error inflating class fragment

I set

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

in analytics.xml, as I'm using EasyTracker.

What should I do to get the full stacktrace in case of crashes as well?

Analizer
  • 1,594
  • 16
  • 30
  • I want to track crashes in analytics too. AFIK it is only possible to track the first 200bytes. Since I use the Tag-Manager I cannot use the property in @Raanan's answer. Do you know more? – rekire Mar 03 '15 at 08:15

2 Answers2

4

Just faced with this problem. Simply paste the given code to BaseApplication.onCreate() method of your project or to other place to set the custom ExceptionReporter for uncaught exceptions. And don't forget to declare the given flag in analytics.xml.

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

Custom uncaught exceptions reporter:

ExceptionReporter myHandler =
        new ExceptionReporter(EasyTracker.getInstance(this), GAServiceManager.getInstance(),
                              Thread.getDefaultUncaughtExceptionHandler(), this);

    StandardExceptionParser exceptionParser =
            new StandardExceptionParser(getApplicationContext(), null) {
                    @Override
                    public String getDescription(String threadName, Throwable t) {
                        return "{" + threadName + "} " + Log.getStackTraceString(t);
                    }
                };

    myHandler.setExceptionParser(exceptionParser);

    // Make myHandler the new default uncaught exception handler.
    Thread.setDefaultUncaughtExceptionHandler(myHandler);
sky
  • 296
  • 4
  • 8
  • 1
    Won't this give you doubled-up reports? Shouldn't you set the bool to false to avoid this? – Mark Jun 15 '14 at 16:01
  • Still have in analytics.xml this line: `true` And no duplicates in reports. – sky Jun 16 '14 at 09:40
  • 1
    For me, it seems to work with "false" too. Any chance you could try that to confirm? – Mark Jun 16 '14 at 13:56
  • It will work, cuz we use setDefaultUncaughtExceptionHandler method to set custom implementation of the uncaught exceptions handler for the main (UI) thread. GA reports is just the details of implementation. – sky Jun 17 '14 at 10:48
  • ga_reportUncaughtExceptions = true, doubled-up the reports! changing it to false resolved this issue – Ali Noureddine Apr 06 '16 at 10:57
  • This solution will send the exception message (via Log.getStackTraceString) which is against usage recommendations noted here: https://developers.google.com/analytics/devguides/collection/android/v4/exceptions#caught – jenglert Apr 16 '16 at 15:22
3

Since you didn't describe what you actually did in order to catch the crashes then I can only send you to the docs: https://developers.google.com/analytics/devguides/collection/android/v2/exceptions

If you are using EasyTracker you can declare:

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

otherwise you can implement the ExceptionReporter class as described and attach it to your thread.

Raanan
  • 4,777
  • 27
  • 47
  • 4
    Thanks a lot for your answer. I'm using true to track exceptions, but this is what only logs the first line of the stacktraces. – Analizer Apr 18 '13 at 11:47
  • In the link check out the ExceptionParser option and this is a duplicate with an answer (workaround using the ExceptionParser) : http://stackoverflow.com/questions/14009883/exception-stack-trace-lost-in-google-analytics-v2-for-android – Raanan Apr 18 '13 at 11:57
  • Thanks a lot Raanan, I will check this out and get back to you as soon as possible. – Analizer Apr 18 '13 at 11:59
  • @Raanan Hello I got also first line of the stacktraces.I create exceptionparser according to google developer link which you shared. I see there is also getDescription(String threadName, Throwable t) method is created. But further I can not move forward.Could you please send me a sample code for that .Please send me to my email mmaidul.islam@gmail.com. I am waiting for your positive reply. – Md Maidul Islam Sep 04 '13 at 13:07
  • @Maid786 GA has released V3, I'm not sure if the same bug/fix is relevant anymore... should try using V3 and following the explanations here: https://developers.google.com/analytics/devguides/collection/android/v3/exceptions Also they implemented a StandardExceptionParser that might be easier to use. – Raanan Sep 04 '13 at 13:34
  • @Raanan Thanks for your reply.But How can I catch this exception on Onstop/OnDestroy methods.I need to implemented this on onstop or on destroy method.Please reply .... – Md Maidul Islam Sep 10 '13 at 07:07
  • @Raanan Thanks for your reply.But How can I catch this exception on Onstop/OnDestroy methods.I need to implemented this on onstop or on destroy method.Please reply ....And What is the "MapBuilder"? I got here some error. I catch Throwable and Put it createexception method instead of e. By I got error in mapbuilder.What is it ? – Md Maidul Islam Sep 10 '13 at 07:15
  • MapBuilder is the new class for creating GA events/screenviews etc... you can see some examples here: https://developers.google.com/analytics/devguides/collection/android/v3/migration – Raanan Sep 10 '13 at 12:13
  • @Raanan Thank you very much.I see this but I need to crash report on onDestroy method.Could you please make a sample for that and share with my email id : mmaidul.islam@gmail.com – Md Maidul Islam Sep 11 '13 at 07:22
  • @Maid786 I'm sorry, it's not so clear to me what you are trying to achieve. – Raanan Sep 11 '13 at 13:08
  • 2
    I don't get this... Why is this the correct answer? It anwers nothing. I still only get the first line of the exception. – Roel Oct 16 '15 at 12:01
  • @DalvikVM, The full answer includes the first 4 comments regarding using ExceptionParser. And you should note that the answer was for an Old V2 SDK, the current SDK is V4. – Raanan Oct 17 '15 at 13:24