ইন্টারস্টিশিয়াল বিজ্ঞাপন কাস্টম ইভেন্ট

প্ল্যাটফর্ম বেছে নিন: Android iOS Android (Legacy)

পূর্বশর্ত

কাস্টম ইভেন্ট সেট-আপ সম্পূর্ণ করুন।

ইন্টারস্টিশিয়াল বিজ্ঞাপনের অনুরোধ করা

ওয়াটারফল মধ্যস্থতা চেনে কাস্টম ইভেন্ট লাইন আইটেমে পৌঁছালে, কাস্টম ইভেন্ট তৈরি করার সময় আপনার দেওয়া ক্লাস নামের উপর loadInterstitial:adConfiguration:completionHandler: পদ্ধতি কল করা হয়। এই ক্ষেত্রে, সেই পদ্ধতিটি SampleCustomEvent-এ আছে, যা তারপরে SampleCustomEventInterstitial-এ থাকা loadInterstitial:adConfiguration:completionHandler: পদ্ধতিকে কল করে।

ইন্টারস্টিশিয়াল বিজ্ঞাপন অনুরোধ করতে, এমন ক্লাস তৈরি বা পরিবর্তন করুন যা 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 প্রোটোকল প্রয়োগ করা হচ্ছে।

  • Google Mobile Ads SDK-এ বিজ্ঞাপন ইভেন্ট কলব্যাক পাওয়া ও রিপোর্ট করা।

UI-তে সংজ্ঞায়িত ঐচ্ছিক প্যারামিটার বিজ্ঞাপন কনফিগারেশনে অন্তর্ভুক্ত করা হয়েছে। প্যারামিটারটি 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 ভ্যালু দিন; ব্যর্থ হলে, আপনার হওয়া ত্রুটি থেকে পাস করুন।

সাধারণত, আপনার অ্যাডাপ্টার প্রয়োগ করা থার্ড-পার্টি 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]
  }
}

Google Mobile Ads SDK-এ মধ্যস্থতা ইভেন্ট ফরওয়ার্ড করা

আপনি লোড করা বিজ্ঞাপন সহ GADMediationInterstitialLoadCompletionHandler কল করার পরে, রিটার্ন করা GADMediationInterstitialAdEventDelegate ডেলিগেট অবজেক্টটি তারপরে থার্ড-পার্টি SDK থেকে Google Mobile Ads SDK-এ প্রেজেন্টেশন ইভেন্ট ফরওয়ার্ড করার জন্য অ্যাডাপ্টার ব্যবহার করতে পারে। SampleCustomEventInterstitial ক্লাস SampleInterstitialAdDelegate প্রোটোকল প্রয়োগ করে, যাতে স্যাম্পেল বিজ্ঞাপন নেটওয়ার্ক থেকে Google Mobile Ads SDK-এ কলব্যাক ফরওয়ার্ড করা যায়।

আপনার কাস্টম ইভেন্ট যেন এইসব কলব্যাক যতটা সম্ভব ফরওয়ার্ড করে, তা নিশ্চিত করা গুরুত্বপূর্ণ। এর ফলে আপনার অ্যাপ 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-এ উপলভ্য। আপনি এটি আগে থেকেই কাজ করে এমন বিজ্ঞাপন নেটওয়ার্কের সাথে ব্যবহার করতে পারেন অথবা কাস্টম ইভেন্ট ইন্টারস্টিশিয়াল বিজ্ঞাপন দেখানোর জন্য এটি পরিবর্তন করতে পারেন।