開始する

PAL を使用すると、広告リクエスト時と広告再生時に Google 広告シグナルを送信できます。

このガイドでは、Android PAL SDK をアプリに追加する方法について説明します。PAL を使用して nonce を生成するサンプルアプリについては、GitHub から Android のサンプルをダウンロードしてください。

Android PAL SDK をライブラリとして追加する

バージョン 18.0.0 以降、PAL SDK は Google の Maven リポジトリでホストされており、次のようにアプリに追加できます。

implementation 'com.google.android.gms:play-services-pal:22.1.0'

または、PAL SDK を Google の Maven リポジトリからダウンロードして、アプリに手動で追加することもできます。

ノンスを生成する

ノンスは、PAL が NonceLoader クラスを使用して生成する 1 つの暗号化された文字列です。PAL では、各ストリーム リクエストに一意のノンスを付加する必要があります。ただし、同じストリーム内の複数の広告リクエストでノンスを再利用できます。PAL SDK を使用して nonce を生成するには、次の変更を行って PAL をインポートして設定し、nonce を生成する関数を作成します。

  1. 次の手順で PAL をインポートして設定します。

    1. PAL クラスをインポートします。

      import com.google.ads.interactivemedia.pal.ConsentSettings;
      import com.google.ads.interactivemedia.pal.NonceLoader;
      import com.google.ads.interactivemedia.pal.NonceManager;
      import com.google.ads.interactivemedia.pal.NonceRequest;
      import com.google.android.gms.tasks.OnFailureListener;
      import com.google.android.gms.tasks.OnSuccessListener;
      import java.util.HashSet;
      import java.util.Set;
      
      
    2. NonceLoader インスタンスと NonceManager インスタンスを保存するプライベート変数を作成します。

      private NonceLoader nonceLoader;
      private NonceManager nonceManager;
      
    3. onCreate メソッドで、ConsentSettings インスタンスを使用して NonceLoader インスタンスを初期化します。

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
        // 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 isStorageAllowed = getConsentToStorage();
      
        ConsentSettings consentSettings =
            ConsentSettings.builder().allowStorage(isStorageAllowed).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);
      
        adClickButton = findViewById(R.id.send_click_button);
      
        logView = findViewById(R.id.log_view);
        logView.setMovementMethod(new ScrollingMovementMethod());
      }
      
      

    アプリで、ユーザー セッションごとに NonceLoader クラスのインスタンスを 1 つ作成します。アプリに複数のページまたは同等のコンストラクトがある場合は、ページまたはページと同等のものごとに新しい NonceLoader インスタンスを作成します。同じ NonceLoader インスタンスを使用することで、ページまたはアプリのユーザー セッションのライフサイクル全体にわたってページ相関子 &correlator を変更しないようにできます。ストリーム相関子 &scor は引き続き制御できますが、新しいストリームごとに新しいノンスを生成してリセットする必要があります。

    フリークエンシー キャップと競合他社の除外機能が動作するには、同じストリームのすべての広告リクエストで同じ NonceLoader インスタンスとストリーム相関値が共有されている必要があります。

  2. ノンスを生成します。

    public void generateNonceForAdRequest(View view) {
      logMessage("Generate Nonce Request");
      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();
    
      nonceLoader
          .loadNonceManager(nonceRequest)
          .addOnSuccessListener(
              new OnSuccessListener<NonceManager>() {
                @Override
                public void onSuccess(NonceManager manager) {
                  nonceManager = manager;
                  String nonceString = manager.getNonce();
                  logMessage("Nonce generated");
                  logMessage(nonceString.substring(0, 20) + "...");
                  Log.i(LOG_TAG, "Generated nonce: " + nonceString);
    
                  // From here you would trigger your ad request and move on to initialize content.
                  exampleMakeAdRequest(DEFAULT_AD_TAG + "&givn=" + nonceString);
    
                  adClickButton.setEnabled(true);
                }
              })
          .addOnFailureListener(
              new OnFailureListener() {
                @Override
                public void onFailure(Exception error) {
                  logMessage("Nonce generation failed");
                  Log.e(LOG_TAG, "Nonce generation failed: " + error.getMessage());
                }
              });
    }
    
    

    1 つのストリーム再生のすべての広告リクエストに必要なノンスは 1 つだけです。テスト目的で、テストアプリのボタンをクリックしたときにこの関数を呼び出します。このガイドで設定されている NonceRequest パラメータは、パラメータの例です。アプリの特性に基づいてパラメータを設定します。

    この関数はノンスを非同期的に生成します。ノンス リクエストの成功と失敗の両方のケースを処理する必要があります。ノンス マネージャーが利用可能になったら、nonceManager.getNonce() メソッドを使用して広告リクエストを行う前にノンスを取得します。

