전면 광고 맞춤 이벤트

기본 요건

맞춤 이벤트 설정을 완료합니다.

전면 광고 요청

폭포식 구조 미디에이션 체인에서 맞춤 이벤트 광고 항목에 도달하면 loadInterstitial:adConfiguration:completionHandler: 메서드는 커스텀 학습 템플릿을 만들 때 입력한 클래스 이름 이벤트를 사용합니다. 이 경우 이 메서드는 SampleCustomEvent에 있으며 이 메서드는 다음을 호출합니다. loadInterstitial:adConfiguration:completionHandler: 메서드 SampleCustomEventInterstitial입니다.

전면 광고를 요청하려면 GADMediationAdapterloadInterstitial:adConfiguration:completionHandler: GADMediationAdapter를 확장하는 클래스가 이미 있는 경우 다음을 구현합니다. 저기 loadInterstitial:adConfiguration:completionHandler:. 또한 GADMediationInterstitialAd를 구현할 새 클래스를 만듭니다.

맞춤 이벤트 예에서 SampleCustomEvent 구현 GADMediationAdapter 인터페이스에 위임한 다음 SampleCustomEventInterstitial입니다.

Swift

import GoogleMobileAds

class SampleCustomEvent: NSObject, GADMediationAdapter {

  fileprivate var interstitialAd: SampleCustomEventInterstitial?
  ...

  func loadInterstitial(
    for adConfiguration: GADMediationInterstitialAdConfiguration,
    completionHandler: @escaping GADMediationInterstitialLoadCompletionHandler
  ) {
    self.interstitialAd = SampleCustomEventInterstitial()
    self.interstitialAd?.loadInterstitial(
      for: adConfiguration, completionHandler: completionHandler)
  }
}

Objective-C

#import "SampleCustomEvent.h"

@implementation SampleCustomEvent

SampleCustomEventInterstitial *sampleInterstitial;

- (void)loadInterstitialForAdConfiguration:
            (GADMediationInterstitialAdConfiguration *)adConfiguration
                         completionHandler:
                             (GADMediationInterstitialLoadCompletionHandler)
                                 completionHandler {
  sampleInterstitial = [[SampleCustomEventInterstitial alloc] init];
  [sampleInterstitial loadInterstitialForAdConfiguration:adConfiguration
                                       completionHandler:completionHandler];
}

SampleCustomEventInterstitial는 다음 작업을 담당합니다.

  • 전면 광고를 로드하고 GADMediationInterstitialAdLoadCompletionHandler 이 메서드를 호출할 수 있습니다.

  • GADMediationInterstitialAd 프로토콜 구현

  • Google 모바일 광고 SDK에 광고 이벤트 콜백을 수신하고 보고합니다.

UI에 정의된 선택적 매개변수는 포함되어 있습니다. 매개변수는 adConfiguration.credentials.settings[@"parameter"] 이 매개변수는 일반적으로 광고 객체를 인스턴스화합니다.

Swift

import GoogleMobileAds

class SampleCustomEventInterstitial: NSObject, GADMediationInterstitialAd {
  /// The Sample Ad Network interstitial ad.
  var interstitial: SampleInterstitial?

  /// The ad event delegate to forward ad rendering events to the Google Mobile Ads SDK.
  var delegate: GADMediationInterstitialAdEventDelegate?

  var completionHandler: GADMediationInterstitialLoadCompletionHandler?

  func loadInterstitial(
    for adConfiguration: GADMediationInterstitialAdConfiguration,
    completionHandler: @escaping GADMediationInterstitialLoadCompletionHandler
  ) {
    interstitial = SampleInterstitial.init(
      adUnitID: adConfiguration.credentials.settings["parameter"] as? String)
    interstitial?.delegate = self
    let adRequest = SampleAdRequest()
    adRequest.testMode = adConfiguration.isTestRequest
    self.completionHandler = completionHandler
    interstitial?.fetchAd(adRequest)
  }

  func present(from viewController: UIViewController) {
    if let interstitial = interstitial, interstitial.isInterstitialLoaded {
      interstitial.show()
    }
  }
}

Objective-C

#import "SampleCustomEventInterstitial.h"

@interface SampleCustomEventInterstitial () <SampleInterstitialAdDelegate,
                                             GADMediationInterstitialAd> {
  /// The sample interstitial ad.
  SampleInterstitial *_interstitialAd;

  /// The completion handler to call when the ad loading succeeds or fails.
  GADMediationInterstitialLoadCompletionHandler _loadCompletionHandler;

  /// The ad event delegate to forward ad rendering events to the Google Mobile
  /// Ads SDK.
  id <GADMediationInterstitialAdEventDelegate> _adEventDelegate;
}
@end

- (void)loadInterstitialForAdConfiguration:
            (GADMediationInterstitialAdConfiguration *)adConfiguration
                         completionHandler:
                             (GADMediationInterstitialLoadCompletionHandler)
                                 completionHandler {
  __block atomic_flag completionHandlerCalled = ATOMIC_FLAG_INIT;
  __block GADMediationInterstitialLoadCompletionHandler
      originalCompletionHandler = [completionHandler copy];

  _loadCompletionHandler = ^id<GADMediationInterstitialAdEventDelegate>(
      _Nullable id<GADMediationInterstitialAd> ad, NSError *_Nullable error) {
    // Only allow completion handler to be called once.
    if (atomic_flag_test_and_set(&completionHandlerCalled)) {
      return nil;
    }

    id<GADMediationInterstitialAdEventDelegate> delegate = nil;
    if (originalCompletionHandler) {
      // Call original handler and hold on to its return value.
      delegate = originalCompletionHandler(ad, error);
    }

    // Release reference to handler. Objects retained by the handler will also
    // be released.
    originalCompletionHandler = nil;

    return delegate;
  };

  NSString *adUnit = adConfiguration.credentials.settings[@"parameter"];
  _interstitialAd = [[SampleInterstitial alloc] initWithAdUnitID:adUnit];
  _interstitialAd.delegate = self;
  SampleAdRequest *adRequest = [[SampleAdRequest alloc] init];
  adRequest.testMode = adConfiguration.isTestRequest;
  [_interstitialAd fetchAd:adRequest];
}

