横幅广告自定义事件

前提条件

完成自定义事件设置

请求横幅广告

当到达广告瀑布流中介链中的自定义事件订单项时,系统将针对您在创建自定义事件时提供的类名称调用the loadBanner:adConfiguration:completionHandler: method 。在本例中,该方法位于 SampleCustomEvent 中,后者随后会在 SampleCustomEventBanner中调用the loadBanner:adConfiguration:completionHandler: method 。

如需请求横幅广告,请创建或修改实现 GADMediationAdapterloadBanner:adConfiguration:completionHandler: 的类。如果已存在扩展 GADMediationAdapter 的类,请在该类中实现 loadBanner:adConfiguration:completionHandler:。此外,请创建一个新类来实现 GADMediationBannerAd

在我们的自定义事件示例中,SampleCustomEvent 会实现the GADMediationAdapter interface ,然后委托给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 method

  • 实现 GADMediationBannerAd protocol

  • 接收广告事件回调并将其报告给 Google 移动广告 SDK

AdMob 界面中定义的可选参数会作为 the loadBanner:adConfiguration:completionHandler: method的一部分传递到自定义事件中。您可以通过 adConfiguration.credentials.settings[@"parameter"] 访问该参数。通常,此参数是实例化广告对象时广告联盟 SDK 所需的广告单元标识符。

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。如果成功,请传递实现 GADMediationBannerAd 的类,其中错误参数的值为 nil;如果失败,请传递您遇到的错误。

通常,这些方法是在适配器实现的第三方 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);
}

GADMediationBannerAd 需要实现 UIView 属性:

Swift

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

Objective-C

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

将中介事件转发到 Google 移动广告 SDK

使用已加载的广告调用 GADMediationBannerLoadCompletionHandler 后,适配器便可以使用返回的 GADMediationBannerAdEventDelegate 委托对象,将展示事件从第三方 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 上提供了完整的示例。您可以将其用于已支持的广告联盟,也可以对其进行修改以展示自定义事件横幅广告。