自訂原生廣告格式

除了系統定義的原生格式外,Ad Manager 發布商也能定義自訂素材資源清單,建立自己的原生廣告格式,稱為自訂原生廣告格式,可與預訂廣告一起使用。發布商可透過這種格式,將任意結構化資料傳遞至應用程式。這些廣告會以 NativeCustomFormatAd 物件表示。

載入自訂原生廣告格式

本指南說明如何載入及顯示自訂原生廣告格式

載入自訂原生廣告

如要載入自訂原生廣告,請按照下列步驟操作:

  1. NativeAdType.CUSTOM_NATIVE 中加入 NativeAdRequest 做為廣告類型。

  2. 設定自訂原生廣告格式 ID。

Kotlin

val adRequest =
  NativeAdRequest.Builder("AD_UNIT_ID", listOf(NativeAdType.CUSTOM_NATIVE))
    .setCustomFormatIds(listOf("CUSTOM_NATIVE_FORMAT_ID"))
    .build()

// Load the native ad with the ad request and callback.
NativeAdLoader.load(
  adRequest,
  object : NativeAdLoaderCallback {
    override fun onCustomNativeAdLoaded(customNativeAd: CustomNativeAd) {
      // TODO: Store the custom native ad.
    }

    override fun onAdFailedToLoad(adError: LoadAdError) {}
  },
)

Java

NativeAdRequest adRequest =
    new NativeAdRequest.Builder("AD_UNIT_ID", List.of(NativeAd.NativeAdType.CUSTOM_NATIVE))
        .setCustomFormatIds(Arrays.asList("CUSTOM_NATIVE_FORMAT_ID"))
        .build();

// Load the native ad with the ad request and callback.
NativeAdLoader.load(
    adRequest,
    new NativeAdLoaderCallback() {
      @Override
      public void onCustomNativeAdLoaded(@NonNull CustomNativeAd customNativeAd) {
        // TODO: Store the custom native ad.
      }

      @Override
      public void onAdFailedToLoad(@NonNull LoadAdError adError) {}
    });

自訂原生廣告格式 ID

前往 Ad Manager 使用者介面,並在「廣告放送」下拉式選單中選擇「原生」,即可找到用來識別自訂原生廣告格式的專屬 ID:

每個自訂原生廣告格式的 ID 都顯示在名稱旁。點選任一名稱即可進入詳細資料畫面,查看該格式欄位的資訊:

您可以在這裡新增、編輯及移除個別欄位。請記下每個素材資源的「名稱」,在顯示自訂原生廣告格式時,此名稱是用來取得相應資料的關鍵值。

顯示自訂原生廣告格式

自訂原生廣告格式與系統定義格式的不同之處在於,發布商可以自行定義組成廣告的素材資源清單。因此,兩者的顯示流程在某些方面有所差異:

  1. 文字和圖片素材資源可透過 getText()getImage() 取得,這些 getter 會將欄位名稱做為參數。
  2. 由於沒有專用的 ViewGroup 類別可向 Google 註冊,因此您需要手動記錄曝光和點擊事件。
  3. 如果廣告不含影片素材資源,自訂原生廣告會顯示 null 媒體內容。

這個範例說明如何顯示 CustomNativeAd

Kotlin

private fun displayCustomNativeAd(customNativeAd: CustomNativeAd, context: Context) {
  // Render the text elements.

  // The `customNativeAdBinding` is the layout binding for the ad container that
  // contains all `CustomNativeAd` assets.
  customNativeAdBinding.headline.text = customNativeAd.getText("Headline")
  customNativeAdBinding.caption.text = customNativeAd.getText("Caption")

  // If the main asset is an image, render it with an ImageView.
  val imageView = ImageView(context)
  imageView.adjustViewBounds = true
  imageView.setImageDrawable(customNativeAd.getImage("MainImage")?.drawable)
  imageView.setOnClickListener { customNativeAd.performClick("MainImage") }
  customNativeAdBinding.mediaPlaceholder.addView(imageView)

  // Render the ad choices icon.
  renderAdChoices(customNativeAd)

  // Record an impression.
  customNativeAd.recordImpression()
}

