Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to track android fragments using firebase analytics

In my android application, I have an Activity that has 3 or 4 fragments that can be attached in sequence based on some user or server events.

I would like to track all these fragments as screens in firebase.

So ideally, if possible, is there an API i can invoke in the onCreate of the fragments, and tell firebase that user is currently in fragment1, fragment2 or fragment3?

like image 559
Zhen Liu Avatar asked Jul 19 '17 21:07

Zhen Liu


People also ask

What can you track with Firebase Analytics?

This dashboard provides detailed insights about your data — from summary data such as active users and demographics, to more detailed data such as identifying your most purchased items.

How do I see all devices in Firebase Analytics?

You can see the device list in Firebase Analytics. In dashboard -> device model click in view device model. You can see list of all the devices with details.

Is Firebase Analytics same as Google Analytics?

Google analytics also supported mobile analytics though android and IOS sdk's which sent Screen hits to google analytics. There was a difference between Mobile and web google analytics accounts. Firebase is a platform developed by Google for creating mobile and web applications.


2 Answers

UPDATE

Since the setCurrentScreen is deprecated, you can use logEvent method

Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.SCREEN_NAME, fragment.getClass().getSimpleName());
bundle.putString(FirebaseAnalytics.Param.SCREEN_CLASS, fragment.getClass().getSimpleName());
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle);

I've used the following adb commands to check if all is working fine.

adb shell setprop log.tag.FA VERBOSE
adb shell setprop log.tag.FA-SVC VERBOSE
adb logcat -v time -s FA FA-SVC

Once you do that you'll see screen_view events in the logcat. Like this one:

10-15 13:14:13.744 V/FA-SVC (20323): Logging event: origin=app,name=screen_view(_vs),params=Bundle[{ga_event_origin(_o)=app, engagement_time_msec(_et)=31600, ga_previous_class(_pc)=ContentsFragment, ga_previous_id(_pi)=8077407744361472421, ga_previous_screen(_pn)=ContentsFragment, ga_screen_class(_sc)=TestFragment, ga_screen_id(_si)=8077407744361472423, ga_screen(_sn)=TestFragment}]

Previous answer

There is a special method to set a current screen - setCurrentScreen

I used it as follows

mFirebaseAnalytics.setCurrentScreen(this, fragment.getClass().getSimpleName(), fragment.getClass().getSimpleName());

Once the method is called, the following message appears in the LogCat

Logging event (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}]

The following event appears on auto activity tracking:

Logging event (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}]

As you see, they are almost the same, so setCurrentScreen is working.

I'm able to see those classes in Firebase Console only on the next day. It is normal for Firebase - it takes time to process such amounts of data.

Firebase Console

like image 178
Artem Mostyaev Avatar answered Oct 17 '22 04:10

Artem Mostyaev


Since setCurrentScreen is deprecated you can use firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle) instead.

there is a blog post here that explains more about tracking screens manually.

here is an example:

private fun setCurrentScreen(screenName: String) = firebaseAnalytics?.run {
    val bundle = Bundle()
    bundle.putString(FirebaseAnalytics.Param.SCREEN_NAME, screenName)
    bundle.putString(FirebaseAnalytics.Param.SCREEN_CLASS, [email protected])
    logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle)
}

In addition, if you want to track screens automatically, you can call this function in one of your BaseFragment lifecycle methods like onResume. just keep in mind that some of the fragments may not have to change the current screen, like the ones which are being created in a ViewPager, so I have declared an open val which you can override in order to change the default behavior.

here is the code in BaseFragment:

protected open val trackScreenView: Boolean = true

override fun onResume() {
    super.onResume()

    if (trackScreenView) setCurrentScreen(this.javaClass.simpleName)
}

and you can disable it by overriding it in your target Fragment:

override val trackScreenView: Boolean = false

By the way, if you are using NavigationUI Component, currently there is no automatic solution for tracking screens, and it only track the single activity you have, so you can prevent firebase automatic screen reporting by putting this meta-data in your app manifest:

<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>
like image 13
Mosius Avatar answered Oct 17 '22 03:10

Mosius