광고를 성공적으로 가져왔거나 오류가 발생한 경우 GADMediationInterstitialLoadCompletionHandler를 호출합니다. 이벤트 GADMediationInterstitialAd를 구현하는 클래스를 전달합니다. 오류 매개변수의 경우 nil 값 실패하면 확인할 수 있습니다

일반적으로 이러한 메서드는 어댑터가 구현하는 서드 파티 SDK를 사용해야 합니다. 이 예에서 샘플 SDK는 관련 콜백이 있는 SampleInterstitialAdDelegate가 있습니다.

Swift

func interstitialDidLoad(_ interstitial: SampleInterstitial) {
  if let handler = completionHandler {
    delegate = handler(self, nil)
  }
}

func interstitial(
  _ interstitial: SampleInterstitial,
  didFailToLoadAdWith errorCode: SampleErrorCode
) {
  let error =
    SampleCustomEventUtilsSwift.SampleCustomEventErrorWithCodeAndDescription(
      code: SampleCustomEventErrorCodeSwift
        .SampleCustomEventErrorAdLoadFailureCallback,
      description:
        "Sample SDK returned an ad load failure callback with error code: \(errorCode)"
    )
  if let handler = completionHandler {
    delegate = handler(nil, error)
  }
}

Objective-C

- (void)interstitialDidLoad:(SampleInterstitial *)interstitial {
  _adEventDelegate = _loadCompletionHandler(self, nil);
}

- (void)interstitial:(SampleInterstitial *)interstitial
    didFailToLoadAdWithErrorCode:(SampleErrorCode)errorCode {
  NSError *error = SampleCustomEventErrorWithCodeAndDescription(
      SampleCustomEventErrorAdLoadFailureCallback,
      [NSString stringWithFormat:@"Sample SDK returned an ad load failure "
                                 @"callback with error code: %@",
                                 errorCode]);
  _adEventDelegate = _loadCompletionHandler(nil, error);
}

GADMediationInterstitialAdpresent 메서드를 구현해야 합니다. 광고:

Swift

func present(from viewController: UIViewController) {
  if let interstitial = interstitial, interstitial.isInterstitialLoaded {
    interstitial.show()
  }
}

Objective-C

- (void)presentFromViewController:(UIViewController *)viewController {
  if ([_interstitialAd isInterstitialLoaded]) {
    [_interstitialAd show];
  } else {
    NSError *error = SampleCustomEventErrorWithCodeAndDescription(
        SampleCustomEventErrorAdNotLoaded,
        [NSString stringWithFormat:@"The interstitial ad failed to present "
                                   @"because the ad was not loaded."]);
    [_adEventDelegate didFailToPresentWithError:error]
  }
}

Google 모바일 광고 SDK로 미디에이션 이벤트 전달하기

일단 로드한 파일을 사용하여 GADMediationInterstitialLoadCompletionHandler 광고가 있으면 반환된 GADMediationInterstitialAdEventDelegate 대리자 객체를 통해 어댑터는 이 값을 사용하여 제3자의 프레젠테이션 이벤트를 Google 모바일 광고 SDK로 이전했습니다. SampleCustomEventInterstitial 클래스 SampleInterstitialAdDelegate 프로토콜을 구현하여 샘플 광고 네트워크를 Google 모바일 광고 SDK에 보냅니다.

맞춤 이벤트에서 이러한 콜백을 최대한 많이 전달하는 것이 중요합니다. 앱이 이와 동등한 이벤트를 Google 모바일 광고 SDK에 오신 것을 환영합니다. 다음은 콜백을 사용하는 예입니다.

Swift

func interstitialWillPresentScreen(_ interstitial: SampleInterstitial) {
  delegate?.willPresentFullScreenView()
  delegate?.reportImpression()
}

func interstitialWillDismissScreen(_ interstitial: SampleInterstitial) {
  delegate?.willDismissFullScreenView()
}

func interstitialDidDismissScreen(_ interstitial: SampleInterstitial) {
  delegate?.didDismissFullScreenView()
}

func interstitialWillLeaveApplication(_ interstitial: SampleInterstitial) {
  delegate?.reportClick()
}

Objective-C

- (void)interstitialWillPresentScreen:(SampleInterstitial *)interstitial {
  [_adEventDelegate willPresentFullScreenView];
  [_adEventDelegate reportImpression];
}

- (void)interstitialWillDismissScreen:(SampleInterstitial *)interstitial {
  [_adEventDelegate willDismissFullScreenView];
}

- (void)interstitialDidDismissScreen:(SampleInterstitial *)interstitial {
  [_adEventDelegate didDismissFullScreenView];
}

- (void)interstitialWillLeaveApplication:(SampleInterstitial *)interstitial {
  [_adEventDelegate reportClick];
}

이것으로 전면 광고용 맞춤 이벤트 구현이 완료되었습니다. 전체 예시를 보려면 GitHub 이미 지원되는 광고 네트워크와 함께 사용하거나 다음과 같이 수정할 수 있습니다. 맞춤 이벤트 전면 광고를 표시합니다.