אירועים בהתאמה אישית של מודעות מעברון

בחירת פלטפורמה: Android iOS Android (גרסה קודמת)

דרישות מוקדמות

משלימים את הגדרת האירועים המותאמים אישית.

בקשה למודעת מעברון

כשמגיעים לפריט של האירוע בהתאמה אישית בשרשרת רשימת הרשתות בתהליך בחירת רשת, השיטה loadInterstitial:adConfiguration:completionHandler: נקראת בשם המחלקה שציינתם כשיצרתם אירוע בהתאמה אישית. במקרה הזה, ה-method הזו נמצאת ב-SampleCustomEvent, שקוראת ל-method‏ 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.

הפרמטר האופציונלי שמוגדר בממשק המשתמש של נכלל בהגדרת המודעה. אפשר לגשת לפרמטר דרך 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 לפרמטר השגיאה. אם הפעולה נכשלה, מעבירים את השגיאה שנתקלתם בה.

בדרך כלל, השיטות האלה מוטמעות בתוך קריאות חוזרות (callbacks) מ-SDK של צד שלישי שהמתאם שלכם מטמיע. בדוגמה הזו, ל-Sample SDK יש SampleInterstitialAdDelegate עם קריאות חוזרות (callbacks) רלוונטיות:

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

העברת אירועים של תהליך בחירת הרשת (Mediation) אל Google Mobile Ads SDK

אחרי שמפעילים את GADMediationInterstitialLoadCompletionHandler עם מודעה שנטענה, המתאם יכול להשתמש באובייקט הנציג GADMediationInterstitialAdEventDelegate שמוחזר כדי להעביר אירועי הצגה מ-SDK של הצד השלישי אל Google Mobile Ads SDK. הסיווג SampleCustomEventInterstitial מטמיע את הפרוטוקול SampleInterstitialAdDelegate כדי להעביר קריאות חוזרות מרשת המודעות לדוגמה אל Google Mobile Ads SDK.

חשוב שהאירוע בהתאמה אישית יעביר כמה שיותר מהקריאות החוזרות האלה, כדי שהאפליקציה תקבל את האירועים המקבילים האלה מ-Google Mobile Ads SDK. דוגמה לשימוש בקריאות חוזרות (callbacks):

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. אפשר להשתמש בו עם רשת מודעות שכבר נתמכת או לשנות אותו כדי להציג מודעות מעברון של אירועים בהתאמה אישית.