前提条件
カスタム イベントの設定を完了します。
バナー広告をリクエストする
ウォーターフォール メディエーション チェーンでカスタム イベント広告申込情報に達すると、カスタム イベントの作成時に指定したクラス名でthe loadBanner:adConfiguration:completionHandler:
method が呼び出されます。この場合、このメソッドは SampleCustomEvent
にあり、 SampleCustomEventBanner
のthe loadBanner:adConfiguration:completionHandler:
method を呼び出します。
バナー広告をリクエストするには、GADMediationAdapter
と loadBanner:adConfiguration:completionHandler:
を実装するクラスを作成または変更します。GADMediationAdapter
を拡張するクラスがすでに存在する場合は、そこに loadBanner:adConfiguration:completionHandler:
を実装します。さらに、GADMediationBannerAd
を実装するための新しいクラスを作成します。
このカスタム イベントの例では、SampleCustomEvent
がthe GADMediationAdapter
interface を実装し、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
method を呼び出すGADMediationBannerAd
protocolの実装広告イベント コールバックを受信して Google Mobile Ads SDK に報告する
UI で定義されたオプション パラメータは、 Ad Manager 広告設定に含まれます。このパラメータには adConfiguration.credentials.settings[@"parameter"]
からアクセスできます。このパラメータは通常、広告ネットワーク SDK が広告オブジェクトをインスタンス化する際に必要とする広告ユニット ID です。
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
を実装するクラスを渡し、error パラメータの値に nil
を指定します。失敗した場合は、発生したエラーを渡します。
通常、これらのメソッドは、アダプタが実装するサードパーティ SDK のコールバック内に実装されます。この例では、関連するコールバックを含む SampleBannerAdDelegate
がサンプル SDK に含まれています。
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 で入手できます。すでにサポートされている広告ネットワークで使用できます。または、これを変更してカスタム イベントのバナー広告を表示できます。