Google Analytics Automatic Activity Detection - can you exclude a single activity from this?

谁都会走 提交于 2019-12-21 04:55:09

问题


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


回答1:


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;
    }

}


来源:https://stackoverflow.com/questions/31279771/google-analytics-automatic-activity-detection-can-you-exclude-a-single-activit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!