เหตุการณ์ที่กำหนดเองสำหรับโฆษณาคั่นระหว่างหน้า

เลือกแพลตฟอร์ม: Android iOS Android (เดิม)

ข้อกำหนดเบื้องต้น

ทำการตั้งค่าเหตุการณ์ที่กำหนดเองให้เสร็จสมบูรณ์

ส่งคำขอโฆษณาคั่นระหว่างหน้า

เมื่อรายการโฆษณาเหตุการณ์ที่กำหนดเองไปถึงเชนการแสดงโฆษณาสื่อกลางตามลำดับขั้น (Waterfall Mediation) ระบบจะเรียกใช้เมธอด loadInterstitial:adConfiguration:completionHandler: ในชื่อคลาสที่คุณระบุไว้เมื่อสร้างเหตุการณ์ที่กำหนดเอง ในกรณีนี้ เมธอดดังกล่าวอยู่ใน SampleCustomEvent ซึ่งจะเรียกใช้ เมธอด loadInterstitial:adConfiguration:completionHandler: ใน SampleCustomEventInterstitial

หากต้องการขอโฆษณาคั่นระหว่างหน้า ให้สร้างหรือแก้ไขคลาสที่ใช้ GADMediationAdapter และ loadInterstitial:adConfiguration:completionHandler: หากมีคลาสที่ขยาย GADMediationAdapter อยู่แล้ว ให้ใช้ loadInterstitial:adConfiguration:completionHandler: ในคลาสนั้น นอกจากนี้ ให้สร้างคลาสใหม่เพื่อใช้ GADMediationInterstitialAd

ในตัวอย่างเหตุการณ์ที่กำหนดเอง SampleCustomEvent จะใช้อินเทอร์เฟซ GADMediationAdapter แล้วมอบสิทธิ์ให้ SampleCustomEventInterstitial

Swift

import GoogleMobileAds

class SampleCustomEvent: NSObject, MediationAdapter {

  fileprivate var interstitialAd: SampleCustomEventInterstitial?
  ...

  func loadInterstitial(
    for adConfiguration: MediationInterstitialAdConfiguration,
    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 มีหน้าที่รับผิดชอบงานต่อไปนี้

  • การโหลดโฆษณาคั่นระหว่างหน้าและการเรียกใช้เมธอด GADMediationInterstitialAdLoadCompletionHandler เมื่อโหลดเสร็จสมบูรณ์

  • กำลังใช้โปรโตคอล GADMediationInterstitialAd

  • การรับและรายงาน Callback ของเหตุการณ์โฆษณาไปยัง Google Mobile Ads SDK

พารามิเตอร์ที่ไม่บังคับซึ่งกำหนดไว้ใน UI จะรวมอยู่ในการกำหนดค่าโฆษณา คุณเข้าถึงพารามิเตอร์ได้ผ่าน adConfiguration.credentials.settings[@"parameter"] โดยทั่วไปพารามิเตอร์นี้จะเป็นตัวระบุหน่วยโฆษณาที่ SDK เครือข่ายโฆษณาต้องการเมื่อสร้างอินสแตนซ์ออบเจ็กต์โฆษณา

Swift

import GoogleMobileAds

class SampleCustomEventInterstitial: NSObject, MediationInterstitialAd {
  /// The Sample Ad Network interstitial ad.
  var interstitial: SampleInterstitial?

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

  var completionHandler: GADMediationInterstitialLoadCompletionHandler?

  func loadInterstitial(
    for adConfiguration: MediationInterstitialAdConfiguration,
    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];
}

ไม่ว่าการดึงข้อมูลโฆษณาจะสำเร็จหรือพบข้อผิดพลาด คุณจะต้องเรียกใช้ GADMediationInterstitialLoadCompletionHandler ในกรณีที่ สำเร็จ ให้ส่งผ่านคลาสที่ใช้ GADMediationInterstitialAd โดยมีค่า nil สำหรับพารามิเตอร์ข้อผิดพลาด ในกรณีที่ล้มเหลว ให้ส่งผ่าน ข้อผิดพลาดที่คุณพบ

โดยปกติแล้ว เมธอดเหล่านี้จะใช้ภายใน Callback จาก SDK ของบุคคลที่สามที่อะแดปเตอร์ของคุณใช้ สำหรับตัวอย่างนี้ SDK ตัวอย่างจะมี SampleInterstitialAdDelegate พร้อม Callback ที่เกี่ยวข้อง ดังนี้

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 ต้องใช้เมธอด present เพื่อแสดงโฆษณา

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

ส่งต่อเหตุการณ์สื่อกลางไปยัง Google Mobile Ads SDK

เมื่อคุณเรียกใช้ GADMediationInterstitialLoadCompletionHandler ด้วยโฆษณาที่โหลดแล้ว อะแดปเตอร์จะใช้ออบเจ็กต์ผู้รับมอบสิทธิ์ GADMediationInterstitialAdEventDelegate ที่แสดงผลเพื่อส่งต่อเหตุการณ์การนำเสนอจาก SDK ของบุคคลที่สามไปยัง Google Mobile Ads SDK ได้ คลาส SampleCustomEventInterstitial ใช้โปรโตคอล SampleInterstitialAdDelegate เพื่อส่งต่อ Callback จากเครือข่ายโฆษณาตัวอย่างไปยัง Google Mobile Ads SDK

เหตุการณ์ที่กำหนดเองควรส่งต่อ Callback เหล่านี้ให้มากที่สุดเท่าที่จะเป็นไปได้ เพื่อให้แอปได้รับเหตุการณ์ที่เทียบเท่าเหล่านี้จาก Google Mobile Ads SDK ตัวอย่างการใช้ 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];
}

เพียงเท่านี้ก็เสร็จสิ้นการติดตั้งใช้งานเหตุการณ์ที่กำหนดเองสำหรับโฆษณาคั่นระหว่างหน้า ดูตัวอย่างแบบเต็มได้ใน GitHub คุณสามารถใช้กับเครือข่ายโฆษณาที่รองรับอยู่แล้วหรือแก้ไขเพื่อแสดงโฆษณาคั่นระหว่างหน้าของเหตุการณ์ที่กำหนดเอง