Java

private void displayCustomNativeAd(CustomNativeAd customNativeAd, Context context) {
  // Render the text elements.

  // The `customNativeAdBinding` is the layout binding for the ad container that
  // contains all `CustomNativeAd` assets.
  if (customNativeAdBinding != null) {
    customNativeAdBinding.headline.setText(customNativeAd.getText("Headline"));
    customNativeAdBinding.caption.setText(customNativeAd.getText("Caption"));

    ImageView imageView = new ImageView(context);
    imageView.setAdjustViewBounds(true);
    imageView.setImageDrawable(customNativeAd.getImage("MainImage").getDrawable());
    imageView.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            customNativeAd.performClick("MainImage");
          }
        });
    customNativeAdBinding.mediaPlaceholder.addView(imageView);

    // Render the ad choices icon.
    renderAdChoices(customNativeAd);

    // Record an impression.
    customNativeAd.recordImpression();
  }
}

自訂原生廣告格式的原生影片廣告

建立自訂格式時,您可以選擇讓格式支援影片。

在應用程式導入過程中,您可以使用 CustomNativeAd.getMediaContent() 取得媒體內容,然後呼叫 setMediaContent(),在媒體檢視區塊設定媒體內容。如果廣告有 null 媒體內容,請準備不含影片的廣告做為替代方案。

以下範例會檢查廣告是否含有影片內容,並在沒有影片可用時顯示圖片:

Kotlin

private fun displayVideoCustomNativeAd(customNativeAd: CustomNativeAd, context: Context) {
  // Check whether the custom native ad has video content.
  val mediaContent = customNativeAd.mediaContent
  if (mediaContent != null && mediaContent.hasVideoContent) {
    // Render the media content in a MediaView.
    val mediaView = MediaView(context)
    mediaView.mediaContent = mediaContent
    customNativeAdBinding.mediaPlaceholder.addView(mediaView)
  } else {
    // Fall back to other assets defined on your custom native ad.
    val imageView = ImageView(context)
    imageView.adjustViewBounds = true
    imageView.setImageDrawable(customNativeAd.getImage("MainImage")?.drawable)
    customNativeAdBinding.mediaPlaceholder.addView(imageView)
  }

  // Record an impression.
  customNativeAd.recordImpression()
}

Java

private void displayVideoCustomNativeAd(CustomNativeAd customNativeAd, Context context) {
  // Check whether the custom native ad has video content.
  MediaContent mediaContent = customNativeAd.getMediaContent();
  if (mediaContent != null && mediaContent.getHasVideoContent()) {
    // Render the media content in a MediaView.
    MediaView mediaView = new MediaView(context);
    mediaView.setMediaContent(mediaContent);
    customNativeAdBinding.mediaPlaceholder.addView(mediaView);
  } else {
    // Fall back to other assets defined on your custom native ad.
    ImageView imageView = new ImageView(context);
    imageView.setAdjustViewBounds(true);
    imageView.setImageDrawable(customNativeAd.getImage("MainImage").getDrawable());
    customNativeAdBinding.mediaPlaceholder.addView(imageView);
  }

  // Record an impression.
  customNativeAd.recordImpression();
}

如要進一步瞭解如何控制自訂原生廣告的影片體驗,請參閱「影片廣告」。

顯示 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 圖示,並設定適當點擊行為。

Kotlin

