谷歌分析w/ Android:以编程方式设置ga_reportuntexceptions ?

时间:2023-02-06 15:20:23

tl;dr

Is there a way to programatically enable reportUncaughtExceptions for Google Analytics (v4) without using the xml config in Android?

有没有一种方法可以在不使用Android中的xml配置的情况下,为谷歌Analytics (v4)程序化地启用reportunctexceptions ?

Longer explanation

I'm using Google Analytics v4 in an Android app, and I need a way to set two different tracking ids by build flavor. I was using a general global_tracker.xml configuration (see below), though I need a way to "dynamically inject" the tracking id based on flavor.

我正在Android应用程序中使用谷歌分析v4,我需要一种方法来根据build flavor设置两个不同的跟踪id。我使用的是通用的global_tracker。xml配置(参见下面),尽管我需要一种方法来“动态注入”基于味道的跟踪id。

<resources xmlns:tools="http://schemas.android.com/tools"
           tools:ignore="TypographyDashes">
    <integer name="ga_sessionTimeout">300</integer>
    <bool name="ga_autoActivityTracking">true</bool>
    <bool name="ga_reportUncaughtExceptions">true</bool>

    <!--  The following value should be replaced with correct property id. -->
    <string name="ga_trackingId">UA-xxxxxx-xx</string>
</resources>

In order to avoid having duplicate xml configs in the build flavor source folders, I initialize a tracker directly using the trackingId and set the attributes programatically.

为了避免在build flavor源文件夹中有重复的xml configs,我使用trackingId直接初始化一个跟踪器,并编程地设置属性。

mGATracker = analytics.newTracker(R.string.ga_code); // this is dynamic depending on flavor
mGATracker.setSessionTimeout(300);
mGATracker.enableAutoActivityTracking(true);

Is there a way to enable reportUncaughtExceptions without using the xml config?

是否有一种方法可以在不使用xml配置的情况下启用reportUncaughtExceptions ?

4 个解决方案

#1


4  

No There is no way to do it in the current version of the APIs. Thanks for pointing it out though. We'll look into it and possibly add it in one of the upcoming versions of the sdk.

不,在当前版本的api中没有办法做到这一点。谢谢你指出来。我们将研究它,并可能将它添加到即将发布的sdk版本中。

#2


7  

If i have not misunderstand your question, the solution is below:

如果我没有误解你的问题,解决方案如下:

mGATracker is your own tracker.

mGATracker是你自己的追踪者。

mGATracker.enableExceptionReporting(true);

Hope it helps, thank you.

希望它能有所帮助,谢谢。

Reference: https://developers.google.com/android/reference/com/google/android/gms/analytics/Tracker#enableExceptionReporting(boolean)

参考:https://developers.google.com/android/reference/com/google/android/gms/analytics/Tracker enableExceptionReporting(布尔)

#3


2  

I think the best you'll be able to do programmatically is have two different tracker xml configuration files, both with the same ga_trackingId but different values for ga_reportUncaughtExceptions. Use the GoogleAnalytics.newTracker() method with the xml resource for the correct configuration file instead of the trackingId.

我认为最好的编程方式是有两个不同的跟踪器xml配置文件,它们都具有相同的ga_trackingId,但ga_reportaughtexceptions的值不同。使用带有xml资源的GoogleAnalytics.newTracker()方法获得正确的配置文件,而不是trackingId。

if (buildFlavor == 1)
    mGATracker = analytics.newTracker(R.xml.tracker_config_1)
else
    mGATracker = analytics.newTracker(R.xml.tracker_config_2)

See http://developer.android.com/reference/com/google/android/gms/analytics/GoogleAnalytics.html#newTracker(int)

看到http://developer.android.com/reference/com/google/android/gms/analytics/GoogleAnalytics.html newTracker(int)

#4


1  

You can set the Analytics exception handler programaticly.

您可以程序化地设置分析异常处理程序。

UncaughtExceptionHandler myHandler = new ExceptionReporter(
    myTracker,                                        // Currently used Tracker.
    Thread.getDefaultUncaughtExceptionHandler(),      // Current default uncaught exception handler.
    context);                                         // Context of the application.

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

See more at https://developer.android.com/reference/com/google/android/gms/analytics/ExceptionReporter.html

多见于https://developer.android.com/reference/com/google/android/gms/analytics/ExceptionReporter.html

#1


4  

No There is no way to do it in the current version of the APIs. Thanks for pointing it out though. We'll look into it and possibly add it in one of the upcoming versions of the sdk.

不,在当前版本的api中没有办法做到这一点。谢谢你指出来。我们将研究它,并可能将它添加到即将发布的sdk版本中。

#2


7  

If i have not misunderstand your question, the solution is below:

如果我没有误解你的问题,解决方案如下:

mGATracker is your own tracker.

mGATracker是你自己的追踪者。

mGATracker.enableExceptionReporting(true);

Hope it helps, thank you.

希望它能有所帮助,谢谢。

Reference: https://developers.google.com/android/reference/com/google/android/gms/analytics/Tracker#enableExceptionReporting(boolean)

参考:https://developers.google.com/android/reference/com/google/android/gms/analytics/Tracker enableExceptionReporting(布尔)

#3


2  

I think the best you'll be able to do programmatically is have two different tracker xml configuration files, both with the same ga_trackingId but different values for ga_reportUncaughtExceptions. Use the GoogleAnalytics.newTracker() method with the xml resource for the correct configuration file instead of the trackingId.

我认为最好的编程方式是有两个不同的跟踪器xml配置文件,它们都具有相同的ga_trackingId,但ga_reportaughtexceptions的值不同。使用带有xml资源的GoogleAnalytics.newTracker()方法获得正确的配置文件,而不是trackingId。

if (buildFlavor == 1)
    mGATracker = analytics.newTracker(R.xml.tracker_config_1)
else
    mGATracker = analytics.newTracker(R.xml.tracker_config_2)

See http://developer.android.com/reference/com/google/android/gms/analytics/GoogleAnalytics.html#newTracker(int)

看到http://developer.android.com/reference/com/google/android/gms/analytics/GoogleAnalytics.html newTracker(int)

#4


1  

You can set the Analytics exception handler programaticly.

您可以程序化地设置分析异常处理程序。

UncaughtExceptionHandler myHandler = new ExceptionReporter(
    myTracker,                                        // Currently used Tracker.
    Thread.getDefaultUncaughtExceptionHandler(),      // Current default uncaught exception handler.
    context);                                         // Context of the application.

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

See more at https://developer.android.com/reference/com/google/android/gms/analytics/ExceptionReporter.html

多见于https://developer.android.com/reference/com/google/android/gms/analytics/ExceptionReporter.html