前提条件
カスタム イベントの設定を完了します。
バナー広告をリクエストする
ウォーターフォール メディエーション チェーンでカスタム イベント広告申込情報に到達すると、
loadBanner:adConfiguration:completionHandler:
メソッドは
カスタム ディメンションの作成時に指定したクラス名
イベントをご覧ください。この例では
このメソッドは SampleCustomEvent
にあり、
loadBanner:adConfiguration:completionHandler:
メソッドを
SampleCustomEventBanner
。
バナー広告をリクエストするには、次を実装するクラスを作成または変更します:
GADMediationAdapter
と loadBanner:adConfiguration:completionHandler:
。もし
GADMediationAdapter
を拡張するクラスがすでに存在するため、実装します。
loadBanner:adConfiguration:completionHandler:
。さらに
GADMediationBannerAd
を実装する新しいクラス。
このカスタムイベントの例では
SampleCustomEvent
の実装
GADMediationAdapter
インターフェースを呼び出してから、
SampleCustomEventBanner
。
Swift
import GoogleMobileAds class SampleCustomEvent: NSObject, GADMediationAdapter { fileprivate var bannerAd: SampleCustomEventBanner? ... func loadBanner( for adConfiguration: GADMediationBannerAdConfiguration, completionHandler: @escaping GADMediationBannerLoadCompletionHandler ) { self.bannerAd = SampleCustomEventBanner() self.bannerAd?.loadBanner( for: adConfiguration, completionHandler: completionHandler) } }
Objective-C
#import "SampleCustomEvent.h" @implementation SampleCustomEvent ... SampleCustomEventBanner *sampleBanner; - (void)loadBannerForAdConfiguration: (GADMediationBannerAdConfiguration *)adConfiguration completionHandler:(GADMediationBannerLoadCompletionHandler) completionHandler { sampleBanner = [[SampleCustomEventBanner alloc] init]; [sampleBanner loadBannerForAdConfiguration:adConfiguration completionHandler:completionHandler]; }
SampleCustomEventBanner
は次のタスクを行います。
バナー広告の読み込みと
GADMediationBannerLoadCompletionHandler
メソッドを 1 回 読み込みが完了しました。GADMediationBannerAd
プロトコルの実装広告イベント コールバックを受信して Google Mobile Ads SDK に報告する
AdMob 管理画面で定義されているオプションのパラメータは次のとおりです。
広告設定に含まれています
このパラメータには、
adConfiguration.credentials.settings[@"parameter"]
。このパラメータは、
通常は広告ユニット識別子です
広告オブジェクトをインスタンス化します
Swift
class SampleCustomEventBanner: NSObject, GADMediationBannerAd { /// The Sample Ad Network banner ad. var bannerAd: SampleBanner? /// The ad event delegate to forward ad rendering events to the Google Mobile Ads SDK. var delegate: GADMediationBannerAdEventDelegate? /// Completion handler called after ad load var completionHandler: GADMediationBannerLoadCompletionHandler? func loadBanner( for adConfiguration: GADMediationBannerAdConfiguration, completionHandler: @escaping GADMediationBannerLoadCompletionHandler ) { // Create the bannerView with the appropriate size. let adSize = adConfiguration.adSize bannerAd = SampleBanner( frame: CGRect(x: 0, y: 0, width: adSize.size.width, height: adSize.size.height)) bannerAd?.delegate = self bannerAd?.adUnit = adConfiguration.credentials.settings["parameter"] as? String let adRequest = SampleAdRequest() adRequest.testMode = adConfiguration.isTestRequest self.completionHandler = completionHandler bannerAd?.fetchAd(adRequest) } }
Objective-C
#import "SampleCustomEventBanner.h" @interface SampleCustomEventBanner () <SampleBannerAdDelegate, GADMediationBannerAd> { /// The sample banner ad. SampleBanner *_bannerAd; /// The completion handler to call when the ad loading succeeds or fails. GADMediationBannerLoadCompletionHandler _loadCompletionHandler; /// The ad event delegate to forward ad rendering events to the Google Mobile /// Ads SDK. id <GADMediationBannerAdEventDelegate> _adEventDelegate; } @end @implementation SampleCustomEventBanner - (void)loadBannerForAdConfiguration: (GADMediationBannerAdConfiguration *)adConfiguration completionHandler:(GADMediationBannerLoadCompletionHandler) completionHandler { __block atomic_flag completionHandlerCalled = ATOMIC_FLAG_INIT; __block GADMediationBannerLoadCompletionHandler originalCompletionHandler = [completionHandler copy]; _loadCompletionHandler = ^id<GADMediationBannerAdEventDelegate>( _Nullable id<GADMediationBannerAd> ad, NSError *_Nullable error) { // Only allow completion handler to be called once. if (atomic_flag_test_and_set(&completionHandlerCalled)) { return nil; } id<GADMediationBannerAdEventDelegate> 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"]; _bannerAd = [[SampleBanner alloc] initWithFrame:CGRectMake(0, 0, adConfiguration.adSize.size.width, adConfiguration.adSize.size.height)]; _bannerAd.adUnit = adUnit; _bannerAd.delegate = self; SampleAdRequest *adRequest = [[SampleAdRequest alloc] init]; adRequest.testMode = adConfiguration.isTestRequest; [_bannerAd fetchAd:adRequest]; }
広告が正常に取得された場合も、エラーが発生した場合も、
GADMediationBannerLoadCompletionHandler
を呼び出します。成功した場合
GADMediationBannerAd
を実装しているクラスを nil
値で渡す
error パラメータに指定します。前のステップで使用したエラーを
発生します。
通常、これらのメソッドは
実装する必要があります。この例では、サンプル SDK が
関連するコールバックを含む SampleBannerAdDelegate
がある
Swift
func bannerDidLoad(_ banner: SampleBanner) { if let handler = completionHandler { delegate = handler(self, nil) } } func banner( _ banner: SampleBanner, 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)bannerDidLoad:(SampleBanner *)banner { _adEventDelegate = _loadCompletionHandler(self, nil); } - (void)banner:(SampleBanner *)banner 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); }
GADMediationBannerAd
には UIView
プロパティを実装する必要があります。
Swift
var view: UIView { return bannerAd ?? UIView() }
Objective-C
- (nonnull UIView *)view { return _bannerAd; }
メディエーション イベントを Google Mobile Ads SDK に転送する
読み込まれた広告から GADMediationBannerLoadCompletionHandler
を呼び出すと、
返された GADMediationBannerAdEventDelegate
委譲オブジェクトは、
プレゼンテーション イベントをサードパーティ SDK から
Google Mobile Ads SDK を使用しますSampleCustomEventBanner
クラスは、
サンプル広告からコールバックを転送する SampleBannerAdDelegate
プロトコル
Google Mobile Ads SDK に接続します
カスタム イベントは、できるだけ多くのコールバックを これにより、アプリはこれらの同等のイベントを Google Mobile Ads SDK があります。コールバックの使用例を次に示します。
Swift
func bannerWillLeaveApplication(_ banner: SampleBanner) { delegate?.reportClick() }
Objective-C
- (void)bannerWillLeaveApplication:(SampleBanner *)banner { [_adEventDelegate reportClick]; }
これで、バナー広告のカスタム イベントの実装が完了しました。サンプル全体 は GitHub。 すでにサポートされている広告ネットワークで使用するか、 カスタム イベントのバナー広告を表示します。