AdMob+Firebase 103 Unity: Change the app based on the predicted user behavior

1. Introduction

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

  • How to use the Firebase Predictions
  • How to create a Remote Config condition based on the prediction

What you'll need

  • Unity 2018.4.4f1 or higher
  • Xcode 10 or higher (to build the target for the iOS)
  • A Google account
  • A test device with Android 5.0+ with a USB cable to connect your device, or an Android Emulator running AVD(Android Virtual Device) with a system image that supports Play Store/Google APIs
  • An iOS device or a simulator running iOS 8.0 or higher

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

2. Setup development environment

Download the code

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

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

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

$ git clone

The repository contains four folders as follows:

  • android_studio_folder.png101-base — Starting code that you will build in this codelab.
  • android_studio_folder.png101-complete_and_102-base — Completed code for this codelab & starter for the 102 codelab.
  • android_studio_folder.png102-complete_and_103-base — Completed code for the 102 codelab & starter for the 103 codelab.
  • android_studio_folder.png103-complete — Completed code for the 103 codelab.

Prepare required files

The Awesome Drawing Quiz uses several open-source codes, which are required to compile and run the project.

Open the terminal, and move to the repository root directory. Then, run ./gradlew :prepareThirdPartyFiles (gradlew.bat :prepareThirdPartyFiles on Windows) from the terminal to copy required files into the project.

Import the starter app

Launch Unity, choose "Open" 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 Unity.

Add Google Mobile Ads Unity Plugin

To serve AdMob Ads on the Unity app, you need to add the Google Mobile Ads Unity Plugin into the project.

  1. Download the Google Mobile Ads Unity Plugin 3.18.1 package. (Note that this Codelab may not compatible with the other version of the plugin)
  2. In the Awesome Drawing Quiz Unity project. In the project, navigate to Assets > Import Package > Custom Package.
  3. Import the GoogleMobileAds.unitypackage into the project that you have downloaded.

You need to set the AdMob app ID as well. In the Unity editor, select Assets > Google Mobile Ads > Settings from the menu.


Enable AdMob by clicking Enabled checkbox under Google AdMob section. Then enter the AdMob app ID as follows:

  • Android: ca-app-pub-3940256099942544~3048611032
  • iOS: ca-app-pub-3940256099942544~2753522596


Add Firebase config files to the Unity project

  1. From the overview screen of the Awesome Drawing Quiz project, click the Settings icon. c3ad8af5ce1a343a.png
  2. Under the General tab, select each Android and iOS app to download the google-service.json (for Android) and the GoogleService-Info.plist (for iOS) file.
  3. Move both configuration files into the android_studio_folder.pngAssets folder in the Unity project.


Add Firebase Analytics and Remote Config SDK

  1. Download the Firebase Unity SDK 5.5.0 and unzip it somewhere convenient. (skip this step if you have already downloaded the SDK)
  2. Open the Awesome Drawing Quiz Unity project, navigate to Assets > Import Package > Custom Package.
  3. From the unzipped SDK, import the Firebase Analytics SDK (dotnet4/FirebaseAnalytics.unitypackage) and the Firebase Remote Config(dotnet4/FirebaseRemoteConfig.unitypackage) SDK.
  4. In the Import Unity package window, click Import.

3. Open the Firebase project from the console

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.


4. Firebase Predictions Basics

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.

5. Make the difficulty adjustable

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 InitAndFetchRemoteConfig() method in the Main.cs file, add a default value for the difficulty Remote Config parameter as follows.


private Task InitAndFetchRemoteConfig() {
    Dictionary<string, object> defaults = new Dictionary<string, object>();
    // TODO: Set a default value for 'difficulty' Remote Config parameter (103)
    defaults.Add(GameSettings.KEY_DIFFICULTY, GameSettings.DIFFICULTY_NORMAL);


Fetch the difficulty from the Remote Config

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


public class GameSettings {


    // TODO: Add a key for 'difficulty' Remote Config parameter (103)
    public const string KEY_DIFFICULTY= "difficulty";


    public string Difficulty {
        get {
            // TODO: Apply difficulty from the Remote Config (103)
            return FirebaseRemoteConfig.GetValue(KEY_DIFFICULTY).StringValue;
        private set { }


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.


6. Lower the difficulty for the users who are not likely to complete the game

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.


public class QuizManager {


    private void ApplyDifficulty() {
        switch(GameSettings.Instance.Difficulty) {
            case GameSettings.DIFFICULTY_EASY:
                disclosedLettersByDefault = 2;
            case GameSettings.DIFFICULTY_NORMAL:
                disclosedLettersByDefault = 1;
                disclosedLettersByDefault = 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 on Unity - Not likely to complete the game'. Then click the Select... dropdown list , and select the Awesome Drawing Quiz (Unity on 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.


Next, define a new condition for the iOS version of the Awesome Drawing Quiz as follows.


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.

7. All done!

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