橫幅廣告自訂事件

必要條件

完成自訂事件設定

請求橫幅廣告

到達刊登序列中介服務鏈中的自訂事件委刊項時, 系統會呼叫 loadBanner:adConfiguration:completionHandler: 方法 您在建立自訂報表時提供的類別名稱 事件。在本例中 位於 SampleCustomEvent 中的方法,然後呼叫 中的 loadBanner:adConfiguration:completionHandler: 方法 SampleCustomEventBanner

如要請求橫幅廣告,請建立或修改導入 《GADMediationAdapter》和《loadBanner: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 Mobile Ads SDK 的廣告事件回呼

AdMob UI 中定義的選用參數是 加進廣告設定中 參數可以透過 adConfiguration.credentials.settings[@"parameter"]。這個參數是 通常是廣告聯播網 SDK 所需的廣告單元 ID 建立廣告物件的例項。

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。在這個範例中 具有包含相關回呼的 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);
}

GADMediationBannerAd 需要導入 UIView 屬性:

Swift

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

Objective-C

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

將中介服務事件轉送至 Google Mobile Ads SDK

透過已載入的廣告呼叫 GADMediationBannerLoadCompletionHandler 後, 傳回的 GADMediationBannerAdEventDelegate 委派物件則可能會 所使用的轉接程式,以便將第三方 SDK 的顯示事件轉送至 Google Mobile Ads SDK。SampleCustomEventBanner 類別會導入 從範例廣告轉送回呼的 SampleBannerAdDelegate 通訊協定 連結至 Google Mobile Ads SDK

請務必讓自訂事件盡可能順利轉送這些回呼,例如: ,讓應用程式從 Google Play 遊戲收到這些對等事件 Mobile Ads SDK以下是回呼的使用範例:

Swift

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

Objective-C

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

這樣就能完成橫幅廣告的自訂事件導入。完整範例 會在 GitHub。 您可以搭配目前支援的廣告聯播網使用,也可以將其修改為 顯示自訂事件橫幅廣告