App Open ads

App open ads are a special ad format intended for publishers wishing to monetize their app load screens. App open ads can be closed at any time, and are designed to be shown when your users bring your app to the foreground.

App open ads automatically show a small branding area so users know they're in your app. Here is an example of what an app open ad looks like:

Prerequisites

Always test with test ads

The following sample code contains an ad unit ID which you can use to request test ads. It's been specially configured to return test ads rather than production ads for every request, making it safe to use.

However, after you've registered an app in the AdMob web interface and created your own ad unit IDs for use in your app, explicitly configure your device as a test device during development.

Android

ca-app-pub-3940256099942544/9257395921

iOS

ca-app-pub-3940256099942544/5575463023

Implementation

The main steps to integrate app open ads are:

  1. Create a utility class
  2. Load the app open ad
  3. Listen to app open ad events
  4. Consider ad expiration
  5. Listen to app state events
  6. Show the app open ad
  7. Clean up the app open ad
  8. Preload the next app open ad

Create a utility class

Create a new class called AppOpenAdController to load the ad. This class controls an instance variable to keep track of a loaded ad and the ad unit ID for each platform.

using System;
using UnityEngine;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;

/// <summary>
/// Demonstrates how to use the Google Mobile Ads app open ad format.
/// </summary>
[AddComponentMenu("GoogleMobileAds/Samples/AppOpenAdController")]
public class AppOpenAdController : MonoBehaviour
{

    // These ad units are configured to always serve test ads.
#if UNITY_ANDROID
    private string _adUnitId = "ca-app-pub-3940256099942544/9257395921";
#elif UNITY_IPHONE
    string _adUnitId = "ca-app-pub-3940256099942544/5575463023";
#else
    private string _adUnitId = "unused";
#endif

    public bool IsAdAvailable
    {
        get
        {
            return _appOpenAd != null;
        }
    }

    public void Start()
    {
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize((InitializationStatus initStatus) =>
        {
            // This callback is called once the MobileAds SDK is initialized.
        });
    }

    /// <summary>
    /// Loads the app open ad.
    /// </summary>
    public void LoadAppOpenAd()
    {
    }

    /// <summary>
    /// Shows the app open ad.
    /// </summary>
    public void ShowAppOpenAd()
    {
    }
}

Load the app open ad

Loading an app open ad is accomplished using the static Load() method on the AppOpenAd class. The load method requires an ad unit ID, an AdRequest object, and a completion handler which gets called when ad loading succeeds or fails. The loaded AppOpenAd object is provided as a parameter in the completion handler. The example below shows how to load an AppOpenAd.


 // These ad units are configured to always serve test ads.
#if UNITY_ANDROID
   private string _adUnitId = "ca-app-pub-3940256099942544/9257395921";
#elif UNITY_IPHONE
   string _adUnitId = "ca-app-pub-3940256099942544/5575463023";
#else
  private string _adUnitId = "unused";
#endif

  private AppOpenAd appOpenAd;

  /// <summary>
  /// Loads the app open ad.
  /// </summary>
  public void LoadAppOpenAd()
  {
      // Clean up the old ad before loading a new one.
      if (appOpenAd != null)
      {
            appOpenAd.Destroy();
            appOpenAd = null;
      }

      Debug.Log("Loading the app open ad.");

      // Create our request used to load the ad.
      var adRequest = new AdRequest();

      // send the request to load the ad.
      AppOpenAd.Load(_adUnitId, adRequest,
          (AppOpenAd ad, LoadAdError error) =>
          {
              // if error is not null, the load request failed.
              if (error != null || ad == null)
              {
                  Debug.LogError("app open ad failed to load an ad " +
                                 "with error : " + error);
                  return;
              }

              Debug.Log("App open ad loaded with response : "
                        + ad.GetResponseInfo());

              appOpenAd = ad;
              RegisterEventHandlers(ad);
          });
  }

Listen to app open ad events

To further customize the behavior of your ad, you can hook into a number of events in the ad's lifecycle: opening, closing, and so on. Listen for these events by registering a delegate as shown below.

private void RegisterEventHandlers(AppOpenAd ad)
{
    // Raised when the ad is estimated to have earned money.
    ad.OnAdPaid += (AdValue adValue) =>
    {
        Debug.Log(String.Format("App open ad paid {0} {1}.",
            adValue.Value,
            adValue.CurrencyCode));
    };
    // Raised when an impression is recorded for an ad.
    ad.OnAdImpressionRecorded += () =>
    {
        Debug.Log("App open ad recorded an impression.");
    };
    // Raised when a click is recorded for an ad.
    ad.OnAdClicked += () =>
    {
        Debug.Log("App open ad was clicked.");
    };
    // Raised when an ad opened full screen content.
    ad.OnAdFullScreenContentOpened += () =>
    {
        Debug.Log("App open ad full screen content opened.");
    };
    // Raised when the ad closed full screen content.
    ad.OnAdFullScreenContentClosed += () =>
    {
        Debug.Log("App open ad full screen content closed.");
    };
    // Raised when the ad failed to open full screen content.
    ad.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogError("App open ad failed to open full screen content " +
                       "with error : " + error);
    };
}

