获取谷歌分析崩溃报告 [英] Get crash report in google analytics

查看:464
本文介绍了获取谷歌分析崩溃报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想收到有关崩溃报告的通知,由于在我的谷歌分析捕获的异常对我的Andr​​oid应用程序。我跟着<给定的步骤href=\"https://developers.google.com/analytics/devguides/collection/android/v4/exceptions#parsing\">https://developers.google.com/analytics/devguides/collection/android/v4/exceptions#parsing
但我仍然不收到任何崩溃报告。我有一个运行时异常我的应用程序运行时。我增加了code为ga_reportUncaughtException为真:

I want to receive notification regarding crash report due to uncaught exception in my google analytics for my android app. I followed the steps given in https://developers.google.com/analytics/devguides/collection/android/v4/exceptions#parsing but still I dont receive any crash report. I had a runtime exception when my app runs. I added the code for ga_reportUncaughtException as true:

真正

在我的analytics.xml。还有什么我需要为了增加被打到在谷歌Analytics帐户。请帮助!

in my analytics.xml. Is there anything else I need to add in order to get hit in google analytics account. Please help!

推荐答案

有在分析一个开放的问题。我遇到相同的行为,但在真实设备API从10到19。

There is an open issue in Analytics. I'm experiencing the same behavior but on real devices from API 10 to 19.

https://开头code。 google.com/p/analytics-issues/issues/detail?id=443

EDIT1 :移除的问题,只是回答中描述的问题。

EDIT1: Removed question, just to answer the question described.

EDIT2:我试图捕捉和发送使用Analytics ExceptionBuilder例外,但没有奏效。

I tried to capture and send the exceptions using the Analytics ExceptionBuilder, but it didn't work.

它看起来像报告发送(至少LogCat中被示出,该碰撞报告),但它不是由分析处理。

It looks like the report is sent (at least LogCat is showing that the crash is reported), but it is not processed by Analytics.

虽然谷歌答复这个问题,我使用此解决办法。我想这是不是最好的解决方案和code可以改善,但它为我的作品:

While Google replies to the issue, I'm using this workaround. I guess it is not the best solution and the code can be improved, but it works for me:


  1. 我创造了这个之后Analytics(分析)的自定义维度步骤的https:/ /support.google.com/analytics/answer/2709829?hl=en

在我的应用程序,我创建了一个自定义异常处理程序,使用Analytics ExceptionReporter类。当一个异常被抓住了,我得到的堆栈跟踪,并截断为150个字节(其实我只得到堆栈的第一行,并截断为150个字符。我假设1Char = 1字节)。我必须截断它,因为它是最大Lenght发送自定义尺寸的值时,通过分析允许的。
堆栈跟踪存储在一个共享preference而不是被发送。我试图直接发送,但一旦应用程序崩溃这是行不通的。

In my App, I created a custom exception handler, using the Analytics ExceptionReporter class. When an exception is caught, I get the stack trace and truncate it to 150 Bytes (Actually I'm getting only the first line of the stack and truncate it to 150 chars. I'm assuming that 1Char = 1 Byte). I have to truncate it, because it is the Max Lenght allowed by Analytics when sending custom dimensions values. The stack trace is stored in a Shared Preference instead of being sent. I tried to send it directly, but it does not work once the App has crashed.

package com.company.package;

import java.lang.Thread.UncaughtExceptionHandler;

import android.content.Context;

import com.google.android.gms.analytics.ExceptionParser;
import com.google.android.gms.analytics.ExceptionReporter;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;

public class GoogleAnalyticsTracker {

    private static Tracker mTracker;
    private static GoogleAnalytics mGa;
    private Context mContext;

    public GoogleAnalyticsTracker(Context context, int resource) {
        mContext = context;
        mGa = GoogleAnalytics.getInstance(context);
        mTracker = getTracker(resource);

        Thread.setDefaultUncaughtExceptionHandler(new AnalyticsExceptionReporter(mTracker, 
            Thread.getDefaultUncaughtExceptionHandler(), context));
    }

    synchronized Tracker getTracker(int xmlResource) {
        return mGa.newTracker(xmlResource);
    }

