Is there a way to programatically enable reportUncaughtExceptions for Google Analytics (v4) without using the xml config in Android?
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?
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)
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.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With