इंटरस्टीशियल विज्ञापनों के कस्टम इवेंट

ज़रूरी शर्तें

कस्टम इवेंट सेटअप पूरा करें.

इंटरस्टीशियल विज्ञापन का अनुरोध करना

वॉटरफ़ॉल मीडिएशन चेन में, कस्टम इवेंट के लाइन आइटम तक पहुंचने पर, 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 प्रोटोकॉल लागू किया जा रहा है.

  • Google Mobile Ads SDK को विज्ञापन इवेंट कॉलबैक पाना और उनकी रिपोर्ट करना.

यह यूज़र इंटरफ़ेस (यूआई) में तय किया गया वैकल्पिक पैरामीटर है विज्ञापन कॉन्फ़िगरेशन में शामिल किया गया है. पैरामीटर को इसके ज़रिए ऐक्सेस किया जा सकता है 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 टूल, जिसे आपका अडैप्टर लागू करता है. उदाहरण के लिए, 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]
  }
}

Google Mobile Ads SDK पर मीडिएशन इवेंट फ़ॉरवर्ड करना

GADMediationInterstitialLoadCompletionHandler को कॉल करने के बाद लोड हो गया विज्ञापन के बाद, लौटाए गए GADMediationInterstitialAdEventDelegate डेलिगेट ऑब्जेक्ट फिर तीसरे पक्ष से प्रज़ेंटेशन इवेंट को फ़ॉरवर्ड करने के लिए, अडैप्टर का इस्तेमाल करें SDK टूल को Google Mobile Ads SDK से जोड़ा जा सकता है. SampleCustomEventInterstitial क्लास कॉलबैक फ़ॉरवर्ड करने के लिए, SampleInterstitialAdDelegate प्रोटोकॉल को लागू करता है सैंपल विज्ञापन नेटवर्क कंपनी को Google Mobile Ads SDK से जोड़ना होगा.

यह ज़रूरी है कि आपका कस्टम इवेंट इनमें से ज़्यादा से ज़्यादा कॉलबैक फ़ॉरवर्ड करे ताकि आपके ऐप्लिकेशन को Google की तरफ़ से इसी तरह के इवेंट मिल सकें मोबाइल विज्ञापन SDK टूल. यहां कॉलबैक का इस्तेमाल करने का एक उदाहरण दिया गया है:

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. इसका इस्तेमाल, पहले से काम करने वाली विज्ञापन नेटवर्क कंपनी के साथ किया जा सकता है. इसके अलावा, इसमें बदलाव भी किया जा सकता है कस्टम इवेंट मध्यवर्ती विज्ञापन दिखाना.