カスタムのネイティブ広告フォーマット

カスタムのネイティブ広告フォーマット

アド マネージャーのパブリッシャーは、システム定義のネイティブ フォーマットに加え、 カスタムリストを定義して 独自のネイティブ広告フォーマットを できます。これらはカスタム ネイティブ広告と呼ばれます。 形式であり、 表示されます。これによりニュース メディアは任意の構造化データを 公開されています。これらの広告は NativeCustomFormatAd 渡されます。

カスタムのネイティブ広告フォーマットを読み込む

このガイドでは、カスタムのネイティブ広告フォーマットの読み込みと表示の方法について説明します。

AdLoader を作成する

ネイティブ広告と同様に カスタムのネイティブ広告フォーマットは、次のように AdLoader クラスを使用して読み込みます。

Java

AdLoader adLoader = new AdLoader.Builder(context, "/6499/example/native")
    .forCustomFormatAd("10063170",
      new NativeCustomFormatAd.OnCustomFormatAdLoadedListener() {
          @Override
          public void onCustomFormatAdLoaded(NativeCustomFormatAd ad) {
              // Show the custom format and record an impression.
          }
      },
      new NativeCustomFormatAd.OnCustomClickListener() {
          @Override
          public void onCustomClick(NativeCustomFormatAd ad, String s) {
              // Handle the click action
          }
      })
    .withAdListener( ... )
    .withNativeAdOptions( ... )
    .build();

Kotlin

val adLoader = AdLoader.Builder(this, "/6499/example/native")
        .forCustomFormatAd("10063170",
            { ad ->
                // Show the custom format and record an impression.
            },
            { ad, s ->
            // Handle the click action
            })
        .withAdListener( ... )
        .withNativeAdOptions( ... )
        .build()

forCustomFormatAd メソッドは、カスタム イベントをリクエストするように AdLoader を構成します。 ネイティブ広告フォーマットですメソッドには 3 つのパラメータが渡されます。

  • AdLoader がリクエストするカスタム ネイティブ広告フォーマットの ID。各 カスタムのネイティブ広告フォーマットに ID が関連付けられています。この パラメータは、アプリが AdLoader にリクエストする形式を示します。
  • OnCustomFormatAdLoadedListener 広告が正常に読み込まれたときに呼び出すことができます。
  • オプションの OnCustomClickListener ユーザーが広告をタップまたはクリックしたときに呼び出すことができます。このトピックについて詳しくは、 ダウンロード方法については、コースリソースにあるセクションをご覧ください。

1 つの広告ユニットを複数のクリエイティブを配信するよう設定できるから。 forCustomFormatAd は、一意のキーで複数回呼び出すことができます。 複数のフォーマット ID を使用して、複数の可能なフォーマット ID を カスタムのネイティブ広告フォーマットです

カスタムのネイティブ広告フォーマット ID

カスタムのネイティブ広告フォーマットの識別に使用するフォーマット ID には、 アド マネージャー管理画面の [配信] の [ネイティブ] で確認できます。 プルダウン:

名前の横には、カスタムのネイティブ広告フォーマット ID が表示されます。いずれかの [詳細] 画面に、その形式のファイルに関する情報が表示されます。 フィールド:

ここで、個々のフィールドを追加、編集、削除できます。注意すべき点は、 各アセットの名前。名前は、特定の期間のデータを取得するために使用される カスタムのネイティブ広告フォーマットを表示する際に

ディスプレイ カスタムのネイティブ広告フォーマット

カスタムのネイティブ広告フォーマットがシステム定義のフォーマットと異なる点は、 アセットのリストを作成して 広告を作成しますそのため、カスタム ディメンションを表示するプロセスは、 アップロードできます。

  1. NativeCustomFormatAd クラスは、 アド マネージャーで定義したカスタムのネイティブ広告フォーマットに、名前は "ゲッターズ"追加できます代わりに、getText や フィールドの名前をパラメータとして受け取る getImage
  2. NativeAdView のような専用の広告ビュークラスがない NativeCustomFormatAd。レイアウトは自由で自由にお使いいただけます。 考慮する必要があります
  3. 専用の ViewGroup クラスがないため、登録する必要はありません。 広告のアセットの表示に使用されたすべてのビュー。そうすれば数行の入力が 追加のコードが必要になりますが 後でクリックを処理します

