広告の事前読み込み(アルファ版)

プラットフォームを選択: Android 新規 Android iOS Unity

広告のプリロードは、Google Mobile Ads SDK の Google 管理の広告読み込み機能で、広告の読み込みとキャッシュ保存を代行します。広告のプリロードでは、広告の読み込みを管理する方法を変更する必要があります。広告のプリロードを使用してパフォーマンスを最適化するには、カスタム キャッシュを無効にして、その責任を Google Mobile Ads SDK に委任します。

広告のプリロードには、手動による広告の読み込みに比べて次のようなメリットがあります。

  • 参照管理: 読み込まれた広告を保持し、表示する準備ができるまで参照を維持する必要がなくなります。
  • 自動再読み込み: キャッシュから広告を取り出すと、新しい広告が自動的に読み込まれます。
  • マネージド再試行: 指数バックオフを使用して、失敗したリクエストを自動的に再試行します。
  • 有効期限の処理: 広告が期限切れになる前に(通常は 1 時間後)、自動的に更新します。
  • キャッシュの最適化: キャッシュサイズが 1 より大きい場合、Google Mobile Ads SDK はキャッシュの順序を最適化して最適な広告を配信します。

このガイドでは、プリロード広告の設定、プリロード広告の利用可能性の確認、プリロード広告の表示について説明します。

前提条件

チュートリアルに進む前に、次の項目を完了する必要があります。

  • Google Mobile Ads SDK バージョン 13.3.0 以降をインストールします。以前のバージョンのデベロッパー リソースは 11.12.0 - 12.2.0 で入手できますが、今後は 13.3.0 以降を使用し、このガイドに沿って作業することをおすすめします。
  • Google Mobile Ads SDK を設定します。

ベータ版ヘッダーをインポートする

広告のプリロードを使用するには、ベータ版のヘッダーをインポートします。

Swift

import GoogleMobileAds_Private

Objective-C

#import <GoogleMobileAds/GoogleMobileAds_Beta.h>

広告の事前読み込みを開始する

広告のプリロードを開始するには、preload() を呼び出します。このメソッドは、プリロード ID ごとに 1 回だけ呼び出します。アプリ セッションの開始時にこのメソッドを呼び出すことをおすすめします。preload() を呼び出すと、Google Mobile Ads SDK は広告を自動的にプリロードし、プリロード ID のリクエストが失敗した場合は再試行します。

次の例では、広告のプリロードを開始しています。

Swift

private func startPreloading(adUnitID: String) {
  // Start the preloading initialization process.
  let request = Request()
  let interstitialConfig = PreloadConfigurationV2(adUnitID: adUnitID, request: request)
  InterstitialAdPreloader.shared.preload(
    for: adUnitID, configuration: interstitialConfig, delegate: self)
}

Objective-C

- (void)startPreloadingWithAdUnitID:(NSString *)adUnitID {
  // Start the preloading initialization process.
  GADRequest *request = [GADRequest request];
  GADPreloadConfigurationV2 *interstitialConfig = [[GADPreloadConfigurationV2 alloc] initWithAdUnitID:adUnitID request:request];

  [GADInterstitialAdPreloader.sharedInstance preloadForPreloadID:adUnitID
                                                       configuration:interstitialConfig
                                                            delegate:self];
}

プリロードされた広告を取得して表示する

広告のプリロードを使用する場合、Google Mobile Ads SDK はキャッシュに保存された広告を保持します。広告を表示する場合は、adWithPreloadID() を呼び出します。Google Mobile Ads SDK は利用可能な広告を取得し、バックグラウンドで次の広告を自動的にプリロードします。

次の例では、プリロードされた広告を取得して表示します。

Swift

private func showInterstitialAd(adUnitID: String) {
  // Verify that the preloaded ad is available before polling.
  guard isInterstitialAvailable(adUnitID: adUnitID) else {
    print("Preloaded interstitial ad is not available.")
    return
  }

  // Polling returns the next available ad and load another ad in the background.
  let ad = InterstitialAdPreloader.shared.ad(with: adUnitID)

  // Interact with the ad object as needed.
  print("Interstitial ad response info: \(String(describing: ad?.responseInfo))")
  ad?.paidEventHandler = { (value: AdValue) in
    print("Interstitial ad paid event: \(value.value), \(value.currencyCode)")
  }

  ad?.fullScreenContentDelegate = self
  ad?.present(from: self)
}

Objective-C

- (void)showInterstitialAdWithAdUnitID:(NSString *)adUnitID {
  // Verify that the preloaded ad is available before polling.
  if (![self isInterstitialAvailableWithAdUnitID:adUnitID]) {
    NSLog(@"Preloaded interstitial ad is not available.");
    return;
  }

  // Getting the preloaded ad loads another ad in the background.
  GADInterstitialAd *ad = [GADInterstitialAdPreloader.sharedInstance adWithPreloadID:adUnitID];

  // Interact with the ad object as needed.
  NSLog(@"Interstitial ad response info: %@", ad.responseInfo);
  ad.paidEventHandler = ^(GADAdValue *_Nonnull value) {
    NSLog(@"Interstitial ad paid event: %@ %@ ", value.value, value.currencyCode);
  };
  ad.fullScreenContentDelegate = self;
  [ad presentFromRootViewController:self];
}

プリロードされた広告のメタデータを取得する

プリロードされた広告のメタデータを取得するには、広告のレスポンス情報オブジェクトを検索します。このプロセスでは、広告のメタデータをキャッシュから削除せずに検査できます。

次の例では、レスポンス情報オブジェクトからプリロードされた広告のメタデータを検索します。

Swift

