まず、Android PAL SDK をアプリに追加します。
Android PAL SDK をライブラリとして追加する
バージョン 18.0.0 以降、PAL SDK は Google の Maven リポジトリでホストされており、次のようにアプリに追加できます。
app/build.gradle
...
dependencies {
implementation 'com.google.android.gms:play-services-pal:22.0.0'
...
}
または、Google の Maven リポジトリから PAL SDK をダウンロードして、アプリに手動で追加することもできます。
ノンスを生成する
「ノンス」は、NonceLoader
を使用して PAL によって生成される単一の暗号化文字列です。PAL SDK では、新しいストリーム リクエストごとに新しく生成されたノンスが付加されている必要があります。ただし、ノンスは同じストリーム内の複数の広告リクエストで再利用できます。PAL SDK を使用してノンスを生成するには、MyActivity.java
を変更します。PAL を使用してノンスを生成するサンプルアプリについては、GitHub から Android の例をダウンロードしてください。
まず、PAL SDK をインポートし、NonceLoader
と NonceManager
を保存するプライベート プロパティを作成してから、NonceLoader を初期化する必要があります。
アプリに複数のページまたは同等のコンストラクトがない限り、アプリ内のユーザー セッションごとに NonceLoader
クラスのインスタンスを 1 つだけ作成することをおすすめします。これにより、ページまたはアプリでのユーザーのセッションの存続期間中、ページ相関値(&correlator
)は変更されません。ストリーム相関値(&scor
)は引き続き制御できますが、新しいストリームごとに 1 回リセットする必要があります。
フリークエンシー キャップと競合相手の除外機能が正しく動作するようにするには、同じストリームのすべての広告リクエストで同じ NonceLoader
とストリーム correlator 値を共有する必要があります。
import android.app.Activity;
import android.os.Bundle;
import com.google.ads.interactivemedia.pal.NonceLoader;
import com.google.ads.interactivemedia.pal.NonceManager;
import com.google.ads.interactivemedia.pal.NonceRequest;
import com.google.ads.interactivemedia.pal.ConsentSettings;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import java.util.HashSet;
import java.util.Set;
public class MainActivity extends Activity {
...
private NonceLoader nonceLoader;
private NonceManager nonceManager = null;
private ConsentSettings consentSettings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The default value for allowStorage() is false, but can be
// changed once the appropriate consent has been gathered. The
// getConsentToStorage() method is a placeholder for the publisher's own
// method of obtaining user consent, either by integrating with a CMP or
// based on other methods the publisher chooses to handle storage consent.
boolean isConsentToStorage = getConsentToStorage();
videoView = findViewById(R.id.video_view);
videoView.setOnTouchListener(this::onVideoViewTouch);
consentSettings = ConsentSettings.builder()
.allowStorage(isConsentToStorage)
.build();
// It is important to instantiate the NonceLoader as early as possible to
// allow it to initialize and preload data for a faster experience when
// loading the NonceManager. A new NonceLoader will need to be instantiated
//if the ConsentSettings change for the user.
nonceLoader = new NonceLoader(this, consentSettings);
...
}
次に、ノンスの生成をトリガーする関数を作成します。1 つのストリーム再生内のすべての広告リクエストに必要なノンスは 1 つだけです。テスト目的で、テストアプリのボタンをクリックしたときにこの関数を呼び出すことができます。ここで設定する NonceRequest
パラメータはサンプル パラメータです。パラメータは、アプリの特性に基づいて設定する必要があります。
この関数はノンスの生成を非同期でトリガーするため、ノンス リクエストの成功または失敗を処理する AsyncTask
を実装する必要があります。
public void generateNonceForAdRequest() {
Set supportedApiFrameWorksSet = new HashSet();
// The values 2, 7, and 9 correspond to player support for VPAID 2.0,
// OMID 1.0, and SIMID 1.1.
supportedApiFrameWorksSet.add(2);
supportedApiFrameWorksSet.add(7);
supportedApiFrameWorksSet.add(9);
NonceRequest nonceRequest = NonceRequest.builder()
.descriptionURL("https://example.com/content1")
.iconsSupported(true)
.omidPartnerVersion("6.2.1")
.omidPartnerName("Example Publisher")
.playerType("ExamplePlayerType")
.playerVersion("1.0.0")
.ppid("testPpid")
.sessionId("Sample SID")
.supportedApiFrameworks(supportedApiFrameWorksSet)
.videoPlayerHeight(480)
.videoPlayerWidth(640)
.willAdAutoPlay(true)
.willAdPlayMuted(false)
.build();
NonceCallbackImpl callback = new NonceCallbackImpl();
nonceLoader
.loadNonceManager(nonceRequest)
.addOnSuccessListener(callback)
.addOnFailureListener(callback);
}
private class NonceCallbackImpl implements OnSuccessListener<NonceManager>, OnFailureListener {
@Override
public void onSuccess(NonceManager manager) {
nonceManager = manager;
String nonceString = manager.getNonce();
Log.i("PALSample", "Generated nonce: " + nonceString);
// from here you would trigger your ad request and move on to initialize content
}
@Override
public void onFailure(Exception error) {
Log.e("PALSample", "Nonce generation failed: " + error.getMessage());
}
}
ノンス マネージャーを作成すると、nonceManager.getNonce()
を使用してノンスをいつでも取得できます。
広告リクエストにノンスを添付する
生成されたノンスを使用するには、広告リクエストを行う前に、広告タグに givn
パラメータとノンス値を追加します。
/**
* The ad tag for your ad request, for example:
* https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external\
* /single_ad_samples&ciu_szs=300x250&impl=s&gdfp_req=1&env=vp&output=vast&unviewed_position_start=1\
* &cust_params=deployment%3Ddevsite%26sample_ct%3Dlinear&correlator=
*
* For more sample ad tags, see
* developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags
*/
private static final String DEFAULT_AD_TAG = "Your ad tag";
...
@Override
public void onSuccess(NonceManager manager) {
nonceManager = manager;
String nonceString = manager.getNonce();
Log.i("PALSample", "Generated nonce: " + nonceString);
// Append the nonce to the ad tag URL.
makeAdRequest(DEFAULT_AD_TAG + "&givn=" + nonceString);
}
再生イベントをトラッキングする
最後に、プレーヤーのさまざまなイベント ハンドラを実装する必要があります。テスト目的では、これらのイベントをボタンのクリック イベントに関連付けることができますが、実際の実装では、適切なプレーヤー イベントによってトリガーされます。
public void sendAdClick() {
if (nonceManager != null) {
nonceManager.sendAdClick();
}
}
public void sendPlaybackStart() {
if (nonceManager != null) {
nonceManager.sendPlaybackStart();
}
}
public void sendPlaybackEnd() {
if (nonceManager != null) {
nonceManager.sendPlaybackEnd();
}
}
public void onVideoViewTouch(MotionEvent e) {
if (nonceManager != null) {
nonceManager.sendTouch(e);
}
}
実装で各関数を呼び出すタイミングは次のとおりです。
sendPlaybackStart()
: 動画再生セッションの開始時sendPlaybackEnd()
: 動画再生セッションの終了時sendAdClick()
: 視聴者が広告をクリックするたびsendTouch()
: プレーヤーをタップするたびに
(省略可)第三者広告サーバー経由で Google アド マネージャーのシグナルを送信する
Google アド マネージャーと連携するようにサードパーティ広告サーバーを設定する場合は、サーバーのドキュメントを参照して、各広告リクエストのノンス値を取得して転送してください。指定された例は、ノンス パラメータを含む広告リクエスト URL です。ノンスのパラメータは、PAL SDK から中間サーバー経由でアド マネージャーに伝播され、収益化の向上に役立ちます。
アド マネージャーへのサーバーのリクエストにノンスを含めるように、第三者広告サーバーを設定します。第三者広告サーバー内で設定された広告タグの例を次に示します。
'https://pubads.serverside.net/gampad/ads?givn=%%custom_key_for_google_nonce%%&...'
詳しくは、Google アド マネージャーのサーバーサイド実装ガイドをご覧ください。
アド マネージャーは givn=
を探してノンス値を特定します。サードパーティ広告サーバーは、%%custom_key_for_google_nonce%%
などの独自のマクロをサポートし、それを前の手順で指定したノンスクエリ パラメータに置き換える必要があります。詳しくは、第三者広告サーバーのドキュメントをご覧ください。