6

Automatic activity detection is great - except my MainActivity is a bunch of different fragments with a nav drawer (like Google Play Music or the Play Store). I am using manual screen hitting to track the fragments in that activity.

Therefore, an automatic screen hit for my MainActivity is meaningless and pollutes my stats. Can I exclude my MainActivity from being tracked in this manner?

Reference: https://developers.google.com/analytics/devguides/collection/android/v4/screens#automatic

ZakTaccardi
  • 12,212
  • 15
  • 59
  • 107

1 Answers1

0

Just set enableAutoActivityTracking(false)to the Tracker instance obtained in the activity.

Assuming that you created a getDefaultTracker() method in your Application class as described in the official docs, you can create a parent class for your application activities that can change auto-tracking behavior on demand:

public abstract class ParentActivity extends Activity {

    Tracker mTracker = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getTracker();
    }

    /* Obtains Google Analytics Tracker for this activity */
    Tracker getTracker() {
        if (mTracker == null) {
            AnalyticsApplication application = (AnalyticsApplication) getApplication();
            mTracker = application.getDefaultTracker();
            // Enable or disable auto-tracking for this activity
            mTracker.enableAutoActivityTracking(shouldAutoTrack());
        }
        return mTracker;
    }

    /* Defines whether this activity should enable auto-track or not. Default is true. */
    protected boolean shouldAutoTrack() {
        return true;
    }
}

Your main activity just have to extend ParentActivity and override shouldAutoTrack method to return false:

public class MainActivity extends ParentActivity {

    /* Disable auto-tracking for this activity */
    protected boolean shouldAutoTrack() {
        return false;
    }

}
AlexGuti
  • 3,063
  • 1
  • 27
  • 28