Search code examples
androidandroid-activitygoogle-analyticsgoogle-play-servicesgoogle-analytics-firebase

Google analytic for multiple Android Activities


I'm posting this question because I could't find a clear answer. :)

Im new to Android and currently making an app which has multiple activities. I use google play services for google analytic. By following google dev guide, I managed to get it working. But whenever I create a new Activity(screen) I have to repeat the same codes inside each activity class.

Ex:-Activity1.java

public class Activity1 extends Activity {

HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_activity1);
    setAnalytics();
}

public enum TrackerName {
   APP_TRACKER, // Tracker used only in this app.
   GLOBAL_TRACKER, // Tracker used by all the apps from a company.
   ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a company.
}

synchronized Tracker getTracker(TrackerName trackerId) {
   if (!mTrackers.containsKey(trackerId)) {
      GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
      Tracker tracker = analytics.newTracker(R.xml.global_tracker);
      mTrackers.put(trackerId, tracker);
   }
   return mTrackers.get(trackerId);
}

private void setAnalytics(){
    // Get tracker.
    Tracker t = getTracker(TrackerName.APP_TRACKER);
    // Set screen name.
    //t.setScreenName("Activity 1");
    // Send a screen view.
    t.send(new HitBuilders.AppViewBuilder().build());
}
}

My global_tracker.xml is

<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>

<!-- The screen names that will appear in reports -->
<screenName name="com.test.activity1">Activity1</screenName>
<screenName name="com.test.activity2">Activity2</screenName>

<!--  The following value should be replaced with correct property id. -->
<string name="ga_trackingId">UA-XXXXXXXX-1</string>

This setup works just fine. My question is "Is this the best way to do this?". Is it possible to use another way to minimize the codes in each Activity class? Because everytime I have to repeat the same code. Is it possible to create a common class to hold those common set of codes and use it as an object inside each Activity class?

Any ideas guys? Thank you in advance.


Solution

  • You should put those code inside Application object instead of configuring in each activity.

    Example:

        public class AnalyticsSampleApp extends Application {
    
        // The following line should be changed to include the correct property id.
        private static final String PROPERTY_ID = "UA-XXXXX-Y";
    
        public static int GENERAL_TRACKER = 0;
    
        public enum TrackerName {
            APP_TRACKER, // Tracker used only in this app.
            GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg: roll-up tracking.
            ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a company.
        }
    
        HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
    
        public AnalyticsSampleApp() {
            super();
        }
    
        synchronized Tracker getTracker(TrackerName trackerId) {
            if (!mTrackers.containsKey(trackerId)) {
    
                GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
                analytics.getLogger().setLogLevel(Logger.LogLevel.VERBOSE);
                Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID)
                        : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(
                                R.xml.global_tracker)
                                : analytics.newTracker(R.xml.ecommerce_tracker);
                t.enableAdvertisingIdCollection(true);
                mTrackers.put(trackerId, t);
            }
            return mTrackers.get(trackerId);
        }
    }
    

    Reference:

    Kindly checkout the sample application provided. You may find it within your android sdk directory at following path:

    <android-sdk-directory>/extras/google/google_play_services/analytics/mobileplayground
    

    Documentation:

    Google Analytics v4