Updated 24 September 2023
This is chapter-2 of “how to track your android application by GAv4″.
In this chapter,
I will show you how to track exception and crashes by GAv4.
Exception: An exception is a computational problem that occurs when any program executes. When an Exception occurs the normal flow of the program is an interrupt and the Application terminates abnormally.
Application Crashes: A Application crash is when a program of application stops functioning properly. Often it will exit the affected program after encountering this type of error.
If you are an app developer obviously your app will be used by many people, from many states and by many countries. And by some reason, the app crashes or got an exception then how do you know. And this is the major issue to track our app that when to get exception/crashes and wherein entire app.
So, For resolving that problem google analytics give us the method for tracking exceptions/crashes of your application.
By analytics, any Exceptions can also be tracked very easily. It permits us to track all the known exception which we tried to catch using try & catch block.
1 2 3 4 5 6 7 8 9 10 |
try { // some Exception occurs } catch (Exception e) { // Tracking exception MobikulApplication.getInstance().trackException(e); Log.e(TAG, "Exception: " + e.getMessage()); } |
and in your application class use this method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class MobikulApplication extends Application { private Tracker mTracker; synchronized public Tracker getDefaultTracker() { if (mTracker == null) { GoogleAnalytics analytics = GoogleAnalytics.getInstance(this); // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG mTracker = analytics.newTracker(R.xml.global_tracker); } return mTracker; } public void trackException(Exception e) { if (e != null) { mTracker.send(new HitBuilders.ExceptionBuilder() .setDescription( new StandardExceptionParser(this, null) .getDescription(Thread.currentThread().getName(), e)) .setFatal(false) .build() ); } } } |
App crash recorded automatically by setting ga_reportUncaughtExceptions to true in app_tracker.xml.
Actually, we have to make our own app_tracker.xml file and replace it to the default global_tracker.xml
This is auto-generated XML file by analytics.
global_tracker.xml
1 2 3 4 |
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="ga_trackingId">UA-XXXXXXXX-X</string> </resources> |
to override default tracker file
make our tracker XML file in app -> res -> xml package
app_tracker.xml
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="utf-8"?> <resources> <!--Replace this trackingId with your tracking ID--> <string name="ga_trackingId">UA-XXXXXXXX-X</string> <!--Enable automatic activity tracking--> <bool name="ga_autoActivityTracking">true</bool> <!--Enable automatic exception tracking--> <bool name="ga_reportUncaughtExceptions">true</bool> </resources> |
ga_autoActivityTracking: If true
, views (Activities) will be tracked automatically.
ga_reportUncaughtExceptions: Automatically track an Exception each time an uncaught exception is thrown in your application.
For more details about google parameters: goto the Google Analytics SDK v4 for Android – Parameters
And on the place of global_tracker.xml
1 2 3 4 5 |
if (mTracker == null) { GoogleAnalytics analytics = GoogleAnalytics.getInstance(this); // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG mTracker = analytics.newTracker(R.xml.global_tracker); } |
write our app_tracker.xml file
1 2 3 4 5 |
if (mTracker == null) { GoogleAnalytics analytics = GoogleAnalytics.getInstance(this); // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG mTracker = analytics.newTracker(R.xml.app_tracker); } |
App crash will be recorded and will be shown on Google Analytics dashboard.
Goto Behaviour -> Crashes and Exceptions section on the dashboard.
Both the Exception and app crash report normally takes 1 day to reflect on Google Analytics dashboard.
In above snapshot, I have intensely crashed my application in MainActivity.
And Analytics dashboard shows the report of crashes and exception.
Sources:
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
3 comments