如何使用firebase分析跟踪android片段

Zhe*_*Liu 31 android android-fragments firebase firebase-analytics

在我的Android应用程序中,我有一个Activity,它有3个或4个片段,可以根据一些用户或服务器事件按顺序连接.

我想跟踪所有这些片段作为firebase中的屏幕.

理想情况下,如果可能的话,是否有一个我可以在片段的onCreate中调用的API,并告诉firebase用户当前是在fragment1,fragment2还是fragment3?

Art*_*aev 51

设置当前屏幕有一种特殊方法 - setCurrentScreen

我用它如下

mFirebaseAnalytics.setCurrentScreen(this, fragment.getClass().getSimpleName(), fragment.getClass().getSimpleName());
Run Code Online (Sandbox Code Playgroud)

调用该方法后,LogCat中将显示以下消息

记录事件(FE):screen_view(_vs),Bundle [{firebase_event_origin(_o)= auto,firebase_previous_class(_pc)= HomeFragment,firebase_previous_id(_ pi)= 4121566113087629222,firebase_previous_screen(_pn)= HomeFragment,firebase_screen_class(_sc)= StatisticsFragment,firebase_screen_id (_si)= 4121566113087629223,firebase_screen(_sn)= StatisticsFragment}]

自动活动跟踪中会显示以下事件:

记录事件(FE):screen_view(_vs),Bundle [{firebase_event_origin(_o)= auto,firebase_previous_class(_pc)= StatisticsFragment,firebase_previous_id(_ pi)= 4121566113087629223,firebase_previous_screen(_pn)= StatisticsFragment,firebase_screen_class(_sc)= LoginActivity,firebase_screen_id (_si)= 4121566113087629224}]

如你所见,它们几乎相同,所以setCurrentScreen工作正常.

我只能在第二天在Firebase Console中看到这些类.Firebase是正常的 - 处理此类数据需要时间.

Firebase控制台

  • 你认为最好的地方是这个方法吗?在`onStart`,`onResume`或其他方法?(在一个片段中) (2认同)
  • 我认为`onResume`是最好的选择.当你从另一个片段返回片段时调用它. (2认同)
  • `setCurrentScreen` 已弃用:( (2认同)

Guf*_*hid 12

在这里为 Artem Mostyaev 的答案添加更多见解。GA/Firebase 面板在 DEV 版本中反映了类名,但在 PROD 版本中没有。这里的主要罪魁祸首是

fragment.getClass().getSimpleName()
Run Code Online (Sandbox Code Playgroud)

混淆了 prod 中的片段名称。所以 GA/Firebase 将类名显示为 (a,b,ah, etc)

在其他情况下使用 getSimpleName() 也很危险。

更多文献:https : //medium.com/@elye.project/the-danger-of-using-class-getsimplename-as-tag-for-fragment-5cdf3a35bfe2

Progaurd 规则

-keepnames class com.somepackage.yourclass 
Run Code Online (Sandbox Code Playgroud)


Mos*_*ius 12

由于setCurrentScreen弃用,您可以firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle)改用。

有一个博客张贴在这里,介绍更多关于手动跟踪显示屏。

这是一个例子:

private fun setCurrentScreen(screenName: String) = firebaseAnalytics?.run {
    val bundle = Bundle()
    bundle.putString(FirebaseAnalytics.Param.SCREEN_NAME, screenName)
    bundle.putString(FirebaseAnalytics.Param.SCREEN_CLASS, this@BaseFragment.javaClass.simpleName)
    logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle)
}
Run Code Online (Sandbox Code Playgroud)

此外,如果您想自动跟踪屏幕,您可以在您的BaseFragment生命周期方法之一中调用此函数,例如onResume. 请记住,某些片段可能不必更改当前屏幕,例如在 a 中创建的那些片段ViewPager,因此我声明了 anopen val您可以override更改默认行为。

这是代码BaseFragment

protected open val trackScreenView: Boolean = true

override fun onResume() {
    super.onResume()

    if (trackScreenView) setCurrentScreen(this.javaClass.simpleName)
}
Run Code Online (Sandbox Code Playgroud)

您可以通过在目标中覆盖它来禁用它Fragment

override val trackScreenView: Boolean = false
Run Code Online (Sandbox Code Playgroud)

顺便说一句,如果您使用的是NavigationUIComponent,目前还没有用于跟踪屏幕的自动解决方案,它仅跟踪您拥有的单个活动,因此您可以通过将其meta-data放在您的应用清单中来防止 Firebase 自动屏幕报告:

<application
    android:name=".App"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    
    <!-- .... -->
    
    <meta-data
        android:name="google_analytics_automatic_screen_reporting_enabled"
        android:value="false" />
</application>
Run Code Online (Sandbox Code Playgroud)


Rah*_*ale 5

对于使用NavigationUI 的项目,您可以使用侦听器NavController.OnDestinationChangedListener

里面 onCreate()

override fun onCreate() {
      super.onCreate()
      .
      .
      .
      .
      navController = Navigation.findNavController(context!!, R.id.nav_host_fragment)
      navController?.addOnDestinationChangedListener(listener)
}
Run Code Online (Sandbox Code Playgroud)

在 3 个侦听器函数参数中,

  • controller 获取类名很有用
  • destination 可用于获取通过属性在 nav_host_fragment 中找到的目的地的布局 xml 名称字符串 android:label
private val listener = NavController.OnDestinationChangedListener { controller, destination, arguments ->
      
      val bundle = Bundle()
      val currentFragmentClassName = (controller.currentDestination as FragmentNavigator.Destination).className
      bundle.putString(FirebaseAnalytics.Param.SCREEN_NAME, destination.label.toString())
      bundle.putString(FirebaseAnalytics.Param.SCREEN_CLASS, currentFragmentClassName)
      FirebaseAnalytics.getInstance(requireContext()).logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle)
      
}
Run Code Online (Sandbox Code Playgroud)

不要忘记清理

override fun onDestroy() {
      super.onDestroy()
      navController?.removeOnDestinationChangedListener(listener)
}
Run Code Online (Sandbox Code Playgroud)