広告のプリロード(ベータ版)

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

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

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

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

前提条件

このチュートリアルに進む前に、Google Mobile Ads SDK を設定する必要があります。

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

アプリの起動時に preload メソッドを 1 回呼び出します。preload メソッドを呼び出すと、Google Mobile Ads SDK は広告を自動的にプリロードし、プリロードされた構成のリクエストが失敗した場合は再試行します。

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

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:(nonnull 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 は利用可能な広告を取得し、次の広告をバックグラウンドで自動的にプリロードします。

広告を表示する準備が整うまでは、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 loads 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:(nonnull 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.responseInfo(
    with: preloadID)
  {
    print("Ad response ID: \(responseInfo.responseIdentifier ?? "")")
  }
}

Objective-C

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

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

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

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

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

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:(nonnull 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.
  // Don't call InterstitialAdPreloader.shared.preload or
  // InterstitialAdPreloader.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.
  // 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 はメモリ消費量と広告配信のレイテンシのバランスを取るようにバッファサイズを最適化します。アプリで次の広告が読み込まれる前に広告を表示する場合は、カスタム バッファサイズを設定して、メモリに保持される広告の数を増やすことができます。

Swift

let preloadConfig = PreloadConfigurationV2(adUnitID: "ca-app-pub-3940256099942544/1712485313")
preloadConfig.bufferSize = 3

Objective-C

GADPreloadConfigurationV2 *preloadConfig =
    [[GADPreloadConfigurationV2 alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313"];
preloadConfig.bufferSize = 3;

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

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

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