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

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

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

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

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

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

ในตัวอย่างเหตุการณ์ที่กำหนดเอง SampleCustomEvent ใช้งานthe GADMediationAdapter interface แล้วมอบสิทธิ์ให้กับ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 มีหน้าที่ดังต่อไปนี้

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

  • การใช้ GADMediationInterstitialAd protocol

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

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

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

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

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

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

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