Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Analytics w/ Android: Programmatically set ga_reportUncaughtExceptions?

tl;dr

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

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.

<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.

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?

like image 272
loeschg Avatar asked Apr 03 '14 21:04

loeschg


4 Answers

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

mGATracker is your own tracker.

mGATracker.enableExceptionReporting(true);

Hope it helps, thank you.

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

like image 77
Sonny Ng Avatar answered Nov 19 '22 21:11

Sonny Ng


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.

like image 36
Avi Avatar answered Nov 19 '22 20:11

Avi


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.

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)

like image 2
XtopherSD Avatar answered Nov 19 '22 22:11

XtopherSD


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

like image 1
bladerunner Avatar answered Nov 19 '22 20:11

bladerunner