Ad preloading (Alpha)

  • Ad preloading is an SDK-managed process that simplifies ad loading and caching.

  • Preloading requires installing Google Mobile Ads SDK beta version 0.14.0-alpha01 or higher and completing the get started guide.

  • To start preloading ads, call startPreload() with a PreloadConfiguration.

  • You can get and show a preloaded ad by polling for an available ad and then calling show().

  • To check if preloaded ads are available, you can call isAdAvailable().

Ad preloading is a SDK-managed loading and caching process which eliminates the need for manual ad loading and caching. This process lets you display ads when required without having to manually load ads or handle ad loading callbacks.

This guide covers configuring preload ads, checking preload ad availability, and how to get and show the preloaded ad.

Prerequisites

Before you proceed with the tutorial, you need to complete the following items:

  • Install Google Mobile Ads SDK (beta) version 0.14.0-alpha01 or higher.
  • Complete the Get started guide.

Start preloading ads

To start preloading, call startPreload(). The Google Mobile Ads SDK automatically retries failed ad requests for preloaded configurations.

The following example starts preloading ads:

Kotlin

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

Java

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

Optional: Modify preloading ads

To change which ads are preloaded, call destroy() and then call startPreload() again with the modified configuration.

Optional: Stop preloading ads

To stop preloading ads, call destroy() with a preload ID.

Optional: Set the buffer size

Buffer size controls the number of preloaded ads held in memory. If your app displays ads faster than new ads can be loaded, set the buffer size to a larger value. If memory use concerns you, set the buffer size to a low value such as 2.

Kotlin

private fun setBufferSize(adUnitID: String) {
  val adRequest: AdRequest = AdRequest.Builder(adUnitID).build()
  val preloadConfig = PreloadConfiguration(adRequest, bufferSize = 3)
  InterstitialAdPreloader.start(adUnitID, preloadConfig)
}

Java

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

Optional: Receive preloading notifications

Implement PreloadCallback to receive notifications for ad preloading events. To debug and log, these events provide a response info object.

Kotlin

private fun startPreloadingWithCallback(adUnitID: String) {
  val preloadCallback =
    // [Important] Don't call ad preloader start() or pollAd() within the PreloadCallback.
    object : PreloadCallback {
      override fun onAdFailedToPreload(preloadId: String, adError: LoadAdError) {
        Log.i(
          TAG,
          ("Interstitial preload ad $preloadId failed to load with error: ${adError.message}"),
        )
        // [Optional] Get the error response info for additional details.
        // val responseInfo = adError.responseInfo
      }

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

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

Java

private void startPreloadingWithCallback(String adUnitId) {
  PreloadCallback preloadCallback =
      // [Important] Don't call ad preloader start() or pollAd() within the PreloadCallback.
      new PreloadCallback() {
        @Override
        public void onAdFailedToPreload(String preloadId, LoadAdError adError) {
          Log.e(
              TAG,
              String.format(
                  "Interstitial preload ad %s failed to load with error: %s",
                  preloadId, adError.getMessage()));
          // [Optional] Get the error response info for additional details.
          // ResponseInfo responseInfo = adError.getResponseInfo();
        }

        @Override
        public void onAdsExhausted(String preloadId) {
          Log.i(TAG, "Interstitial preload ad " + preloadId + " is not available");
        }

        @Override
        public void onAdPreloaded(String preloadId, ResponseInfo responseInfo) {
          Log.i(TAG, "Interstitial preload ad " + preloadId + " is available");
        }
      };

  AdRequest adRequest = new AdRequest.Builder(adUnitId).build();
  PreloadConfiguration preloadConfig = new PreloadConfiguration(adRequest);
  InterstitialAdPreloader.start(adUnitId, preloadConfig, preloadCallback);
}

Get and show the preloaded ad

Ads are available to show once onAdsAvailable() is called. Ads are continuously made available as ads are shown and exhausted. After getting an ad, listen to ad lifecycle events and show the ad.

The following example shows 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 = InterstitialAdPreloader.pollAd(adUnitID)

  // Interact with the ad object as needed.
  ad?.apply {
    Log.d(TAG, "Interstitial ad response info: ${this.getResponseInfo()}")
    this.adEventCallback =
      object : InterstitialAdEventCallback {
        override fun onAdImpression() {
          Log.d(TAG, "Interstitial ad recorded an impression.")
        }

        override fun onAdPaid(value: AdValue) {
          Log.d(TAG, "Interstitial ad onPaidEvent: ${value.valueMicros} ${value.currencyCode}")
        }
      }

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

Java

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

  // Interact with the ad object as needed.
  if (ad != null) {
    Log.d(TAG, "Interstitial ad response info: " + ad.getResponseInfo());
    ad.setAdEventCallback(
        new InterstitialAdEventCallback() {
          @Override
          public void onAdImpression() {
            Log.d(TAG, "Interstitial ad recorded an impression.");
          }

          @Override
          public void onAdPaid(AdValue value) {
            Log.d(
                TAG,
                "Interstitial ad onPaidEvent: "
                    + value.getValueMicros()
                    + " "
                    + value.getCurrencyCode());
          }
        });

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

Optional: Check preloaded ad availability

To know whether ads are available, call isAdAvailable:

Kotlin

private fun isAdAvailable(adUnitID: String): Boolean {
  return InterstitialAdPreloader.isAdAvailable(adUnitID)
}

Java

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