Add Google Analytics for Firebase to your instant app

Tracking the success of an app, instant or installed, is important to each developer. Several analytics libraries are compatible with Google Play Instant, including Fabric Answers, Localytics, and Mixpanel.

If your current analytics solution isn't listed or if you find that it doesn't work with Google Play Instant, consider using Google Analytics for Firebase as your telemetry solution. This page describes how to set up Google Analytics for Firebase in an instant app project.

Adding Google Analytics for Firebase to an instant app project

  1. Add the Firebase SDK to your app by following the instructions described in the Getting started guide for Google Analytics for Firebase.
  2. Use the latest version of the google-services plugin.
  3. Place the google-services.json file in each module.
  4. Add the following line to each module's build.gradle file:

    Groovy

    // android { ... }
    // dependencies { ... }
    plugins {
        id 'com.google.gms.google-services'
    }
    

    Kotlin

    // android { ... }
    // dependencies { ... }
    plugins {
        id("com.google.gms.google-services")
    }
    

Once you have added Google Analytics for Firebase to your instant app project, you can use the Google Analytics for Firebase APIs as you might in an installable app project.

For more information about how to use the Google Analytics for Firebase APIs, see the getting started documentation for Google Analytics for Firebase.

Differentiating between installed and instant app data

Because both your installed and your instant app share a package name, you may want to differentiate the events and data collected from each. To differentiate your instant and installed apps in Analytics, set a app_type user property, with the value "instant" for the instant app and "installed" for the installed app.

The following code snippet shows an activity that gets an Analytics instance and then sets a user property. Notice that the code uses PackageManagerCompat.isInstantApp() in the onCreate(android.os.Bundle) method to determine the app's context.

Kotlin

val STATUS_INSTALLED = "installed"
val STATUS_INSTANT = "instant"
val ANALYTICS_USER_PROP = "app_type"

private lateinit var firebaseAnalytics: FirebaseAnalytics

protected fun onCreate(savedInstanceState: Bundle?) {
    ...

    firebaseAnalytics = FirebaseAnalytics.getInstance(this)

    // Determine the current app context, either installed or instant, then
    // set the corresponding user property for Google Analytics.
    if (InstantApps.getPackageManagerCompat(this).isInstantApp()) {
        firebaseAnalytics.setUserProperty(ANALYTICS_USER_PROP, STATUS_INSTANT)
    } else {
        firebaseAnalytics.setUserProperty(ANALYTICS_USER_PROP, STATUS_INSTALLED)
    }
}

Java

final String STATUS_INSTALLED = "installed";
final String STATUS_INSTANT = "instant";
final String ANALYTICS_USER_PROP = "app_type";

private FirebaseAnalytics firebaseAnalytics;

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...

    firebaseAnalytics = FirebaseAnalytics.getInstance(this);

    // Determine the current app context, either installed or instant, then
    // set the corresponding user property for Google Analytics.
    if (InstantApps.getPackageManagerCompat(this).isInstantApp()) {
        firebaseAnalytics.setUserProperty(ANALYTICS_USER_PROP, STATUS_INSTANT);
    } else {
        firebaseAnalytics.setUserProperty(ANALYTICS_USER_PROP, STATUS_INSTALLED);
    }

}

Once you set the app_type user property, you can select an event in the Analytics console's Events tab and then filter the event by the app_type value. The resulting data projection gives you a count for the specified event in your instant or installed app.

For more information about how to log and view events in Google Analytics for Firebase, see Log Events.

Interpreting Analytics events

Analytics allows you to track a variety of metrics valuable to an instant app. The following table describes relevant metrics for your instant app, including the corresponding event name or property in Analytics.

Name Analytics value Definition
Visits session_start Session started. This event is automatically tracked.
Physical purchases Firebase.Event.ECOMMERCE_PURCHASE Physical purchases. You must explicitly track this event in your code.
Digital purchases in_app_purchase Digital in-app purchases. This event is automatically tracked.
Time in app user_engagement Amount of time that the app spends in the foreground. This event is automatically tracked.
Instant app context app_type Events raised from the app running in the instant or installed context. You must explicitly track this event in your code. See Differentiating between installed and instant app data above.
Return visitors session_start.count and app_type Audience of users who visit twice or more. You must explicitly track the app_type event; session_start is tracked for you. See Differentiating between installed and instant app data above.

For more information about the constants for events that you can collect in Analytics, see FirebaseAnalytics.Event.