    public void sendScreenLabel(String screenLabel) {
        mTracker.setScreenName(screenLabel);
        mTracker.send(new HitBuilders.AppViewBuilder().build());
    }

    public void sendCustomDimension(int index, String value) {
        mTracker.send(new HitBuilders.AppViewBuilder().setCustomDimension(index, value).build());
    }

    private class AnalyticsExceptionReporter extends ExceptionReporter {

        public AnalyticsExceptionReporter(Tracker tracker, UncaughtExceptionHandler originalHandler, Context context) {
            super(tracker, originalHandler, context);
            setExceptionParser(new AnalyticsExceptionParser());
        }

        @Override
        public void uncaughtException(Thread t, Throwable e) {
        String exceptionDescription =  getExceptionParser().getDescription(t.getName(), e);

            //Add code to store the exception stack trace in shared preferences

            super.uncaughtException(t, e);
        }
    }

    private class AnalyticsExceptionParser implements ExceptionParser {

        @Override
        public String getDescription(String arg0, Throwable arg1) {
            StringBuilder exceptionFirsLine = new StringBuilder();
            for (StackTraceElement element : arg1.getStackTrace()) {
                exceptionFirsLine.append(element.toString());
                break;
            }

            //150 Bytes is the maximum allowed by Analytics for custom dimensions values. Assumed that 1 Byte = 1 Character (UTF-8)
            String exceptionDescription = exceptionFirsLine.toString(); 
            if(exceptionDescription.length() > 150) 
                exceptionDescription = exceptionDescription.substring(0, 149);

            return exceptionDescription;
        }
    }
}


  • 在MainActivity时的OnStart(),我检查是否有在共享preferences任何存储堆栈跟踪。如果是这样,我送了自定义维度,并清除共享preference。

  • In the MainActivity when OnStart(), I check if there is any stored stack trace in the shared preferences. If so, I send the custom dimension and clear the shared preference.

    @Override
    protected void onStart() {
        super.onStart();
        String exception = getExceptionFromSharedPreferences(this);
        if(exception != null && !exception.isEmpty()) {
            MainApplication.googleAnalyticsTracker.sendCustomDimension(1, exception);
        } 
        clearExceptionFromSharedPreferences(this);
    }
    


  • 最后,我创建了一个分析自定义报告

  • Finally I created a custom report in Analytics

    修改3:

    我意识到,我只是发送文件名和行号,但不是ExceptionName和我的包异常的起源。我通过添加code还发送信息提高了答案。

    I realized that I was sending only the fileName and lineNumber, but not the ExceptionName and the origin of the Exception in my package. I have improved the answer by adding code to also send that info.

    private class AnalyticsExceptionParser implements ExceptionParser {
    
        @Override
        public String getDescription(String arg0, Throwable arg1) {
            String exceptionDescription = getExceptionInfo(arg1, "", true) + getCauseExceptionInfo(arg1.getCause()); 
    
            //150 Bytes is the maximum allowed by Analytics for custom dimensions values. Assumed that 1 Byte = 1 Character (UTF-8)
            if(exceptionDescription.length() > 150) 
                exceptionDescription = exceptionDescription.substring(0, 150);
    
            return exceptionDescription;
        }
    }
    //#endregion
    
    //#region PRIVATE METHODS
    private String getCauseExceptionInfo(Throwable t) {
        String causeDescription = "";
        while(t != null && causeDescription.isEmpty()) {
            causeDescription = getExceptionInfo(t, "com.myPackageName", false);
            t = t.getCause();
        }
        return causeDescription;
    }
    
    private String getExceptionInfo(Throwable t, String packageName, boolean includeExceptionName) {
        String exceptionName = "";
        String fileName = "";
        String lineNumber = "";
    
        for (StackTraceElement element : t.getStackTrace()) {
            String className = element.getClassName().toString().toLowerCase();
            if(packageName.isEmpty() || (!packageName.isEmpty() && className.contains(packageName))){
                exceptionName = includeExceptionName ? t.toString() : "";
                fileName = element.getFileName();
                lineNumber = String.valueOf(element.getLineNumber());
                return exceptionName + "@" + fileName + ":" + lineNumber;
            }
        }
        return "";
    }
    

    这篇关于获取谷歌分析崩溃报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