広告リクエストに nonce を添付する

生成されたノンスを使用するには、広告リクエストを行う前に、広告タグに givn パラメータとノンス値を付加します。

// From here you would trigger your ad request and move on to initialize content.
exampleMakeAdRequest(DEFAULT_AD_TAG + "&givn=" + nonceString);

再生イベントをトラッキングする

再生イベントをトラッキングするには、広告シグナルを Google に送信するようイベント ハンドラを設定する必要があります。

// Triggered when a user clicks-through on an ad which was requested using a PAL nonce.
public void sendAdClick(View view) {
  logMessage("Ad click sent");
  if (nonceManager != null) {
    nonceManager.sendAdClick();
  }
}

// In a typical PAL app, this is called when a user touch or click is detected,
// on the ad other than an ad click-through.
public void onVideoViewTouch(MotionEvent e) {
  if (nonceManager != null) {
    nonceManager.sendAdTouch(e);
  }
}

// In a typical PAL app, this is called when a content playback session starts.
public void sendPlaybackStart() {
  logMessage("Playback start");
  if (nonceManager != null) {
    nonceManager.sendPlaybackStart();
  }
}

// In a typical PAL app, this is called when a content playback session ends.
public void sendPlaybackEnd() {
  logMessage("Playback end");
  if (nonceManager != null) {
    nonceManager.sendPlaybackEnd();
  }
}

実装で各関数を呼び出すタイミングは次のとおりです。

  • sendPlaybackStart(): 動画の再生セッションが開始されたとき
  • sendPlaybackEnd(): 動画の再生セッションが終了したとき
  • sendAdClick(): 視聴者が広告をクリックするたび
  • sendTouch(): プレーヤーとのタッチ操作のたびに

テスト用に、イベント ハンドラ メソッドをボタンのクリック イベントに関連付けます。本番環境の実装では、プレーヤー イベントでイベント ハンドラ メソッドを呼び出すようにアプリを設定します。

(省略可)第三者広告サーバー経由で Google アド マネージャーのシグナルを送信する

Google アド マネージャーと連携するように第三者広告サーバーを設定する場合は、サーバーのドキュメントを参照して、各広告リクエストでノンス値をキャプチャして転送してください。次の例は、ノンス パラメータを含む広告リクエスト URL です。ノンス パラメータは、PAL SDK から仲介サーバーを経由してアド マネージャーに伝播され、収益化の向上に役立ちます。

アド マネージャーへのサーバー リクエストに nonce を含めるように、第三者広告サーバーを設定します。第三者広告サーバー内で設定された広告タグの例を次に示します。

'https://pubads.serverside.net/gampad/ads?givn=%%custom_key_for_google_nonce%%&...'

詳しくは、Google アド マネージャーのサーバーサイド実装ガイドをご覧ください。

アド マネージャーは givn= を探して、ノンス値を特定します。第三者広告サーバーは、%%custom_key_for_google_nonce%% などの独自のマクロをサポートし、前のステップで指定した nonce クエリ パラメータに置き換える必要があります。この方法について詳しくは、第三者広告サーバーのドキュメントをご覧ください。