広告のプリロードは、Google Mobile Ads SDK の Google 管理の広告読み込み機能で、 広告の読み込みとキャッシュを代行して管理します。広告のプリロードを使用するには、広告の読み込み方法を変更する必要があります。広告のプリロードを使用してパフォーマンスを最適化するには、 カスタム キャッシュを無効にして、その処理を Google Mobile Ads SDK に委任します。
広告のプリロードには、手動での広告読み込みに比べて次のようなメリットがあります。
- 参照の管理: 読み込まれた広告を保持するため、広告を表示する準備ができるまで参照を維持する必要はありません。
- 自動再読み込み: キャッシュから広告を取り出すと、新しい広告が自動的に読み込まれます。
- 管理された再試行: 指数バックオフを使用して、失敗したリクエストを自動的に再試行します。
- 有効期限の処理: 広告の有効期限が切れる前に(通常は 1 時間後)、広告を自動的に更新します。
- キャッシュの最適化: 2 以上のキャッシュサイズを使用すると、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: "/21775744923/example/rewarded")
preloadConfig.bufferSize = 3
Objective-C
GADPreloadConfigurationV2 *preloadConfig =
[[GADPreloadConfigurationV2 alloc] initWithAdUnitID:@"/21775744923/example/rewarded"];
preloadConfig.bufferSize = 3;
プリロード キャッシュの上限
Google Mobile Ads SDK は、すべての広告ユニットとプリロード ID でプリロードされる広告の合計数にアプリ全体の上限を適用します。
- デフォルトの上限: Google Mobile Ads SDK は、最大 6 つのプリロードされた広告をメモリに保持します。 この上限は、すべてのフォーマットとプリロード ID で共有されます。
- プリロード ID ごとに 2 ~ 3 のバッファサイズを維持することをおすすめします。