広告のプリロードは、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
// 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
// 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 イベントに登録すると、広告のプリロードが成功したとき、プリロードに失敗したとき、広告キャッシュがなくなったときに通知を受け取ることができます。
Preload イベントは分析を目的としています。Preload イベントのコールバック内:
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 を呼び出します。
バッファサイズを設定する
バッファサイズは、メモリに保持されるプリロードされた広告の数を制御します。デフォルトでは、メモリ消費量と広告配信のレイテンシのバランスを取るために、バッファサイズが最適化されます。次の広告が読み込まれる前にアプリで広告が表示される場合は、カスタム バッファサイズを設定して、メモリに保持される広告の数を増やすことができます。
Swift
let preloadConfig = PreloadConfigurationV2(adUnitID: adUnitID)
// Define a PreloadConfiguration and set the buffer size to 2 preloaded ads.
preloadConfig.bufferSize = 2
Objective-C
GADPreloadConfigurationV2 *preloadConfig =
[[GADPreloadConfigurationV2 alloc] initWithAdUnitID:adUnitID];
// Define a PreloadConfiguration and set the buffer size to 2 preloaded ads.
preloadConfig.bufferSize = 2;
プリロード キャッシュの上限
Google Mobile Ads SDK は、すべての広告ユニットとプリロード ID にわたるプリロードされた広告の総数に、アプリ全体の上限を適用します。
- デフォルトの上限: Google Mobile Ads SDK は、メモリに最大 6 個のプリロードされた広告を保持します。この上限は、すべてのフォーマットとプリロード ID で共有されます。
- プリロード ID ごとに 2 つのバッファサイズを維持することをおすすめします。