private fun renderAdChoices(customNativeAd: CustomNativeAd) {
  // Render the AdChoices image.
  val adChoiceAsset = customNativeAd.getImage(NativeAdAssetNames.ASSET_ADCHOICES_CONTAINER_VIEW)
  if (adChoiceAsset != null) {
    customNativeAdBinding.adchoices.setImageDrawable(adChoiceAsset.drawable)
    customNativeAdBinding.adchoices.visibility = View.VISIBLE
    customNativeAdBinding.adchoices.setOnClickListener {
      // Handle click. See the next section for more details.
      customNativeAd.performClick(NativeAdAssetNames.ASSET_ADCHOICES_CONTAINER_VIEW)
    }
  } else {
    customNativeAdBinding.adchoices.visibility = View.GONE
  }
}

Java

private void renderAdChoices(CustomNativeAd customNativeAd) {
  // Render the AdChoices image.
  Image adChoiceAsset =
      customNativeAd.getImage(NativeAdAssetNames.ASSET_ADCHOICES_CONTAINER_VIEW);
  if (adChoiceAsset != null) {
    if (customNativeAdBinding != null) {
      customNativeAdBinding.adchoices.setImageDrawable(adChoiceAsset.getDrawable());
      customNativeAdBinding.adchoices.setVisibility(View.VISIBLE);
      customNativeAdBinding.adchoices.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              // Handle click.
              customNativeAd.performClick(NativeAdAssetNames.ASSET_ADCHOICES_CONTAINER_VIEW);
            }
          });
    }
  } else {
    if (customNativeAdBinding != null) {
      customNativeAdBinding.adchoices.setVisibility(View.GONE);
    }
  }
}

記錄曝光次數及回報點擊事件

您的應用程式必須負責記錄曝光次數,並向 Google Mobile Ads SDK (Beta 版) 回報點擊事件。

記錄曝光次數

如要記錄自訂原生廣告的曝光次數,請呼叫廣告的 recordImpression() 方法:

Kotlin

// Record an impression.
customNativeAd.recordImpression()

Java

// Record an impression.
customNativeAd.recordImpression();

如果應用程式意外對相同廣告呼叫該方法兩次,SDK 會自動避免在單次請求中重複記錄曝光次數。

回報點擊

如要向 SDK 回報某個素材資源檢視區塊的點擊事件,請呼叫廣告的 performClick() 方法。使用您在 Ad Manager 使用者介面中定義的相同字串,提供已點選的素材資源名稱。

Kotlin

imageView.setOnClickListener { customNativeAd.performClick("MainImage") }

Java

imageView.setOnClickListener(
    new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        customNativeAd.performClick("MainImage");
      }
    });

請注意,您不需要對廣告中的每個檢視區塊呼叫這個方法。舉例來說,如果名為「Caption」的欄位只用來顯示內容,無法點按或輕觸,應用程式就不必對這個素材資源的檢視區塊呼叫 performClick

回應自訂點擊動作

當使用者點選自訂格式廣告,SDK 會依序嘗試以下三種可能回應:

  1. 叫用 OnCustomClickListener (如有)。
  2. 針對廣告的每個深層連結網址,嘗試找出內容解析器,並啟動實際執行解析的第一個解析器。
  3. 開啟瀏覽器,前往廣告的到達網頁網址。

如要導入自訂點擊動作,請提供 OnCustomClickListener

Kotlin

customNativeAd.onCustomClickListener =
  object : OnCustomClickListener {
    override fun onCustomClick(assetName: String) {
      // Perform your custom action.
    }
  }

Java

customNativeAd.setOnCustomClickListener(
    new OnCustomClickListener() {
      @Override
      public void onCustomClick(@NonNull String assetName) {
        // Perform your custom action.
      }
    });

一開始,您可能會覺得自訂點擊事件監聽器很奇怪。畢竟應用程式才剛告訴 SDK 發生了點擊事件,為什麼 SDK 還要再回報給應用程式呢?

其實這種資訊流動有不少好處,最重要的一點是 SDK 能掌控點擊事件的處理方式。比方說,您無需額外撰寫程式碼,SDK 就會自動連線偵測 (ping) 廣告素材設定的第三方追蹤網址,並在背景處理其他工作。