Sự kiện tuỳ chỉnh cho quảng cáo xen kẽ

Điều kiện tiên quyết

Hoàn tất quy trình thiết lập sự kiện tuỳ chỉnh.

Yêu cầu quảng cáo xen kẽ

Khi đạt đến mục hàng sự kiện tuỳ chỉnh trong chuỗi dàn xếp kiểu thác nước, phương thức loadInterstitial:adConfiguration:completionHandler: sẽ được gọi theo tên lớp mà bạn đã cung cấp khi tạo một sự kiện tuỳ chỉnh. Trong trường hợp này, phương thức đó nằm trong SampleCustomEvent, sau đó phương thức này sẽ gọi phương thức loadInterstitial:adConfiguration:completionHandler: trong SampleCustomEventInterstitial.

Để yêu cầu một quảng cáo xen kẽ, hãy tạo hoặc sửa đổi một lớp triển khai GADMediationAdapterloadInterstitial:adConfiguration:completionHandler:. Nếu đã tồn tại một lớp mở rộng GADMediationAdapter, hãy triển khai loadInterstitial:adConfiguration:completionHandler: ở đó. Ngoài ra, tạo một lớp mới để triển khai GADMediationInterstitialAd.

Trong ví dụ về sự kiện tuỳ chỉnh của chúng tôi, SampleCustomEvent sẽ triển khai giao diện GADMediationAdapter và sau đó uỷ quyền cho 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 chịu trách nhiệm thực hiện những việc sau:

  • Tải quảng cáo xen kẽ và gọi phương thức GADMediationInterstitialAdLoadCompletionHandler sau khi tải xong.

  • Triển khai giao thức GADMediationInterstitialAd.

  • Nhận và báo cáo lệnh gọi lại sự kiện quảng cáo cho SDK quảng cáo trên thiết bị di động của Google.

Thông số không bắt buộc được xác định trong giao diện người dùng sẽ được đưa vào cấu hình quảng cáo. Bạn có thể truy cập vào thông số này qua adConfiguration.credentials.settings[@"parameter"]. Thông số này là thông thường, giá trị nhận dạng đơn vị quảng cáo mà SDK mạng quảng cáo yêu cầu khi tạo thực thể của một đối tượng quảng cáo.

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];
}

Cho dù quảng cáo được tìm nạp thành công hay gặp lỗi, bạn sẽ gọi GADMediationInterstitialLoadCompletionHandler. Trong trường hợp thành công, hãy chuyển qua lớp triển khai GADMediationInterstitialAd có giá trị nil cho thông số lỗi; nếu không thành công, hãy truyền thông qua lỗi bạn gặp phải.

Thông thường, các phương thức này được triển khai bên trong lệnh gọi lại từ SDK bên thứ ba mà bộ chuyển đổi của bạn triển khai. Trong ví dụ này, SDK mẫu có một SampleInterstitialAdDelegate với các lệnh gọi lại phù hợp:

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);
}

GADMediationInterstitialAd yêu cầu triển khai một phương thức present để hiển thị quảng cáo:

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]
  }
}

Chuyển tiếp sự kiện dàn xếp đến SDK quảng cáo trên thiết bị di động của Google

Sau khi bạn gọi GADMediationInterstitialLoadCompletionHandler bằng một thuộc tính đã tải thì đối tượng uỷ quyền GADMediationInterstitialAdEventDelegate được trả về có thể sau đó sẽ được bộ chuyển đổi sử dụng để chuyển tiếp các sự kiện trình bày từ bên thứ ba SDK cho SDK Quảng cáo của Google trên thiết bị di động. Lớp SampleCustomEventInterstitial triển khai giao thức SampleInterstitialAdDelegate để chuyển tiếp lệnh gọi lại từ mạng quảng cáo mẫu đến SDK quảng cáo trên thiết bị di động của Google.

Điều quan trọng là sự kiện tuỳ chỉnh của bạn phải chuyển tiếp càng nhiều lệnh gọi lại này càng tốt để ứng dụng của bạn có thể nhận được các sự kiện tương đương này từ SDK quảng cáo trên thiết bị di động của Google. Dưới đây là ví dụ về cách sử dụng lệnh gọi lại:

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];
}

Đến đây, bạn đã hoàn tất việc triển khai sự kiện tuỳ chỉnh cho quảng cáo xen kẽ. Bạn có thể xem toàn bộ ví dụ trên GitHub. Bạn có thể sử dụng đơn vị quảng cáo đó với một mạng quảng cáo đã được hỗ trợ hoặc sửa đổi đơn vị quảng cáo thành hiển thị quảng cáo xen kẽ sự kiện tùy chỉnh.