पेज पर अचानक दिखने वाले विज्ञापनों के कस्टम इवेंट

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

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

पेज पर अचानक दिखने वाले विज्ञापन का अनुरोध करना

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

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

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

आम तौर पर, इन तरीकों को तीसरे पक्ष के SDK टूल से कॉलबैक में लागू किया जाता है. यह 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 क्लास, सैंपल विज्ञापन नेटवर्क से कॉलबैक को, Google Mobile Ads SDK पर फ़ॉरवर्ड करने के लिए, SampleInterstitialAdDelegate प्रोटोकॉल लागू करती है.

आपके कस्टम इवेंट से ज़्यादा से ज़्यादा कॉलबैक फ़ॉरवर्ड करने चाहिए, ताकि आपके ऐप्लिकेशन को Google Mobile Ads 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 पर उपलब्ध है. इसे पहले से काम करने वाले किसी विज्ञापन नेटवर्क के साथ इस्तेमाल किया जा सकता है. इसके अलावा, कस्टम इवेंट पर अचानक दिखने वाले विज्ञापनों को दिखाने के लिए इसमें बदलाव किया जा सकता है.