By using the Firebase Remote Config, you can change the app's behavior without having to publish an app update. However, the change will be applied to every user regardless of each user's predicted behavior.

For example, for users who think the game is too difficult for them, they are likely to remove it. In this case, how can you prevent them from losing their interest? What if you can automatically adjust the game difficulty for those kinds of people, so they can continue to play the game?

What you'll learn

What you'll need

How would you rate your level of experience with AdMob?

Novice Intermediate Proficient

How would you rate your level of experience with Firebase?

Novice Intermediate Proficient

Download the code

Click the following button to download all the code for this codelab:

Download source code

Unpack the downloaded zip file. This will unpack a root folder named admob-firebase-codelabs-android-master.

...or clone the GitHub repository from the command line.

$ git clone https://github.com/googlecodelabs/admob-firebase-codelabs-android

The repository contains four folders as follows:

Import the starter app

Launch Android Studio, choose "Import project" in the welcome screen. Then select the 102-complete_and_103-base directory from the code you have downloaded.

You should now have the project open in Android Studio.

Add Firebase config file to the Android project

  1. From the overview screen of the Awesome Drawing Quiz project, click the Settings icon.
  2. Under the General tab, select the Android app to download the google-service.json file.
  3. Move the configuration file into the android_studio_folder.pngapp directory in your project.

Before moving on to the next step, open the project from the Firebase console that you have created in the ‘Setup Firebase Project' step of the AdMob+Firebase 101 Codelab.

Firebase Predictions applies machine learning to your analytics data to create dynamic user groups based on your users' predicted behavior.

Predefined Predictions

By default, Predictions provides four types of predictions: churn and not_churn, which predicts which users will disengage (or won't) from your app over the next 7 days (that is, they will not open the app or app-related notification messages), and spend and not_spend, which predicts which users will (or won't) spend money in your app over the next seven days.

Once you turn on Predictions in your project, you'll see the following screen that shows you a list of predictions provided by Firebase by default.

Firebase Predictions needs sufficient event data volume to make meaningful predictions. If Predictions does not have sufficient data to make a prediction, it will display "Preparing a prediction" message as you can see from the above screenshot.

Once prediction data is ready for use, its status will be updated, and the number of users that can be targeted along with its risk tolerance will also be shown on the prediction card. The following screenshot shows an example of prediction cards:

For more information about predefined predictions, refer to developer documentation.

Custom Predictions

In addition to Predefined Predictions, Firebase Predictions allows you to create predictions that target users who are likely to trigger any Analytics conversion event.

In this Codelab, you will create a custom prediction that predicts users who are not likely to complete the game, based on the game_complete analytics event. Also, you will learn how to use the Predictions and the Remote Config together to provide a different game difficulty to users in a specific predicted group.

In this section, you're going to make the app's difficulty adjustable without an app update with the Remote Config.

Set a default value of the ‘difficulty' Remote Config parameter

In the remote_config_defaults.xml file under res/xml folder, add a default value for the difficulty Remote Config parameter as follows.

res/xml/remote_config_defaults.xml

<?xml version="1.0" encoding="utf-8"?>
<defaultsMap>
    <!-- TODO: Set a default value for 'difficulty' Remote Config parameter (103) -->
    <entry>
        <key>difficulty</key>
        <value>normal</value>
    </entry>
    
    ...
</defaultsMap>

Fetch the difficulty from the Remote Config

Modify the GameSettings.kt to make the difficulty adjustable in the Remote Config.

GameSettings.kt

class GameSettings(private val rc: FirebaseRemoteConfig) {

  // TODO: Apply difficulty from the Remote Config (103)
  val difficulty: String
    get() = rc.getString(KEY_DIFFICULTY)

  ...

  companion object {

    ...

    // TODO: Add a key for 'difficulty' Remote Config parameter (103)
    private const val KEY_DIFFICULTY = "difficulty"

    ...
  }
}

Create a new Remote Config parameter from the console

Next, you'll create a new Remote Config parameter for the difficulty.

Go to the Firebase console, then select the Awesome Drawing Quiz project that you have created previously. Select the Remote Config, and click the Add parameter button.

Name the parameter as difficulty, and set its default value to normal. Then click the Add Parameter button.

Click the Publish Changes button to make the change live to the users.

To lower the difficulty to only a specific group of users (in this case, users who are not likely to complete the game), you should tell the Remote Config to target this kind of people. In the Remote Config, you can do it by creating a condition based on a prediction.

Enable Analytics data sharing and Predictions

Firebase Predictions uses the data you log with Google Analytics for Firebase to make its predictions. Since you've already added Analytics into the app, the only step left is to share this data with Firebase Predictions.

Go to the Firebase console and select the Predictions panel. You will see the following screen.

Click Yes, I'm in button to enable Analytics data sharing for Predictions.

Mark game_complete event as a conversion

To make a custom prediction based on the analytics event, you need to mark it as a conversion event. In this Codelab, you'll create a custom prediction based on the game_complete event.

In the Events screen, turn on the ‘Mark as conversion' toggle of the game_complete event as shown in the below screenshot.

Create a custom prediction

In the Predictions screen, click the Create a prediction button.

Configure a new prediction as shown in the below screenshot. It will predict users who are not likely to trigger the game_complete event (the one will be triggered once a user finishes the game).

Create a new condition for the ‘difficulty' parameter

Note that the code already handles the difficulty parameter for you. If the difficulty is set to easy, it will reveal the first two letters as a hint while the normal difficulty only reveals the first one letter.

The following code snippet shows how the app handles the difficulty parameter.

GameViewModel.kt

class GameViewModel(
    private val drawingDao: DrawingDao,
    private val settings: GameSettings
) : ViewModel() {

  ...

  private fun applyDifficulty() {
    disclosedLettersByDefault = when (settings.difficulty) {
      GameSettings.DIFFICULTY_EASY -> 2
      GameSettings.DIFFICULTY_NORMAL -> 1
      else -> 1
    }
    disclosedLetters = disclosedLettersByDefault
  }

  ...
}

In the Awesome Drawing Quiz Firebase console, select the Remote Config. Then select the difficulty parameter from the list.

Click the Add value for condition dropdown, then click the Define new condition from the dropdown list.

In the following dialog, name a new condition as ‘Android - Not likely to complete the game'. Then click the Select... dropdown list , and select the Awesome Drawing Quiz (Android) app as a condition.

Click the and button to add more conditions. Click the Select... dropdown list, select the Prediction, and select Predict: will_not_complete_game.

In this Codelab, we're going to target users as conservative as possible. To do so, select the Low risk tolerance as a risk profile.

Click the Create condition button to create a new condition to target users based on the prediction.

After you create a new condition, you'll be able to assign a new value for the users who meet the condition. Set the difficulty to easy for those users while leaving a default difficulty as normal.

Click the Publish Changes button to make the change live to the users.

Now the app will give the first two letters as a hint for the users who are not likely to complete the game, while the others will receive only the first one letter (as we did previously) as a hint.

You have completed AdMob+Firebase 103 Android Codelab. You can find the completed code for this Codelab on android_studio_folder.png103-complete folder.