Skip to content

Instantly share code, notes, and snippets.

@tekkies
Last active January 2, 2016 04:59
Show Gist options
  • Save tekkies/8254377 to your computer and use it in GitHub Desktop.
Save tekkies/8254377 to your computer and use it in GitHub Desktop.
Report Android caught exceptions as a Google Analytics events (http://www.google.com/analytics/mobile/). Extracts minimal essential exception data and reports it. It's not reported as an App exception - just an interesting event :)
Replace com.example with your package.
/**
* Extracts minimal essential exception data and reports it as a Google Analytics event
* (Not reported as an App exception :).<br/>
* <br/>
* Reported action: Exception type & message<br/>
* Reported label: File & line no
* <code><pre>
* } catch (Exception e) {
* Analytics.reportCaughtException(getActivity(), e);
* }
* </pre></code>
* @param context
* @param exception
*/
public static void reportCaughtException(Context context, Exception exception) {
String message=exception.getClass().getSimpleName();
if(exception.getMessage() != null) {
message += ":"+exception.getMessage();
}
StackTraceElement[] stackTraceElements = exception.getStackTrace();
String location="";
for(int i=0;i<stackTraceElements.length; i++) {
StackTraceElement stackTraceElement = stackTraceElements[i];
if(stackTraceElement.getClassName().contains("com.example")) {
location += stackTraceElement.getFileName().replace(".java","")+":"+stackTraceElement.getLineNumber();
break;
}
}
EasyTracker easyTracker = EasyTracker.getInstance(context);
easyTracker.send(MapBuilder.createEvent("caught_exception", message, location, 0L).build());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment