Peristiwa kustom iklan interstisial

Prasyarat

Selesaikan penyiapan peristiwa kustom.

Meminta iklan interstisial

Saat item baris peristiwa kustom dicapai dalam rantai mediasi waterfall, metode loadInterstitial:adConfiguration:completionHandler: akan dipanggil pada nama class yang Anda berikan saat membuat peristiwa kustom. Dalam hal ini, metode tersebut berada di SampleCustomEvent, yang kemudian memanggil metode loadInterstitial:adConfiguration:completionHandler: di SampleCustomEventInterstitial.

Untuk meminta iklan interstisial, buat atau ubah class yang mengimplementasikan GADMediationAdapter dan loadInterstitial:adConfiguration:completionHandler:. Jika class yang memperluas GADMediationAdapter sudah ada, terapkan loadInterstitial:adConfiguration:completionHandler: di sana. Selain itu, buat class baru untuk menerapkan GADMediationInterstitialAd.

Dalam contoh peristiwa kustom, SampleCustomEvent mengimplementasikan antarmuka GADMediationAdapter, lalu mendelegasikan ke 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 bertanggung jawab atas tugas berikut:

  • Memuat iklan interstisial dan memanggil metode GADMediationInterstitialAdLoadCompletionHandler setelah pemuatan selesai.

  • Mengimplementasikan protokol GADMediationInterstitialAd.

  • Menerima dan melaporkan callback peristiwa iklan ke Google Mobile Ads SDK.

Parameter opsional yang ditentukan di UI disertakan dalam konfigurasi iklan. Parameter ini dapat diakses melalui adConfiguration.credentials.settings[@"parameter"]. Parameter ini biasanya adalah ID unit iklan yang diperlukan SDK jaringan iklan saat membuat instance objek iklan.

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

Baik iklan berhasil diambil atau mengalami error, Anda akan memanggil GADMediationInterstitialLoadCompletionHandler. Jika berhasil, teruskan class yang mengimplementasikan GADMediationInterstitialAd dengan nilai nil untuk parameter error; jika gagal, teruskan error yang Anda temui.

Biasanya, metode ini diterapkan di dalam callback dari SDK pihak ketiga yang diterapkan adaptor Anda. Untuk contoh ini, Sample SDK memiliki SampleInterstitialAdDelegate dengan callback yang relevan:

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 memerlukan penerapan metode present untuk menampilkan iklan:

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

Meneruskan peristiwa mediasi ke Google Mobile Ads SDK

Setelah Anda memanggil GADMediationInterstitialLoadCompletionHandler dengan iklan yang dimuat, objek delegasi GADMediationInterstitialAdEventDelegate yang ditampilkan kemudian dapat digunakan oleh adaptor untuk meneruskan peristiwa presentasi dari SDK pihak ketiga ke Google Mobile Ads SDK. Class SampleCustomEventInterstitial menerapkan protokol SampleInterstitialAdDelegate untuk meneruskan callback dari contoh jaringan iklan ke Google Mobile Ads SDK.

Peristiwa kustom Anda harus meneruskan callback ini sebanyak mungkin, sehingga aplikasi Anda menerima peristiwa yang setara ini dari Google Mobile Ads SDK. Berikut adalah contoh penggunaan callback:

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

Ini melengkapi implementasi peristiwa kustom untuk iklan interstisial. Contoh lengkap tersedia di GitHub. Anda dapat menggunakannya dengan jaringan iklan yang sudah didukung atau mengubahnya untuk menampilkan iklan interstisial peristiwa kustom.