광고 미리 로드 (알파)

플랫폼 선택: 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당 한 번만 호출합니다. 앱 세션이 시작될 때 메서드를 호출하는 것이 좋습니다. 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;