次に、NativeCustomFormatAd を表示する関数の例を示します。

Java

public void displayCustomFormatAd (ViewGroup parent,
                                     NativeCustomFormatAd customFormatAd) {
    // Inflate a layout and add it to the parent ViewGroup.
    LayoutInflater inflater = (LayoutInflater) parent.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View adView = inflater.inflate(R.layout.custom_format_ad, parent);

    // Locate the TextView that will hold the value for "Headline" and
    // set its text.
    TextView myHeadlineView = (TextView) adView.findViewById(R.id.headline);
    myHeadlineView.setText(customFormatAd.getText("Headline"));

    // Locate the ImageView that will hold the value for "MainImage" and
    // set its drawable.
    Button myMainImageView = (ImageView) adView.findViewById(R.id.main_image);
    myMainImageView.setImageDrawable(
            customFormatAd.getImage("MainImage").getDrawable());

    ...
    // Continue locating views and displaying assets until finished.
    ...
}

Kotlin

public fun displayCustomFormatAd (parent: ViewGroup,
                                customFormatAd: NativeCustomFormatAd) {
    val adView = layoutInflater
            .inflate(R.layout.ad_simple_custom_format, null)

    val myHeadlineView = adView.findViewById<TextView>(R.id.headline)
    myHeadlineView.setText(customFormatAd.getText("Headline"));

    // Locate the ImageView that will hold the value for "MainImage" and
    // set its drawable.
    val myMainImageView = adView.findViewById(R.id.main_image);
    myMainImageView.setImageDrawable(
            customFormatAd.getImage("MainImage").drawable);

    ...
    // Continue locating views and displaying assets until finished.
    ...
}

AdChoices アイコンをレンダリングする

デジタル サービス法(DSA)への支援の一環として、 欧州経済領域(EEA)で配信される純広告には、 AdChoices アイコンと、Google の [この広告について] ページへのリンク。 カスタム ネイティブ広告を実装する場合は、ご自身で AdChoices アイコン。レンダリングを行い、クリックを設定することをおすすめします。 メインの広告アセットのレンダリング時に AdChoices アイコンのリスナーを設定できます。

次の例では、<ImageView /> 要素が ビュー階層を変更して AdChoices ロゴを保持できます。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView
        android:id="@+id/adChoices"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:adjustViewBounds="true"
        android:contentDescription="AdChoices icon." />
</LinearLayout>

次のサンプルでは、AdChoices アイコンと 適切なクリック動作を設定します。

Java

private AdSimpleCustomTemplateBinding customTemplateBinding;

private void populateAdView(final NativeCustomFormatAd nativeCustomFormatAd) {
  // Render the AdChoices icon.
  String adChoicesKey = NativeAdAssetNames.ASSET_ADCHOICES_CONTAINER_VIEW;
  NativeAd.Image adChoicesAsset = nativeCustomFormatAd.getImage(adChoicesKey);
  if (adChoicesAsset == null) {
    customTemplateBinding.adChoices.setVisibility(View.GONE);
  } else {
    customTemplateBinding.adChoices.setVisibility(View.VISIBLE);
    customTemplateBinding.adChoices.setImageDrawable(adChoicesAsset.getDrawable());

    // Enable clicks on AdChoices.
    customTemplateBinding.adChoices.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            nativeCustomFormatAd.performClick(adChoicesKey);
          }
        });
  }
  ...
}

Kotlin

private lateinit var customTemplateBinding: AdSimpleCustomTemplateBinding

