배너 광고 맞춤 이벤트

기본 요건

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

배너 광고 요청

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

배너 광고를 요청하려면 GADMediationAdapterloadBanner:adConfiguration:completionHandler:. 만약 GADMediationAdapter를 확장하는 클래스가 이미 있습니다. 이를 구현하세요. 저기 loadBanner:adConfiguration:completionHandler:. 또한 새 클래스를 사용하여 GADMediationBannerAd를 구현합니다.

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

Swift

import GoogleMobileAds

class SampleCustomEvent: NSObject, GADMediationAdapter {

  fileprivate var bannerAd: SampleCustomEventBanner?
  ...

  func loadBanner(
    for adConfiguration: GADMediationBannerAdConfiguration,
    completionHandler: @escaping GADMediationBannerLoadCompletionHandler
  ) {
    self.bannerAd = SampleCustomEventBanner()
    self.bannerAd?.loadBanner(
      for: adConfiguration, completionHandler: completionHandler)
  }
}

Objective-C

#import "SampleCustomEvent.h"

@implementation SampleCustomEvent
...

SampleCustomEventBanner *sampleBanner;

- (void)loadBannerForAdConfiguration:
            (GADMediationBannerAdConfiguration *)adConfiguration
                   completionHandler:(GADMediationBannerLoadCompletionHandler)
                                         completionHandler {
  sampleBanner = [[SampleCustomEventBanner alloc] init];
  [sampleBanner loadBannerForAdConfiguration:adConfiguration
                           completionHandler:completionHandler];
}

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

  • 배너 광고를 로드하고 GADMediationBannerLoadCompletionHandler 메서드를 한 번 완료되었을 수 있습니다.

  • GADMediationBannerAd 프로토콜 구현

  • Google 모바일 광고 SDK에 광고 이벤트 콜백 수신 및 보고

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

Swift

class SampleCustomEventBanner: NSObject, GADMediationBannerAd {
  /// The Sample Ad Network banner ad.
  var bannerAd: SampleBanner?

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

  /// Completion handler called after ad load
  var completionHandler: GADMediationBannerLoadCompletionHandler?

  func loadBanner(
    for adConfiguration: GADMediationBannerAdConfiguration,
    completionHandler: @escaping GADMediationBannerLoadCompletionHandler
  ) {
    // Create the bannerView with the appropriate size.
    let adSize = adConfiguration.adSize
    bannerAd = SampleBanner(
      frame: CGRect(x: 0, y: 0, width: adSize.size.width, height: adSize.size.height))
    bannerAd?.delegate = self
    bannerAd?.adUnit = adConfiguration.credentials.settings["parameter"] as? String
    let adRequest = SampleAdRequest()
    adRequest.testMode = adConfiguration.isTestRequest
    self.completionHandler = completionHandler
    bannerAd?.fetchAd(adRequest)
  }
}

Objective-C

#import "SampleCustomEventBanner.h"

@interface SampleCustomEventBanner () <SampleBannerAdDelegate,
                                       GADMediationBannerAd> {
  /// The sample banner ad.
  SampleBanner *_bannerAd;

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

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

@implementation SampleCustomEventBanner

- (void)loadBannerForAdConfiguration:
            (GADMediationBannerAdConfiguration *)adConfiguration
                   completionHandler:(GADMediationBannerLoadCompletionHandler)
                                         completionHandler {
  __block atomic_flag completionHandlerCalled = ATOMIC_FLAG_INIT;
  __block GADMediationBannerLoadCompletionHandler originalCompletionHandler =
      [completionHandler copy];

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

    id<GADMediationBannerAdEventDelegate> 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"];
  _bannerAd = [[SampleBanner alloc]
      initWithFrame:CGRectMake(0, 0, adConfiguration.adSize.size.width,
                               adConfiguration.adSize.size.height)];
  _bannerAd.adUnit = adUnit;
  _bannerAd.delegate = self;
  SampleAdRequest *adRequest = [[SampleAdRequest alloc] init];
  adRequest.testMode = adConfiguration.isTestRequest;
  [_bannerAd fetchAd:adRequest];
}

광고를 성공적으로 가져왔거나 오류가 발생한 경우 GADMediationBannerLoadCompletionHandler를 호출합니다. 성공하면 nil 값으로 GADMediationBannerAd를 구현하는 클래스를 전달합니다. 오류 매개변수의 경우 실패할 경우 사용자가 보고 있는 발생할 수 있습니다

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

Swift

func bannerDidLoad(_ banner: SampleBanner) {
  if let handler = completionHandler {
    delegate = handler(self, nil)
  }
}

func banner(
  _ banner: SampleBanner, 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)bannerDidLoad:(SampleBanner *)banner {
  _adEventDelegate = _loadCompletionHandler(self, nil);
}

- (void)banner:(SampleBanner *)banner
    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);
}

GADMediationBannerAdUIView 속성을 구현해야 합니다.

Swift

var view: UIView {
  return bannerAd ?? UIView()
}

Objective-C

- (nonnull UIView *)view {
  return _bannerAd;
}

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

로드된 광고로 GADMediationBannerLoadCompletionHandler를 호출한 후에는 그런 다음 반환된 GADMediationBannerAdEventDelegate 위임 객체는 어댑터가 사용하여 제3자 SDK의 프레젠테이션 이벤트를 Google 모바일 광고 SDK에 대해 살펴보았습니다 SampleCustomEventBanner 클래스는 샘플 광고의 콜백을 전달하는 SampleBannerAdDelegate 프로토콜 네트워크를 Google 모바일 광고 SDK에 연결하는 것입니다.

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

Swift

func bannerWillLeaveApplication(_ banner: SampleBanner) {
  delegate?.reportClick()
}

Objective-C

- (void)bannerWillLeaveApplication:(SampleBanner *)banner {
  [_adEventDelegate reportClick];
}

배너 광고용 맞춤 이벤트 구현이 완료되었습니다. 전체 예 사용 가능 날짜: GitHub 이미 지원되는 광고 네트워크와 함께 사용하거나 다음과 같이 수정할 수 있습니다. 맞춤 이벤트 배너 광고를 게재할 수 있습니다.