メディエーション アダプタは、Google Mobile Ads SDK からメッセージとリクエストを受け取り、それらのリクエストを実行するために第三者ネットワーク SDK と通信します。
このガイドは、Google モバイル広告メディエーション用のリワード広告メディエーション アダプタの構築を検討している広告ネットワークを対象としています。以下のコード スニペットでは、デモ目的でサンプル SDK を使用しています。このサンプル SDK 用に作成されたアダプタの完全な実装は、iOSメディエーション プロジェクトで確認できます。このガイドでは、アダプタのビルド方法について説明します。
アダプタのクラス名とサーバー パラメータを定義する
AdMob メディエーション プラットフォームを介してメディエーションされる広告ネットワークでは通常、パブリッシャーを識別するために 1 つ以上の識別子が必要です。こうした識別子はサーバー パラメータとして表され、AdMob 管理画面でメディエーションで使用する第三者広告ネットワークを設定する際に定義されます。
メディエーション アダプタを開発する前に、アダプタのクラス名と、広告ネットワークへのアクセス権を取得するために必要なその他のパラメータを Google に提供する必要があります。
GADMediationAdapter プロトコルに準拠する
まず、アダプタクラスに GADMediationAdapter
プロトコルを実装します。
#import <Foundation/Foundation.h>
#import <GoogleMobileAds/GoogleMobileAds.h>
#import <SampleAdSDK/SampleAdSDK.h>
@interface GADMediationAdapterSampleAdNetwork : NSObject <GADMediationAdapter>
@end
この変更により、クラスに以下で説明するいくつかのメソッドが実装されるようになります。
エクストラのレポート クラス
第三者ネットワークでパブリッシャーが広告リクエストに追加のオプション パラメータを渡せるようにするには、networkExtrasClass
メソッドから extras
クラスを返す必要があります。サードパーティがパブリッシャー提供のエクストラをサポートしていない場合は、Nil
を返します。
#import <GoogleMobileAds/GoogleMobileAds.h>
@interface SampleExtras : NSObject<GADAdNetworkExtras>
/// Use this to indicate if debug mode is on for logging.
@property(nonatomic) BOOL debugLogging;
/// Use this to indicate whether to mute audio for video ads.
@property(nonatomic) BOOL muteAudio;
@end
#import "GADMediationAdapterSampleAdNetwork.h"
@implementation GADMediationAdapterSampleAdNetwork
...
+ (Class<GADAdNetworkExtras>)networkExtrasClass {
return [SampleExtras class];
}
...
@end
バージョン番号を報告する
アダプタは、アダプタ自体のバージョンと第三者 SDK のバージョンの両方を Google Mobile Ads SDK に報告する必要があります。バージョンは、 GADVersionNumber
を使用して報告されます。
Google のオープンソースおよびバージョン対応のアダプタは、4 桁のアダプタ バージョン スキームを使用しますが、 GADVersionNumber
使用できる数字は 3 桁のみです。この問題を回避するには、次に示すように、最後の 2 桁をパッチ バージョンにまとめることをおすすめします。
#import "GADMediationAdapterSampleAdNetwork.h"
@implementation GADMediationAdapterSampleAdNetwork
...
+ (GADVersionNumber)adSDKVersion {
NSString *versionString = SampleSDKVersion;
NSArray *versionComponents = [versionString componentsSeparatedByString:@"."];
GADVersionNumber version = {0};
if (versionComponents.count >= 3) {
version.majorVersion = [versionComponents[0] integerValue];
version.minorVersion = [versionComponents[1] integerValue];
version.patchVersion = [versionComponents[2] integerValue];
}
return version;
}
+ (GADVersionNumber)version {
NSString *versionString = SampleAdapterVersion;
NSArray *versionComponents = [versionString componentsSeparatedByString:@"."];
GADVersionNumber version = {0};
if (versionComponents.count >= 4) {
version.majorVersion = [versionComponents[0] integerValue];
version.minorVersion = [versionComponents[1] integerValue];
// Adapter versions have 2 patch versions. Multiply the first patch by 100.
version.patchVersion =
[versionComponents[2] integerValue] * 100 + [versionComponents[3] integerValue];
}
return version;
}
...
@end
アダプターを初期化する
アプリが Google Mobile Ads SDK を初期化すると、AdMob 管理画面で、そのアプリ用に設定されているすべてのアダプタで setUpWithConfiguration:completionHandler:
が呼び出されます。
GADMediationServerConfiguration
引数は、AdMob 管理画面でアプリ用に設定されているすべてのプレースメントに関する情報を提供します。この情報を使用して、広告ネットワーク SDK を初期化します。広告ネットワーク SDK を初期化したら、GADMediationAdapterSetUpCompletionBlock
引数を呼び出します。このブロックを使用すると、nil
または NSError
オブジェクトを使用して完了ハンドラを呼び出すことで、初期化の成功または失敗を Google Mobile Ads SDK に報告できます。
#import "SampleAdapter.h"
@implementation SampleAdapter
...
+ (void)setUpWithConfiguration:(GADMediationServerConfiguration *)configuration
completionHandler:(GADMediationAdapterSetUpCompletionBlock)completionHandler {
// Since the Sample SDK doesn't need to be initialized, the completion
//handler is called directly here.
completionHandler(nil);
}
...
@end
リワード広告をリクエストする
アプリが Google Mobile Ads SDK を使用してリワード広告を読み込むと、メディエーション ウォーターフォールで広告ネットワークに到達したら、アダプタで loadRewardedAdForAdConfiguration:completionHandler:
が呼び出されます。
GADRewardedLoadCompletionHandler
では、GADMediationRewardedAd
プロトコルに準拠したオブジェクトへの参照を指定することで広告の読み込み成功を Google Mobile Ads SDK に報告できます。また、nil
参照と NSError
オブジェクトを指定すれば広告の読み込みの失敗をレポートすることも可能です。読み込み完了ハンドラを呼び出すと、GADMediationRewardedAdEventDelegate
が返されます。アダプタは、広告のライフサイクルの間、この先を保持して後続のイベントを Google Mobile Ads SDK に通知する必要があります。
#import "SampleAdapter.h"
@interface SampleAdapter () <GADMediationRewardedAd> {
/// Rewarded ads from Sample SDK.
SampleRewardedAd *_rewardedAd;
/// Handles any callback when the sample rewarded ad finishes loading.
GADMediationRewardedLoadCompletionHandler _loadCompletionHandler;
/// Delegate for receiving rewarded ad notifications.
__weak id<GADMediationRewardedAdEventDelegate> _rewardedAdDelegate;
}
@end
@implementation SampleAdapter
...
- (void)loadRewardedAdForAdConfiguration:(GADMediationRewardedAdConfiguration *)adConfiguration
completionHandler:
(GADMediationRewardedLoadCompletionHandler)completionHandler {
_loadCompletionHandler = completionHandler;
NSString *adUnit = adConfiguration.credentials.settings[SampleSDKAdUnitIDKey];
SampleExtras *extras = adConfiguration.extras;
_rewardedAd = [[SampleRewardedAd alloc] initWithAdUnitID:adUnit];
_rewardedAd.enableDebugLogging = extras.enableDebugLogging;
/// Check the extras to see if the request should be customized.
SampleAdRequest *request = [[SampleAdRequest alloc] init];
request.mute = extras.muteAudio;
/// Set the delegate on the rewarded ad to listen for callbacks from the Sample SDK.
_rewardedAd.delegate = self;
[_rewardedAd fetchAd:request];
}
...
@end
広告の読み込みイベントをリレーする
第三者 SDK のコールバックをリッスンし、適切な Google Mobile Ads SDK のコールバックにマッピングするのは、アダプタの責任です。
広告またはエラーとともに loadCompletionHandler
を呼び出して、第三者の広告読み込みイベントの成功または失敗を報告します。広告とともに完了ハンドラが呼び出され、エラーがなければ、広告イベント デリゲート オブジェクトが返されます。後でアダプタがプレゼンテーション イベントをリレーできるように、このデリゲートへの参照を保持しておきます。
- (void)rewardedAdDidReceiveAd:(nonnull SampleRewardedAd *)rewardedAd {
_rewardedAdDelegate = _loadCompletionHandler(self, nil);
}
- (void)rewardedAdDidFailToLoadWithError:(SampleErrorCode)errorCode {
_loadCompletionHandler(nil, [NSError errorWithDomain:kAdapterErrorDomain
code:GADErrorNoFill
userInfo:nil]);
}
広告を表示する
アプリが Google Mobile Ads SDK にリワード広告の表示をリクエストすると、SDK は読み込み完了ハンドラの呼び出しで指定された GADMediationRewardedAd
のインスタンスで presentFromViewController:
メソッドを呼び出します。ここではリワード広告を画面に表示します。
- (void)presentFromViewController:(nonnull UIViewController *)viewController {
if (!_rewardedAd.isReady) {
NSError *error =
[NSError errorWithDomain:kAdapterErrorDomain
code:0
userInfo:@{NSLocalizedDescriptionKey : @"Unable to display ad."}];
[_rewardedAdDelegate didFailToPresentWithError:error];
return;
}
[_rewardedAd presentFromRootViewController:viewController];
}
広告イベントを Mobile Ads SDK にレポートする
広告を表示した後、アダプタは、広告の読み込み成功時に返されたのと同じ広告イベント デリゲートを使用して、広告プレゼンテーションのライフサイクル イベントをレポートする必要があります。
- (void)rewardedAdDidPresent:(nonnull SampleRewardedAd *)rewardedAd {
[_rewardedAdDelegate willPresentFullScreenView];
[_rewardedAdDelegate didStartVideo];
[_rewardedAdDelegate reportImpression];
}
- (void)rewardedAdDidDismiss:(nonnull SampleRewardedAd *)rewardedAd {
[_rewardedAdDelegate willDismissFullScreenView];
[_rewardedAdDelegate didEndVideo];
[_rewardedAdDelegate didDismissFullScreenView];
}
- (void)rewardedAd:(nonnull SampleRewardedAd *)rewardedAd userDidEarnReward:(NSUInteger)reward {
GADAdReward *aReward =
[[GADAdReward alloc] initWithRewardType:@"GADMediationAdapterSampleAdNetwork"
rewardAmount:[NSDecimalNumber numberWithUnsignedInt:reward]];
[_rewardedAdDelegate didRewardUserWithReward:aReward];
}
Google Mobile Ads SDK に報告される広告イベントの詳細は次のとおりです。
広告イベント | 説明 |
---|---|
willPresentFullScreenView | 広告が表示されることを Google Mobile Ads SDK に通知します。 |
didStartVideo | リワード広告が再生されたことを Google Mobile Ads SDK に通知します。 |
reportImpression | 広告でインプレッションが発生したことを Google Mobile Ads SDK に通知します。 |
didEndVideo | リワード広告の再生が終了したことを Google Mobile Ads SDK に通知します。 |
didRewardUserWithReward | ユーザーが報酬を獲得したことを Google Mobile Ads SDK に通知します。 |
reportClick | 広告がクリックされたことを Google Mobile Ads SDK に通知します。 |
willDismissFullScreenView | 広告が閉じられることを Google Mobile Ads SDK に通知します。 |
didDismissFullScreenView | 広告が閉じられたことを Google Mobile Ads SDK に通知します。 |