private fun populateAdView(nativeCustomFormatAd: NativeCustomFormatAd) {
  // Render the AdChoices icon.
  val adChoicesKey = NativeAdAssetNames.ASSET_ADCHOICES_CONTAINER_VIEW
  val adChoicesAsset = nativeCustomFormatAd.getImage(adChoicesKey)
  if (adChoicesAsset == null) {
    customTemplateBinding.adChoices.visibility = View.GONE
  } else {
    customTemplateBinding.adChoices.setImageDrawable(adChoicesAsset.drawable)
    customTemplateBinding.adChoices.visibility = View.VISIBLE

    // Enable clicks on AdChoices.
    customTemplateBinding.adChoices.setOnClickListener {
      nativeCustomFormatAd.performClick(adChoicesKey)
    }
  }
  ...
}

カスタム ネイティブ広告フォーマット用のネイティブ動画

カスタム フォーマットを作成する際は、 そのフォーマットを動画対応にすることができます。

アプリの実装では、 NativeCustomFormatAd.getMediaContent() 取得する必要があります次に、setMediaContent() を呼び出します。 メディアビューにメディア コンテンツを設定します。追加できます 広告に動画コンテンツがない場合は、別の表示プランを作成します 動画なしで広告を再生します。

以下の例では、広告に動画コンテンツが含まれているかどうかを確認し、画像を表示しています 動画を利用できない場合は、代わりに次のように表示されます。

Java

// Called when a custom native ad loads.
@Override
public void onCustomFormatAdLoaded(final NativeCustomFormatAd ad) {

  MediaContent mediaContent = ad.getMediaContent();

  // Assumes you have a FrameLayout in your view hierarchy with the id media_placeholder.
  FrameLayout mediaPlaceholder = (FrameLayout) findViewById(R.id.media_placeholder);

  // Apps can check the MediaContent's hasVideoContent property to determine if the
  // NativeCustomFormatAd has a video asset.
  if (mediaContent != null && mediaContent.hasVideoContent()) {
    MediaView mediaView = new MediaView(mediaPlaceholder.getContext());
    mediaView.setMediaContent(mediaContent);
    mediaPlaceholder.addView(mediaView);

    // Create a new VideoLifecycleCallbacks object and pass it to the VideoController. The
    // VideoController will call methods on this object when events occur in the video
    // lifecycle.
    VideoController vc = mediaContent.getVideoController();
    vc.setVideoLifecycleCallbacks(
        new VideoController.VideoLifecycleCallbacks() {
          @Override
          public void onVideoEnd() {
            // Publishers should allow native ads to complete video playback before
            // refreshing or replacing them with another ad in the same UI location.
            super.onVideoEnd();
          }
        });
  } else {
    ImageView mainImage = new ImageView(this);
    mainImage.setAdjustViewBounds(true);
    mainImage.setImageDrawable(ad.getImage("MainImage").getDrawable());
    mediaPlaceholder.addView(mainImage);
    mainImage.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View view) {
            ad.performClick("MainImage");
          }
        });
  }
}

Kotlin

// Called when a custom native ad loads.
NativeCustomFormatAd.OnCustomFormatAdLoadedListener { ad ->

  val mediaContent = ad.mediaContent

  // Apps can check the MediaContent's hasVideoContent property to determine if the
  // NativeCustomFormatAd has a video asset.
  if (mediaContent != null && mediaContent.hasVideoContent()) {
    val mediaView = MediaView(mediaPlaceholder.getContest())
    mediaView.mediaContent = mediaContent

    val videoController = mediaContent.videoController

    // Create a new VideoLifecycleCallbacks object and pass it to the VideoController. The
    // VideoController will call methods on this object when events occur in the video
    // lifecycle.
    if (videoController != null) {
      videoController.videoLifecycleCallbacks =
        object : VideoController.VideoLifecycleCallbacks() {
          override fun onVideoEnd() {
            // Publishers should allow native ads to complete video playback before refreshing
            // or replacing them with another ad in the same UI location.
            super.onVideoEnd()
          }
        }
    }
  } else {
    val mainImage = ImageView(this)
    mainImage.adjustViewBounds = true
    mainImage.setImageDrawable(ad.getImage("MainImage")?.drawable)

    mainImage.setOnClickListener { ad.performClick("MainImage") }
    customTemplateBinding.simplecustomMediaPlaceholder.addView(mainImage)
  }
}

