前提条件
カスタム イベントのセットアップを完了しておきます。
インタースティシャル広告をリクエストする
ウォーターフォール メディエーション チェーンでカスタム イベントの広告申込情報に回ってくると、
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 が要求する広告ユニット
ID です。
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 のコールバック内に実装されます。この例では、関連するコールバックを含む SampleInterstitialAdDelegate が「Sample SDK」に含まれています。
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 に転送できます。The 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 で入手できます。このサンプルは、すでにサポートされている広告ネットワークの実装例として利用できるほか、必要に応じてコードをカスタマイズし、独自のインタースティシャル広告用カスタム イベントを表示するために利用することも可能です。