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

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

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

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

เมื่อถึงรายการโฆษณาเหตุการณ์ที่กําหนดเองในห่วงโซ่การแสดงโฆษณาสื่อกลางตามลําดับขั้น ระบบจะเรียกใช้เมธอด 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

  • รับและรายงานการเรียกกลับของเหตุการณ์โฆษณาไปยัง SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google

พารามิเตอร์ที่ไม่บังคับที่กำหนดไว้ใน 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 the 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 สําหรับพารามิเตอร์ข้อผิดพลาด ในกรณีที่ล้มเหลว ให้ส่งผ่านข้อผิดพลาดที่คุณพบ

โดยปกติแล้ว วิธีการเหล่านี้จะได้รับการติดตั้งใช้งานภายในโค้ดเรียกกลับจาก 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]
  }
}

ส่งต่อเหตุการณ์สื่อกลางไปยัง SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google

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

คุณควรส่งต่อเหตุการณ์ที่กําหนดเองเป็น Callback เหล่านี้ให้มากที่สุด เพื่อให้แอปได้รับเหตุการณ์ที่เทียบเท่าจาก SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google ตัวอย่างการใช้ฟังก์ชันเรียกกลับ

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 คุณสามารถใช้กับเครือข่ายโฆษณาที่รองรับอยู่แล้ว หรือแก้ไขเพื่อ แสดงโฆษณาคั่นระหว่างหน้าของเหตุการณ์ที่กำหนดเอง