広告のプリロードは、Google Mobile Ads SDK の Google 管理の広告読み込み機能です。この機能は、広告の読み込みとキャッシュ保存を代行します。広告のプリロードでは、広告の読み込みを管理する方法を変更する必要があります。広告のプリロードを使用してパフォーマンスを最適化するには、カスタム キャッシュを無効にして、その責任を Google Mobile Ads SDK に委任します。
広告のプリロードには、手動による広告の読み込みに比べて次のようなメリットがあります。
- 参照管理: 読み込まれた広告を保持し、表示する準備ができるまで参照を維持する必要がなくなります。
- 自動再読み込み: キャッシュから広告を取り出すと、新しい広告が自動的に読み込まれます。
- マネージド再試行: 指数バックオフを使用して、失敗したリクエストを自動的に再試行します。
- 有効期限の処理: 広告が期限切れになる前に(通常は 1 時間後)、自動的に更新されます。
- キャッシュの最適化: キャッシュサイズを 1 より大きく設定すると、Google Mobile Ads SDK はキャッシュの順序を最適化して最適な広告を配信します。
このガイドでは、プリロード広告の設定、プリロード広告の利用可能性の確認、プリロード広告の表示について説明します。
前提条件
チュートリアルに進む前に、次の項目を完了する必要があります。
- Google Mobile Ads SDK バージョン 12.6.0 以降をインストールします。以前のバージョンのデベロッパー向けリソースは 11.12.0 - 12.2.0 で入手できますが、12.6.0 以降を使用し、今後このガイドに沿って作業することをおすすめします。
- Google Mobile Ads SDK を設定します。
プリロード プレビュー ヘッダーをダウンロードして、プロジェクトに含めます。
省略可: 広告のプリロードのサンプルアプリをダウンロードして実行します。
広告のプリロードを開始する
広告のプリロードを開始するには、preload() を呼び出します。このメソッドは、アプリの起動時に 1 回だけ呼び出します。preload() を呼び出すと、Google Mobile Ads SDK は自動的に広告をプリロードし、プリロードされた構成のリクエストが失敗した場合は再試行します。
次の例では、広告のプリロードを開始しています。
Swift
// Start the preloading initialization process.
let request = Request()
let interstitialConfig = PreloadConfigurationV2(
adUnitID: Constants.interstitialAdUnitID, request: request)
InterstitialAdPreloader.shared.preload(
for: Constants.interstitialAdUnitID, configuration: interstitialConfig, delegate: self)
Objective-C
// Start the preloading initialization process.
GADRequest *request = [GADRequest request];
GADPreloadConfigurationV2 *interstitialConfig = [[GADPreloadConfigurationV2 alloc] initWithAdUnitID:interstitialAdUnitID request:request];
[GADInterstitialAdPreloader.sharedInstance preloadForPreloadID:interstitialAdUnitID
configuration:interstitialConfig
delegate:self];
プリロードされた広告を取得して表示する
広告のプリロードを使用する場合、Google Mobile Ads SDK はキャッシュに保存された広告を保持します。広告を表示する場合は、adWithPreloadID() を呼び出します。Google Mobile Ads SDK は利用可能な広告を取得し、バックグラウンドで次の広告を自動的にプリロードします。
広告を表示する準備が整うまでは、このメソッドの呼び出しを避けてください。広告をキャッシュに保存しておくと、Google Mobile Ads SDK によって期限切れの広告が自動的に更新され、キャッシュの最適化が行われます。
次の例では、プリロードされた広告を取得して表示します。
Swift
private func showInterstitialAd() {
// Verify that the preloaded ad is available before polling.
guard isInterstitialAvailable() else {
printAndShowAlert("Preload interstitial ad is exhausted.")
return
}
// Polling returns the next available ad and load another ad in the background.
let ad = InterstitialAdPreloader.shared.ad(with: Constants.interstitialAdUnitID)
// 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)showInterstitialAd {
// Verify that the preloaded ad is available before polling.
if (![self isInterstitialAvailable]) {
[self logAndShowAlert:@"Preloaded interstitial ad is not available."];
return;
}
// Getting the preloaded ad loads another ad in the background.
GADInterstitialAd *ad = [GADInterstitialAdPreloader.sharedInstance adWithPreloadID:interstitialAdUnitID];
// 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 isInterstitialAvailable() -> Bool {
// Verify that an ad is available before polling.
return InterstitialAdPreloader.shared.isAdAvailable(with: Constants.interstitialAdUnitID)
}
Objective-C
- (BOOL)isInterstitialAvailable {
// Verify that an ad is available before polling.
return [GADInterstitialAdPreloader.sharedInstance isAdAvailableWithPreloadID:(interstitialAdUnitID)];
}
プリロードされた広告の利用可能性をリッスンする
プリロード イベントを登録すると、広告のプリロードが成功したとき、プリロードに失敗したとき、広告キャッシュがなくなったときに通知を受け取ることができます。
プリロード イベントは分析を目的としています。プリロード イベントのコールバック内:
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() を呼び出します。
バッファサイズを設定する
バッファサイズは、メモリに保持されるプリロードされた広告の数を制御します。デフォルトでは、Google はメモリ消費量と広告配信レイテンシのバランスを取るようにバッファサイズを最適化します。アプリで次の広告が読み込まれる前に広告を表示する場合は、カスタム バッファサイズを設定して、メモリに保持する広告の数を増やすことができます。バッファサイズは最大 4 にすることをおすすめします。
Swift
let preloadConfig = PreloadConfigurationV2(adUnitID: "/21775744923/example/rewarded")
preloadConfig.bufferSize = 3
Objective-C
GADPreloadConfigurationV2 *preloadConfig =
[[GADPreloadConfigurationV2 alloc] initWithAdUnitID:@"/21775744923/example/rewarded"];
preloadConfig.bufferSize = 3;