コンテンツ チャットの使用方法については、MediaContent をご覧ください。 カスタム ネイティブ広告の動画エクスペリエンスをカスタマイズする。

Google アド マネージャーのカスタム レンダリング 例 ネイティブ動画広告の実際の使用例を見てみましょう

カスタム ネイティブ広告フォーマットのクリック数とインプレッション数

カスタムのネイティブ広告フォーマットでは、アプリが クリック イベントを Google Mobile Ads SDK にレポートできます。

インプレッションを記録する

カスタム フォーマット広告のインプレッションを記録するには、recordImpression を呼び出します。 メソッドを、対応する NativeCustomFormatAd で呼び出します。

myCustomFormatAd.recordImpression();

アプリが同じ広告に対して誤ってメソッドを 2 回呼び出すと、SDK は 1 つ以上のインプレッションが重複して記録されることを リクエストできます。

レポートのクリック数

アセットビューでクリックが発生したことを SDK に報告するには、 対応する NativeCustomFormatAdperformClick メソッドを使用し、 クリックされたアセットの名前。たとえば 「MainImage」というカスタムフォーマットをクリックを報告するには ImageView と指定すると、コードは次のようになります。

myCustomFormatAd.performClick("MainImage");

ビューに関連付けられているすべてのビューに対してこのメソッドを呼び出す必要はありません。 追加できます「Caption」という別のフィールドがある場合データ アナリストが クリックやタップがなかった場合、アプリは そのアセットのビューに対して performClick を呼び出す。

カスタム クリック アクションに応答する

カスタム フォーマットの広告がクリックされると、次の 3 つのいずれかが考えられます。 次の順序で試行されます。

  1. OnCustomClickListener が指定されている場合は、AdLoader から呼び出します。
  2. 広告のディープリンク URL ごとに、コンテンツ リゾルバを特定する 解決された最初の接続を開始します
  3. ブラウザを開き、広告の従来のリンク先 URL に移動します。

forCustomFormatAd メソッドは OnCustomClickListener を受け取ります。もし リスナー オブジェクトを渡すと、SDK は代わりにその onCustomClick メソッドを呼び出します。 何もする必要はありません。ただし、リスナーとして null 値を渡した場合は、 SDK は 表示されます。

カスタム クリック リスナーを使用すると、アプリで最適なアクションを決定できます。 UI の更新や新しいアクティビティの起動など クリックを記録するだけですクリックの発生を単純に記録する例を次に示します place:

Java

AdLoader adLoader = new AdLoader.Builder(context, "/6499/example/native")
    .forCustomFormatAd("10063170",
      new NativeCustomFormatAd.OnCustomFormatAdLoadedListener() {
        // Display the ad.
      },
      new NativeCustomFormatAd.OnCustomClickListener() {
          @Override
          public void onCustomClick(NativeCustomFormatAd ad, String assetName) {
            Log.i("MyApp", "A custom click just happened for " + assetName + "!");
          }
      }).build();

Kotlin

val adLoader = AdLoader.Builder(this, "/6499/example/native")
    .forCustomFormatAd("10063170",
        { ad ->
            // Display the ad.
        },
        { ad, assetName ->
                Log.i("MyApp", "A custom click just happened for $assetName!")
    }).build()

最初は、カスタム クリック リスナーがあることに不思議に思えるかもしれませんが、結局のところ、 クリックの発生をアプリが SDK に通知したので、 アプリに報告しますか?

この情報の流れが役立つ理由はいくつかありますが、最も重要なのは、 これにより、SDK はクリックへのレスポンスの制御を維持できます。機能 に対して設定されている第三者のトラッキング URL を自動的に ping する 他のタスクを処理したり、バックグラウンドで他のタスクを 追加のコードです。