Firebase events in Instant Apps

I decided recently to use Firebase to see how many users play Paper Football in Instant App and how many use the Installed App.

A few notes, not covered by article

  1. To add Firebase in your Android project is straight forward if you follow the official documentation.
  2. To log events in Firebase is also thoroughly described in the official documentation.
  3. To differentiate in code between the Installed App and the Instant App you can use:
import com.google.android.instantapps.InstantApps;
...
if (InstantApps.isInstantApp(context)) {
// do something in Instant App
} else {
// do something else in Installed App
}
...

It was easy to assume that since I was already using Firebase in the Installed App would work seamlessly in the Instant App. After several tries I didn’t see any event coming.


There are a few things to check if you don’t see events in Firebase:

  • the length of the event name: all Firebase event names are limited to 40 chars and if you exceed this, the event will simply be discarded.
  • the google-services.json file: this should be downloaded from your Project Settings in Firebase console and added to project’s module folder, typically app/. I couldn’t find in the documentation where to add it when the project has multiple modules and I assumed it should be the base one. This didn’t seem to work for the Instant App.
  • assemble your Instant App by running your usual gradlew command and check in the terminal if the process{build_type}GoogleServices task has run.

Debugging

There are 2 ways to check Firebase events:

I. Using Logcat in Android Studio — this way you’ll see events are sent to Firebase. Here you can also get more information if your project is not properly configured to use Firebase.

Install the debug version of the app on a USB connected device / emulator and run the below commands in terminal.

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

Start using the app and check Logcat on Verbose mode where you should see what happens. You can filter the logs by adding firebase as keyword.

II. Using the DebugView in Firebase console — this will ensure your events reach Firebase. Here you can check either the debug or release version of your app.

If you use an emulator, make sure the time on device is the current time! Otherwise you’ll not see the events in real-time in DebugView.

Install the debug / release version of the app on a device / emulator, run the below command in terminal.

adb shell setprop debug.firebase.analytics.app <package_name>

Start using the app and see the events coming up in near real-time.

Instant App problem

When debugging Firebase events in Instant App using Android Studio and Logcat (first method described in the previous section) the message was pretty clear:

Missing google_app_id. Firebase Analytics disabled.

The value of google_app_id is the identifier that links your application project to Firebase. It can be found in Firebase console in Project Settings under the name App ID or in the google-services.json file under the name mobilesdk_app_id.

When adding Firebase SDK or other Google APIs to your project, one step is to add the google-services plugin. Its job, amongst others, is to add this google_app_id to the generated values.xml file and package it in the apk. This was initially not happening for my Instant App.

Fixing this issue

I found so far two ways of solving this, although I’m not 100% happy with any of them.

I. Manually add google_app_id to the strings.xml file — this is also suggested here

II. Have the below configuration

  • add google-services.json to all modules. For the application and feature this can be placed either in the root of the module, or in the build types folders. For the instant app module I found it works only if it’s added in the build types folders (i.e src/debug and src/release).
  • add this to the end of all build.gradle files in all modules
// THIS HAS TO BE AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'

Conclusion

  1. If you want to send Firebase events from your Instant App, make sure you check them by enabling the Firebase debug mode as described in the Debugging section above.
  2. If you manually add google_app_id to your project as described in Instant App problem section above, make sure you add the correct values if you have multiple build types / flavours defined in your Android Project and in your Firebase project.

In my case I have 2 apps declared in Firebase, one for debug and one for release builds. The App ID is the value needed, and is different for each app defined.

3. If you choose to place the google-services.json file in all modules, make sure you place it in the correct folder and apply the google services plugin in all build.gradle files at the end.