简体   繁体   中英

Full stack traces in Google Analytics v4 via Tag-Manager

I'm a little confused about crash tracking with Analytics . AFIK it is only possible to store in Analytics strings which are no longer then 200bytes (per property). So it is not possible to see full stack traces, since they are commonly much longer than 200 chars.

However with the Analytics v2 API this is possible with the EasyTracker, as described here . There are also some half-duplicated which use the ga_reportUncaughtExceptions property like this one: Google Analytics crash report only shows first line of stack trace or Exception stack trace lost in Google Analytics v2 for Android?

Since I use the Tag-Manager I cannot use those solutions, I already figured out how I can correctly track the crash for the first 200bytes with the Tag-Manager, but what is about the full stack trace?

I also use ACRA to provide the user a way to contact us directly (with the stack trace as attachment), so the direct connection to the Google crash reporter is stopped and the Google Play Console shows no crashes.

It is possible to send full stack trace in Google Analytics v4 for all the uncaught exceptions in Android.

Set the automatic activity tracking to true in your Application class.

    // Enable automatic activity tracking for your app
    tracker.enableAutoActivityTracking(true);

Further override the getDescription method of the StandardExceptionParser class. The Throwable contains array of stack trace elements which if converted to string format will be your stack trace as we see in Logs.

    public class AnalyticsExceptionParser extends StandardExceptionParser {

    public AnalyticsExceptionParser(Context context, Collection<String> additionalPackages) {
        super(context, additionalPackages);
    }

    @Override
    protected String getDescription(Throwable cause,
        StackTraceElement element, String threadName) {

        StringBuilder descriptionBuilder = new StringBuilder();
        final Writer writer = new StringWriter();
        final PrintWriter printWriter = new PrintWriter(writer);
        cause.printStackTrace(printWriter);
        descriptionBuilder.append(writer.toString());
        printWriter.close();

        return descriptionBuilder.toString();
    }
}

Finally, provide your AnalyticsExceptionParser to an instance of ExceptionReporter .

    public class MyApplication extends Application {

        @Override
        public void onCreate() {
        ...

            ExceptionReporter reporter = new ExceptionReporter(getTracker(), Thread.getDefaultUncaughtExceptionHandler(), this);
            reporter.setExceptionParser(new AnalyticsExceptionParser(this, packages));
            Thread.setDefaultUncaughtExceptionHandler(reporter);
        }
    }

Thats it. See your crash reports on your GA console in Behavior > Crashes and Exceptions .

You can override the ExceptionParser that is used by the main thread to tweak it to report full stacktrace:

final Thread.UncaughtExceptionHandler uncaughtExceptionHandler =
        Thread.getDefaultUncaughtExceptionHandler();
if (uncaughtExceptionHandler instanceof ExceptionReporter) {
    ExceptionReporter exceptionReporter = (ExceptionReporter) uncaughtExceptionHandler;
    exceptionReporter.setExceptionParser(new ExceptionParser() {
        @Override
        public String getDescription(String s, Throwable throwable) {
            return "Thread: " + s + ", Stacktrace: " + ExceptionUtils.getStackTrace(throwable);
        }
    });
}

Put this where you instantiate your Google Analytics instance. The ExceptionUtils class I use is from apache's common-lang3 library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM