17

Previously, to track screen view in Google Analytics, I'm using the following code snippet. It is very convenient, as I need not to pass Activity and Context around. To use it, I just need to call Utils.trackGAView("ShareDialogFragment"); any where in the code.

Google Analytics

public static Tracker getTracker() {
    if (false == isGooglePlayServicesAvailable()) {
        return null;
    }

    if (tracker == null) {
        GoogleAnalytics analytics = GoogleAnalytics.getInstance(JStockApplication.instance());
        tracker = analytics.newTracker(R.xml.app_tracker);
    }
    return tracker;
}

public static void trackGAView(String view) {
    Tracker tracker = Utils.getTracker();
    if (tracker == null) {
        return;
    }
    tracker.setScreenName(view);
    tracker.send(new HitBuilders.ScreenViewBuilder().build());
}

However, in Firebase, it is not really that convinient, as it requires an Activity - https://firebase.google.com/docs/analytics/screenviews

Not every place in the code we have access to the Activity object.

Firebase

mFirebaseAnalytics.setCurrentScreen(activity, screenName, null /* class override */);

Is there any good technique, which I can keep track screen view in Firebase, without the need of passing around Activity?

KENdi
  • 7,576
  • 2
  • 16
  • 31
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

0 Answers0