tugce
tugce

Reputation: 661

google analytics v3 catch all exceptions

I want to catch all exceptions and have report them meaningfully in google analytics. What I have done so far is :

set <bool name="ga_reportUncaughtExceptions">true</bool>, I guess this is working only for the activities that easytracker enabled like this: EasyTracker.getInstance(this).activityStart(this);

I wanted to catch all exceptions in application level, and easyTracker to also keep working in defined activites.

I have tried to modify this v2 solution into v3, but still not seeing my exception in google analytics(http://dandar3.blogspot.com/2013/03/google-analytics-easytracker-detailed.html)

EasyTracker easyTracker = EasyTracker.getInstance(this);

ExceptionReporter exceptionReporter = new ExceptionReporter( 
    easyTracker, // Tracker, may return null if not yet initialized.
    GAServiceManager.getInstance(),                        // GAServiceManager singleton.
    Thread.getDefaultUncaughtExceptionHandler(), this); 

exceptionReporter.setExceptionParser(new AnalyticsExceptionParser());

UncaughtExceptionHandler myHandler =  exceptionReporter;       // Current default uncaught exception handler.

// Make myHandler the new default uncaught exception handler.
Thread.setDefaultUncaughtExceptionHandler(myHandler);

Upvotes: 0

Views: 1140

Answers (1)

Giru Bhai
Giru Bhai

Reputation: 14398


1.To get exception report in google analytics
Use this function in your global file

 public static void sendExceptiontoServer(Context mContext,String title, Exception e){
     try{
         EasyTracker easyTracker = EasyTracker.getInstance(mContext);
         easyTracker.send(MapBuilder.createException(
                 new StandardExceptionParser(mContext, null)
                 .getDescription(title + " : " + Thread.currentThread().getName(), e), false).build());
     }catch(Exception ex){
     }
 }

And call this function from anywhere in your code as

      try{ 
}catch(Exception e){ 
 GlobalFile.sendExceptiontoServer(mContext, "error description :", e); 
}

2. To get crash report
copy-paste this code in Oncreate function of your Application class

 EasyTracker.getInstance(this).set(Fields.SCREEN_NAME, getClass().getSimpleName());

Thread.UncaughtExceptionHandler uncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
if (uncaughtExceptionHandler instanceof ExceptionReporter) {
  ExceptionReporter exceptionReporter = (ExceptionReporter) uncaughtExceptionHandler;
  exceptionReporter.setExceptionParser(new AnalyticsExceptionParser());
}

And create AnalyticsExceptionParser class as

 public class AnalyticsExceptionParser implements ExceptionParser {
            @Override
            public String getDescription(String thread, Throwable throwable) {
           return String.format("Thread: %s, Exception: %s", thread,Log.getStackTraceString(throwable));
               }
           }

Upvotes: 1

Related Questions