Streamlining Google Analytics Integration in Android with Activity Lifecycle Callbacks
I was working on integrating Google Analytics (GA) into an existing Android application.
If you want to track activities in GA, the conventional approach requires you to initiate and stop reports within your activities’ onStart
and onStop
lifecycle events. This manual process must be repeated in every activity.
I dislike this approach, as it leads to repetitive boilerplate code, which I always try to avoid.
Fortunately, there’s a more elegant solution.
You can implement ActivityLifecycleCallbacks
. By overriding its onActivityStopped
and onActivityStarted
methods, you can centralize the management of activity tracking for all your activities. Here’s how to implement it:
public class CustomActivityLifeCycleListener implements Application.ActivityLifecycleCallbacks {
@Override
public void onActivityStopped(Activity activity) {
GoogleAnalytics.getInstance(activity).reportActivityStop(activity);
}
@Override
public void onActivityStarted(Activity activity) {
GoogleAnalytics.getInstance(activity).reportActivityStart(activity);
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
// Not used for GA tracking in this context
}
@Override
public void onActivityResumed(Activity activity) {
// Not used for GA tracking in this context
}
@Override
public void onActivityPaused(Activity activity) {
// Not used for GA tracking in this context
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
// Not used for GA tracking in this context
}
@Override
public void onActivityDestroyed(Activity activity) {
// Not used for GA tracking in this context
}
}
Next, you need to register this lifecycle listener in your application class (by extending Application
) and initialize your GA tracker.
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new CustomActivityLifeCycleListener());
// R.xml.global_tracker is my tracker configuration. You can also initialize the tracker
// by passing your GA property ID in the String constructor.
GoogleAnalytics.getInstance(this).newTracker(R.xml.global_tracker);
}
}
You can also save this tracker instance in your application class, as you will need it to send events or perform other tracking operations throughout your app.
One crucial point: you must register your tracker resource in your AndroidManifest.xml
file; otherwise, your GA configuration will not work. It took me about three hours to figure this out!
Additionally, ensure you update your Google Play Services library and install the Google Analytics libraries via your Android SDK Manager.
Enjoy Reading This Article?
Here are some more articles you might like to read next: