Set up app open ads

SDK.

App open ads are an ad format intended for publishers who want 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.

For more information, see App open ad guidance.

This guide explains how to integrate app open ads into an Android app.

Before you begin

Before you continue, do the following:

  • Set up GMA Next-Gen SDK.
  • Use the test app open ad unit ID /6499/example/app-open.
    • When building and testing your app, make sure that you use test ads rather than live, production ads. If you don't use the test ad unit ID, Google can suspend your account.
    • Before you publish your app, replace this ID with your ad unit ID.
    • For details on GMA Next-Gen SDK test ads, see Enable test ads.

Understand ad preloading

Ad preloading in GMA Next-Gen SDK automates ad loading and caching.

Ad preloading provides the following benefits:

  • Reference management: maintains references until ads are shown.
  • Automatic reloading: loads a new ad when one is retrieved from the cache.
  • Managed retries: loads a new ad when one fails to load.
  • Expiration handling: refreshes ads before expiring.
  • Cache optimization: optimizes the cache order to deliver the highest priority ad.

Start ad preloading

To begin preloading ads, call the start method once at app start. After you call the start method, GMA Next-Gen SDK automatically preloads ads and retries failed requests for preloaded configurations.

The following example shows how to start preloading ads:

Kotlin

private fun startPreloading(adUnitId: String) {
  val adRequest = AdRequest.Builder(adUnitId).build()
  val preloadConfig = PreloadConfiguration(adRequest)
  AppOpenAdPreloader.start(adUnitId, preloadConfig)
}

Java

private void startPreloading(String adUnitId) {
  AdRequest adRequest = new AdRequest.Builder(adUnitId).build();
  PreloadConfiguration preloadConfig = new PreloadConfiguration(adRequest);
  AppOpenAdPreloader.start(adUnitId, preloadConfig);
}

Replace AD_UNIT_ID with your ad unit ID.

The previous example shows how to use the ad unit ID as the preload ID. A preload ID is a string identifier that you create to identify an ad preloading configuration. If your app requires multiple targeting configurations for the same ad unit ID, pass a custom string identifier.

Get and show the preloaded ad

When you want to show an ad, call the pollAd method. GMA Next-Gen SDK retrieves an available ad and automatically preloads the next ad in the background. If no ad is available, GMA Next-Gen SDK returns no ads.

When you have an available ad object, call the show method to display the ad. The following example shows how to retrieve and show a preloaded ad:

Kotlin

private fun pollAndShowAd(activity: Activity, adUnitId: String) {
  // Polling returns the next available ad and loads another ad in the background.
  val ad = AppOpenAdPreloader.pollAd(adUnitId)
  if (ad == null) {
    Log.e(TAG, "App open ad is not available.")
    return
  }

  // Interact with the ad object as needed.
  Log.d(TAG, "App open ad response info: ${ad.getResponseInfo()}")
  ad.adEventCallback =
    object : AppOpenAdEventCallback {
      override fun onAdImpression() {
        Log.d(TAG, "App open ad recorded an impression.")
      }
    }
  ad.show(activity)
}

Java

private void pollAndShowAd(Activity activity, String adUnitId) {
  // Polling returns the next available ad and loads another ad in the background.
  AppOpenAd ad = AppOpenAdPreloader.pollAd(adUnitId);

  // Interact with the ad object as needed.
  if (ad == null) {
    Log.e(TAG, "App open ad is not available.");
    return;
  }

  Log.d(TAG, "App open ad response info: " + ad.getResponseInfo());
  ad.setAdEventCallback(
      new AppOpenAdEventCallback() {
        @Override
        public void onAdImpression() {
          Log.d(TAG, "App open ad recorded an impression.");
        }
      });

  // Show the ad.
  ad.show(activity);
}

Avoid calling the pollAd method until you are ready to show an ad. To read the ad response info without showing it, see Read the response info.

Listen to ad events

Before showing the ad, listen to ad events. The following example shows how to register callbacks for ad events:

Kotlin

private fun listenToAdEvents() {
  // Listen for ad events.
  val ad = appOpenAd
  if (ad == null) {
    Log.e(TAG, "App open ad is not ready yet.")
    return
  }

  ad.adEventCallback =
    object : AppOpenAdEventCallback {
      override fun onAdShowedFullScreenContent() {
        // App open ad did show.
      }

      override fun onAdDismissedFullScreenContent() {
        // App open ad did dismiss.
        appOpenAd = null
      }

      override fun onAdFailedToShowFullScreenContent(
        fullScreenContentError: FullScreenContentError
      ) {
        // App open ad failed to show.
        Log.e(TAG, "App open ad failed to show: ${fullScreenContentError.message}")
      }

      override fun onAdImpression() {
        // App open ad did record an impression.
      }

      override fun onAdClicked() {
        // App open ad did record a click.
      }
    }
}

Java

private void listenToAdEvents() {
  // Listen for ad events.
  if (appOpenAd == null) {
    Log.e(TAG, "App open ad is not ready yet.");
    return;
  }

  appOpenAd.setAdEventCallback(
      new AppOpenAdEventCallback() {
        @Override
        public void onAdShowedFullScreenContent() {
          // App open ad did show.
        }

        @Override
        public void onAdDismissedFullScreenContent() {
          // App open ad did dismiss.
          appOpenAd = null;
        }

        @Override
        public void onAdFailedToShowFullScreenContent(
            @NonNull FullScreenContentError fullScreenContentError) {
          // App open ad failed to show.
          Log.e(TAG, "App open ad failed to show: " + fullScreenContentError.getMessage());
        }

        @Override
        public void onAdImpression() {
          // App open ad did record an impression.
        }

        @Override
        public void onAdClicked() {
          // App open ad did record a click.
        }
      });
}

Optional: Listen to preloading events

When you start preloading ads, register for preload events to get notified when ads are preloaded successfully, fail to preload, or the ad cache is exhausted.

The following example shows how to register for preloading ad events:

Kotlin

private fun startPreloadingWithCallback(adUnitId: String) {
  val preloadCallback =
    object : PreloadCallback {
      override fun onAdFailedToPreload(preloadId: String, adError: LoadAdError) {
        Log.d(TAG, "App open preload ad $preloadId failed to load with error: ${adError.message}")
      }

      override fun onAdsExhausted(preloadId: String) {
        Log.i(TAG, "App open preload ad $preloadId is not available")
      }

      override fun onAdPreloaded(preloadId: String, responseInfo: ResponseInfo) {
        Log.i(TAG, "App open preload ad $preloadId is available")
      }
    }
  val adRequest = AdRequest.Builder(adUnitId).build()
  val preloadConfig = PreloadConfiguration(adRequest)
  AppOpenAdPreloader.start(adUnitId, preloadConfig, preloadCallback)
}

Java

private void startPreloadingWithCallback(String adUnitId) {
  PreloadCallback preloadCallback =
      new PreloadCallback() {
        @Override
        public void onAdFailedToPreload(@NonNull String preloadId, @NonNull LoadAdError adError) {
          Log.d(
              TAG,
              String.format(
                  "App open preload ad %s failed to load with error: %s",
                  preloadId, adError.getMessage()));
        }

        @Override
        public void onAdsExhausted(@NonNull String preloadId) {
          Log.i(TAG, String.format("App open preload ad %s is not available", preloadId));
        }

        @Override
        public void onAdPreloaded(@NonNull String preloadId, @NonNull ResponseInfo responseInfo) {
          Log.i(TAG, String.format("App open preload ad %s is available", preloadId));
        }
      };
  AdRequest adRequest = new AdRequest.Builder(adUnitId).build();
  PreloadConfiguration preloadConfig = new PreloadConfiguration(adRequest);
  AppOpenAdPreloader.start(adUnitId, preloadConfig, preloadCallback);
}

When an ad fails to load, GMA Next-Gen SDK automatically preloads ads and retries failed requests for preloaded configurations.

