পূর্বশর্ত
কাস্টম ইভেন্ট সেটআপ সম্পূর্ণ করুন।
একটি মধ্যবর্তী বিজ্ঞাপনের জন্য অনুরোধ করুন
ওয়াটারফল মিডিয়েশন চেইনে যখন কাস্টম ইভেন্ট লাইন আইটেমটিতে পৌঁছানো হয়, তখন কাস্টম ইভেন্ট তৈরি করার সময় আপনার দেওয়া ক্লাসের নামে loadInterstitial:adConfiguration:completionHandler: ` মেথডটি কল করা হয়। এই ক্ষেত্রে, সেই মেথডটি SampleCustomEvent এর মধ্যে রয়েছে, যা পরবর্তীতে SampleCustomEventInterstitial এর ` loadInterstitial:adConfiguration:completionHandler: ` মেথডটিকে কল করে।
একটি ইন্টারস্টিশিয়াল বিজ্ঞাপন অনুরোধ করতে, এমন একটি ক্লাস তৈরি বা পরিবর্তন করুন যা GADMediationAdapter ইমপ্লিমেন্ট করে এবং loadInterstitial:adConfiguration:completionHandler: ` ব্যবহার করে। যদি GADMediationAdapter এক্সটেন্ড করে এমন কোনো ক্লাস আগে থেকেই থাকে, তাহলে সেখানে loadInterstitial:adConfiguration:completionHandler: ` ইমপ্লিমেন্ট করুন। এছাড়াও, GADMediationInterstitialAd ইমপ্লিমেন্ট করার জন্য একটি নতুন ক্লাস তৈরি করুন।
আমাদের কাস্টম ইভেন্টের উদাহরণে, SampleCustomEvent প্রথমে GADMediationAdapter ইন্টারফেসটি ইমপ্লিমেন্ট করে এবং তারপর SampleCustomEventInterstitial কাছে দায়িত্ব অর্পণ করে।
সুইফট
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) } }
উদ্দেশ্য-সি
#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 একটি বিজ্ঞাপন অবজেক্ট ইনস্ট্যানশিয়েট করার সময় প্রয়োজন হয়।
সুইফট
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() } } }
উদ্দেশ্য-সি
#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 কল করতে হবে। সফল হলে, error প্যারামিটারের জন্য nil মান সহ GADMediationInterstitialAd ইমপ্লিমেন্টকারী ক্লাসটি পাস করুন; ব্যর্থ হলে, আপনার সম্মুখীন হওয়া ত্রুটিটি পাস করুন।
সাধারণত, এই মেথডগুলো আপনার অ্যাডাপ্টার দ্বারা বাস্তবায়িত থার্ড-পার্টি SDK-এর কলব্যাকের ভিতরে প্রয়োগ করা হয়। এই উদাহরণের জন্য, স্যাম্পল SDK-তে প্রাসঙ্গিক কলব্যাকসহ একটি SampleInterstitialAdDelegate রয়েছে:
সুইফট
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) } }
উদ্দেশ্য-সি
- (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 পদ্ধতি প্রয়োগ করা প্রয়োজন:
সুইফট
func present(from viewController: UIViewController) { if let interstitial = interstitial, interstitial.isInterstitialLoaded { interstitial.show() } }
উদ্দেশ্য-সি
- (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 থেকে এই সমতুল্য ইভেন্টগুলো গ্রহণ করতে পারে। কলব্যাক ব্যবহারের একটি উদাহরণ নিচে দেওয়া হলো:
সুইফট
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() }
উদ্দেশ্য-সি
- (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]; }
এর মাধ্যমে ইন্টারস্টিশিয়াল বিজ্ঞাপনের জন্য কাস্টম ইভেন্ট বাস্তবায়ন সম্পন্ন হলো। সম্পূর্ণ উদাহরণটি গিটহাবে পাওয়া যাবে। আপনি এটি আগে থেকেই সমর্থিত কোনো বিজ্ঞাপন নেটওয়ার্কের সাথে ব্যবহার করতে পারেন অথবা কাস্টম ইভেন্ট ইন্টারস্টিশিয়াল বিজ্ঞাপন দেখানোর জন্য এটিকে পরিবর্তন করতে পারেন।