private func getInterstitialAdResponseInfo(preloadID: String) {
  // Get the response info for the preloaded ad.
  if let responseInfo = InterstitialAdPreloader.shared.adResponseInfo(withPreloadID: preloadID) {
    print("Ad response ID: \(responseInfo.responseID)")
  }
}

Objective-C

- (void)getInterstitialAdResponseInfoWithPreloadID:(NSString *)preloadID {
  // Get the response info for the preloaded ad.
  GADResponseInfo *responseInfo =
      [GADInterstitialAdPreloader.sharedInstance
          adResponseInfoWithPreloadID:preloadID];
  if (responseInfo) {
    NSLog(@"Ad response ID: %@", responseInfo.responseID);
  }
}

プリロード広告の可用性を確認する

広告の利用状況を確認するには、次のいずれかを選択します。

プリロードされた広告の利用可能性を取得する

次の例では、広告の利用可能性を確認します。

Swift

private func isInterstitialAvailable(adUnitID: String) -> Bool {
  // Verify that an ad is available before polling.
  return InterstitialAdPreloader.shared.isAdAvailable(with: adUnitID)
}

Objective-C

- (BOOL)isInterstitialAvailableWithAdUnitID:(NSString *)adUnitID {
  // Verify that an ad is available before polling.
  return [GADInterstitialAdPreloader.sharedInstance isAdAvailableWithPreloadID:adUnitID];
}

プリロードされた広告の利用可能性をリッスンする

プリロード イベントを登録すると、広告のプリロードが成功したとき、プリロードに失敗したとき、広告キャッシュがなくなったときに通知を受け取ることができます。

プリロード イベントは分析を目的としています。プリロード イベントのコールバック内:

  • preload() は呼び出さないでください。
  • 広告がすぐに表示される場合を除き、adWithPreloadID() の呼び出しは避けてください。

次の例では、広告イベントを登録しています。

Swift

func adAvailable(forPreloadID preloadID: String, responseInfo: ResponseInfo) {
  // This callback indicates that an ad is available for the specified configuration.
  // No action is required here, but updating the UI can be useful in some cases.
  print("Ad preloaded successfully for ad preload ID: \(preloadID)")
}

func adsExhausted(forPreloadID preloadID: String) {
  // This callback indicates that all the ads for the specified configuration have been
  // consumed and no ads are available to show. No action is required here, but updating
  // the UI can be useful in some cases.
  // [Important] Don't call AdPreloader.shared.preload or AdPreloader.shared.ad
  // from adsExhausted.
  print("Ad exhausted for ad preload ID: \(preloadID)")
}

func adFailedToPreload(forPreloadID preloadID: String, error: Error) {
  print(
    "Ad failed to load with ad preload ID: \(preloadID), Error: \(error.localizedDescription)"
  )
}

Objective-C

- (void)adAvailableForPreloadID:(nonnull NSString *)preloadID responseInfo:(nonnull GADResponseInfo *)responseInfo {
  // This callback indicates that an ad is available for the specified configuration.
  // No action is required here, but updating the UI can be useful in some cases.
  NSLog(@"Ad preloaded successfully for ad unit ID: %@", preloadID);
}

- (void)adsExhaustedForPreloadID:(nonnull NSString  *)preloadID {
  // This callback indicates that all the ads for the specified configuration have been
  // consumed and no ads are available to show. No action is required here, but updating
  // the UI can be useful in some cases.
  // [Important] Don't call [GAD<Format>AdPreloader preloadForPreloadID:] or
  // [GAD<Format>AdPreloader adWithPreloadID:] from adsExhaustedForPreloadID.
  NSLog(@"Ad exhausted for ad preload ID: %@", preloadID);
}

- (void)adFailedToPreloadForPreloadID:(nonnull NSString  *)preloadID error:(nonnull NSError *)error {
  NSLog(@"Ad failed to load with ad preload ID: %@, Error: %@", preloadID, error.localizedDescription);
}

広告の事前読み込みを停止する

セッションでプリロード ID の広告を再度表示する必要がない場合は、広告のプリロードを停止できます。特定のプリロード ID の広告のプリロードを停止するには、プリロード ID を指定して stopPreloadingAndRemoveAdsForPreloadID() を呼び出します。すべてのプリローダーのプリロードを停止するには、stopPreloadingAndRemoveAllAds() を呼び出します。

バッファサイズを設定する

バッファサイズは、メモリに保持されるプリロード済み広告の数を制御します。デフォルトでは、Google はメモリ消費量と広告配信のレイテンシのバランスを取るようにバッファサイズを最適化します。アプリで次の広告が読み込まれる前に広告が表示される場合は、カスタム バッファサイズを設定して、メモリに保持される広告の数を増やすことができます。バッファサイズは 2 または 3 にすることをおすすめします。

プリロード キャッシュの上限

Google Mobile Ads SDK は、すべての広告ユニットとプリロード ID でプリロードされる広告の合計数にアプリ全体の制限を適用します。

  • デフォルトの上限: Google Mobile Ads SDK は、メモリに最大 6 個のプリロードされた広告を保持します。この上限は、すべての形式とプリロード ID で共有されます。
  • プリロード ID ごとに 2 ~ 3 個のバッファサイズを維持することをおすすめします。

Swift

let preloadConfig = PreloadConfigurationV2(adUnitID: "/21775744923/example/rewarded")
preloadConfig.bufferSize = 3

Objective-C

GADPreloadConfigurationV2 *preloadConfig =
    [[GADPreloadConfigurationV2 alloc] initWithAdUnitID:@"/21775744923/example/rewarded"];
preloadConfig.bufferSize = 3;