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

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

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

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

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

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

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

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

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

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

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

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