Consider ad expiration

To ensure you don't show an expired ad, add a method to the AppOpenAdController that checks how long it has been since your ad loaded. Then, use that method to check if the ad is still valid.

The app open ad has a 4 hour timeout. Cache the load time in the _expireTime variable.

// send the request to load the ad.
AppOpenAd.Load(_adUnitId, adRequest,
    (AppOpenAd ad, LoadAdError error) =>
    {
        // If the operation failed, an error is returned.
        if (error != null || ad == null)
        {
            Debug.LogError("App open ad failed to load an ad with error : " +
                            error);
            return;
        }

        // If the operation completed successfully, no error is returned.
        Debug.Log("App open ad loaded with response : " + ad.GetResponseInfo());

        // App open ads can be preloaded for up to 4 hours.
        _expireTime = DateTime.Now + TimeSpan.FromHours(4);

        _appOpenAd = ad;
    });

Update the IsAdAvailable property to check _expireTime to confirm the loaded ad is still valid.

public bool IsAdAvailable
{
    get
    {
        return _appOpenAd != null
               && _appOpenAd.IsLoaded()
               && DateTime.Now < _expireTime;
    }
}

Listen to app state events

Use the AppStateEventNotifier to listen to application foreground and background events. This class will raise the AppStateChanged event whenever the application foregrounds or backgrounds.

private void Awake()
{
    // Use the AppStateEventNotifier to listen to application open/close events.
    // This is used to launch the loaded ad when we open the app.
    AppStateEventNotifier.AppStateChanged += OnAppStateChanged;
}

private void OnDestroy()
{
    // Always unlisten to events when complete.
    AppStateEventNotifier.AppStateChanged -= OnAppStateChanged;
}

When we handle the AppState.Foreground state and the IsAdAvailable is true, we call ShowAppOpenAd() to show the ad.

private void OnAppStateChanged(AppState state)
{
    Debug.Log("App State changed to : "+ state);

    // if the app is Foregrounded and the ad is available, show it.
    if (state == AppState.Foreground)
    {
        if (IsAdAvailable)
        {
            ShowAppOpenAd();
        }
    }
}

Show the app open ad

To show a loaded app open ad, call the Show() method on the AppOpenAd instance. Ads can only be shown once per load. Use the CanShowAd() method to verify that the ad is ready to be shown.

/// <summary>
/// Shows the app open ad.
/// </summary>
public void ShowAppOpenAd()
{
    if (appOpenAd != null && appOpenAd.CanShowAd())
    {
        Debug.Log("Showing app open ad.");
        appOpenAd.Show();
    }
    else
    {
        Debug.LogError("App open ad is not ready yet.");
    }
}

Clean up the app open ad

When you are finished with a AppOpenAd, make sure to call the Destroy() method before dropping your reference to it:

appOpenAd.Destroy();

This notifies the plugin that the object is no longer used and the memory it occupies can be reclaimed. Failure to call this method results in memory leaks.

Preload the next app open ad

AppOpenAd is a one-time-use object. This means once a app open ad is shown, the object can't be used again. To request another app open ad, you'll need to create a new AppOpenAd object.

To prepare an app open ad for the next impression opportunity, preload the app open ad once the OnAdFullScreenContentClosed or OnAdFullScreenContentFailed ad event is raised.

private void RegisterReloadHandler(AppOpenAd ad)
{
    ...
    // Raised when the ad closed full screen content.
    ad.OnAdFullScreenContentClosed += ()
    {
        Debug.Log("App open ad full screen content closed.");

        // Reload the ad so that we can show another as soon as possible.
        LoadAppOpenAd();
    };
    // Raised when the ad failed to open full screen content.
    ad.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogError("App open ad failed to open full screen content " +
                       "with error : " + error);

        // Reload the ad so that we can show another as soon as possible.
        LoadAppOpenAd();
    };
}

Cold starts and loading screens

The documentation thus far assumes that you only show app open ads when users foreground your app when it is suspended in memory. "Cold starts" occur when your app is launched but was not previously suspended in memory.

An example of a cold start is when a user opens your app for the first time. With cold starts, you won't have a previously loaded app open ad that's ready to be shown right away. The delay between when you request an ad and receive an ad back can create a situation where users are able to briefly use your app before being surprised by an out of context ad. This should be avoided because it is a bad user experience.

The preferred way to use app open ads on cold starts is to use a loading screen to load your game or app assets, and to only show the ad from the loading screen. If your app has completed loading and has sent the user to the main content of your app, do not show the ad.

Best practices

App open ads help you monetize your app's loading screen when the app first launches and during app switches, but it's important to keep the following best practices in mind so that your users enjoy using your app.

  • Show your first app open ad after your users have used your app a few times.
  • Show app open ads during times when your users would otherwise be waiting for your app to load.
  • If you have a loading screen under the app open ad and your loading screen completes loading before the ad is dismissed, dismiss your loading screen in the OnAdDidDismissFullScreenContent event handler.
  • On the iOS platform, AppStateEventNotifier instantiates an AppStateEventClient GameObject. This GameObject is required for events to fire so don't destroy it. Events stop firing if the GameObject is destroyed.

Additional resources