Optional: Check for ad availability

If you need to know whether an ad is available, check for ad availability. The following example shows how to check whether a preloaded ad is available:

Kotlin

private fun isAdAvailable(adUnitId: String): Boolean {
  return AppOpenAdPreloader.isAdAvailable(adUnitId)
}

Java

private boolean isAdAvailable(String adUnitId) {
  return AppOpenAdPreloader.isAdAvailable(adUnitId);
}

Optional: Set the buffer size

The buffer size controls the number of preloaded ads held in memory. By default, Google optimizes buffer size to balance memory consumption and ad serving latency. You can set a custom buffer size to increase the number of ads kept in memory. We recommend a maximum buffer size of four.

The following example shows how to set a buffer size of four preloaded ads:

Kotlin

private fun setBufferSize(adUnitId: String) {
  val adRequest = AdRequest.Builder(adUnitId).build()
  // Four is the recommended maximum buffer size.
  val preloadConfig = PreloadConfiguration(adRequest, bufferSize = 4)
  AppOpenAdPreloader.start(adUnitId, preloadConfig)
}

Java

private void setBufferSize(String adUnitId) {
  AdRequest adRequest = new AdRequest.Builder(adUnitId).build();
  // Four is the recommended maximum buffer size.
  PreloadConfiguration preloadConfig = new PreloadConfiguration(adRequest, 4);
  AppOpenAdPreloader.start(adUnitId, preloadConfig);
}

Preload cache limits

GMA Next-Gen SDK enforces an app-wide limit on the total number of preloaded ads across all ad units and preload IDs:

  • Default limit: Google holds a maximum of 6 preloaded ads in memory. This limit is shared across all formats and preload IDs.
  • We recommend to keep a buffer size of 2 or 3 per preload ID.

Optional: Stop preloading ads

If you don't need to show ads for a specific preload ID again in the session, you can stop preloading ads. To stop loading ads for a specific preload ID, call the destroy method with a preload ID. Calling the destroy method removes all preloaded ads associated with the preload ID from the cache.

The following example shows how to stop preloading ads:

Kotlin

private fun stopPreloading(adUnitId: String) {
  // Stops the preloading and destroy preloaded ads.
  AppOpenAdPreloader.destroy(adUnitId)
}

Java

private void stopPreloading(String adUnitId) {
  // Stops the preloading and destroy preloaded ads.
  AppOpenAdPreloader.destroy(adUnitId);
}

Optional: Read the response info

Read the next preloaded ad's response info without removing the ad from the cache.

The following example shows how to read the next preloaded ad response info:

Kotlin

val responseInfo = AppOpenAdPreloader.peekAdResponseInfo(preloadId)
if (responseInfo == null) {
  Log.e(TAG, "Failed to peek ad response info.")
  return
}

Log.d(TAG, "Peeked ad response ID: ${responseInfo.responseId}")

Java

ResponseInfo responseInfo = AppOpenAdPreloader.peekAdResponseInfo(preloadId);
if (responseInfo == null) {
  Log.e(TAG, "Failed to peek ad response info.");
  return;
}

Log.d(TAG, "Peeked ad response ID: " + responseInfo.getResponseId());

Cold starts and loading screens

A cold start occurs when your app launches from scratch, for example, when a user opens your app for the first time or after memory termination. During a cold start, you lack a previously loaded app open ad ready to show right away.

Due to ad request delays, users might interact with your app before an app open ad appears. To avoid a poor user experience, show app open ads on cold starts strictly from a loading screen while app assets load. If asset loading completes and the user reaches the main content before the ad loads, don't show the ad.

Load app assets on a background thread so loading continues while the ad is displayed.

Best practices

Follow these best practices for app open ads:

  • Show your first app open ad only after a user opens your app several times.
  • Show app open ads only while users wait for your app to load.
  • If your loading screen finishes loading while the ad is displayed, dismiss the loading screen in the ad dismissed callback method.