Disable auto-activity tracking in Firebase

ぐ巨炮叔叔 提交于 2021-02-04 11:52:27

问题


I'm wondering, are there any way to disable analytics auto activity tracking? I have view hierarchy based on fragments and there are few cases:

  1. Activity that have one fragment always.
  2. Activity that can have different fragments as root.
  3. Activity with root fragment, that contains ViewPager with other fragments.

I use such a code in fragments from Firebase docs to track custom screens:

mFirebaseAnalytics.setCurrentScreen(getActivity(), "some_fragment_1", null);

In first case, I want to track only root fragment. In second case, I want to track only each fragment that becomes root. In third case, I want to track only each fragment that becomes visible in ViewPager.

And, the problem is, that I don't want to track Activities at all, but unfortunately, Firebase do it on its own - as a result of that, my statistics looks weird, like:

SomeActivity 50%

some_fragment_1 30%

some_fragment_2 20%

I dont't need activity in this statistics, because, fragment statistics already includes it.

So, is there any way to disable activity tracking?


回答1:


Now it's possible with new API to manually track screens.

Can disable auto-tracking

On iOS, set FirebaseAutomaticScreenReportingEnabled to NO in your info.plist. On Android, set google_analytics_automatic_screen_reporting_enabled to false in your manifest.

Manual Tracking

iOS

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    // After enough time has passed to make this screen view significant.
    Analytics.logEvent(AnalyticsEventScreenView, parameters: [
        AnalyticsParameterScreenName: screenName!,
        AnalyticsParameterScreenClass: screenClass!,
        MyAppAnalyticsParameterFitnessCategory: category!
    ])
}

Android

@Override
public void onResume() {
    super.onResume();

    // After enough time has passed to make this screen view significant.
    Bundle bundle = new Bundle();
    bundle.putString(FirebaseAnalytics.Param.SCREEN_NAME, screenName);
    bundle.putString(FirebaseAnalytics.Param.SCREEN_CLASS, screenClass);
    bundle.putString(MyAppAnalyticsConstants.Param.TOPIC, topic);
    mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle);
}

https://firebase.googleblog.com/2020/08/google-analytics-manual-screen-view.html




回答2:


I know that this is rather a hack, but seems to be working with latest firebase analytics. The idea is that, Firebase Analytics uses registerActivityLifecycleCallbacks() internally on each activity declared in the manifest, so disabling that way disables auto-activity reporting.

Place that code in each root Activity you are using.

@Override
public void registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback) {
    // com.google.android.gms.measurement.internal.zzfl for firebase-core:17.1.0, play-services-measurement-impl:17.1.0
    if (!callback.getClass().getName().startsWith("com.google.android.gms.measurement.")){
            super.registerActivityLifecycleCallbacks(callback);
    }
}

Credits for finding that goes to @liudongmiao on https://github.com/firebase/quickstart-android/issues/370.




回答3:


To disable screen auto-tracking in Firebase Analytics 17.5.0, I had to catch callback registrations in my Application class, not in individual activities, and the callback class name has changed again:

    @Override
    public void registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback) {
        if (!callback.getClass().getName().startsWith("com.google.android.gms.internal.measurement.")){
            super.registerActivityLifecycleCallbacks(callback);
        }
    }

Unfortunately, contrary to what Google's documentation states (https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics.Event#SCREEN_VIEW), I wasn't able to manually tag screen views by logging the event FirebaseAnalytics.Event.SCREEN_VIEW. For now I've resorted to logging screen views with a custom event.



来源:https://stackoverflow.com/questions/51936945/disable-auto-activity-tracking-in-firebase

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