自定义原生广告格式

自定义原生广告格式

除了系统定义的原生广告格式之外,Ad Manager 发布商还可以 可让您通过在 Google Analytics 中 资源。这类广告称为自定义原生广告 格式,并且可与 预留广告。这样,发布商就可以将任意结构化数据传递给 应用。这些广告由 NativeCustomFormatAd 对象。

加载自定义原生广告格式

本指南介绍了如何加载和显示自定义原生广告格式

构建 AdLoader

与原生广告一样 自定义原生广告格式是使用 AdLoader 类加载的:

Java

AdLoader adLoader = new AdLoader.Builder(context, "/21775744923/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, "/21775744923/example/native")
        .forCustomFormatAd("10063170",
            { ad ->
                // Show the custom format and record an impression.
            },
            { ad, s ->
            // Handle the click action
            })
        .withAdListener( ... )
        .withNativeAdOptions( ... )
        .build()

forCustomFormatAd 方法将 AdLoader 配置为请求自定义 原生广告格式有三个参数传递到该方法中:

  • AdLoader 应请求的自定义原生广告格式的 ID。每个 自定义原生广告格式具有与其相关联的 ID这个 参数指示您的应用希望 AdLoader 请求哪种格式。
  • 一个 OnCustomFormatAdLoadedListener 在广告成功加载后调用。
  • 可选的 OnCustomClickListener 。如需详细了解 请参阅“处理点击次数和展示次数”部分。

由于可以将单个广告单元设置为投放多个广告素材 格式,因此可以使用唯一值多次调用 forCustomFormatAd 以便广告加载程序为多种可能的 自定义原生广告格式

自定义的原生广告格式 ID

用于标识自定义原生广告格式的格式 ID 可以是 您可以在 Ad Manager 界面中投放原生部分下找到 下拉菜单:

每个自定义的原生广告格式 ID 都会显示在其名称旁边。点击 可让您转到详情屏幕 字段:

在这里,可以添加、修改和移除各个字段。请注意 每项资产的名称。名称是用于获取 在展示自定义原生广告格式时逐一列出每个素材资源

展示自定义的原生广告格式

自定义原生广告格式与系统定义的原生广告格式的不同之处在于 有权定义自己的素材资源列表 。因此,显示自定义广告的过程与系统定义的 广告格式:

  1. 由于 NativeCustomFormatAd 类用于处理 在 Ad Manager 中定义的自定义原生广告格式, “getters”。而是提供了 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.
    ...
}

呈现“广告选项”图标

根据支持《数字服务法案》(DSA) 的规定, 在欧洲经济区 (EEA) 投放的预订型广告需要 “广告选择”图标以及指向 Google 的“关于此广告”页面的链接。 在植入自定义原生广告时,您负责呈现 “广告选择”图标。我们建议您采取措施来呈现和设置点击 监听器。

以下示例假定您已在<ImageView /> 以容纳“广告选择”徽标

<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>

以下示例呈现了“广告选择”图标和 配置适当的点击行为。

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,详细了解如何 自定义自定义原生广告的视频体验。

下载 Ad Manager 自定义呈现 示例 原生视频广告的实际运用示例

自定义原生广告格式的点击次数和展示次数

使用自定义原生广告格式时,您的应用会负责录制 并向 Google 移动广告 SDK 报告点击事件。

记录展示次数

要记录自定义格式广告的展示次数,请调用 recordImpression 方法(针对相应的 NativeCustomFormatAd):

myCustomFormatAd.recordImpression();

如果您的应用不小心针对同一个广告调用该方法两次,则 SDK 可自动防止系统针对单个广告素材或服务 请求。

报告点击次数

要向 SDK 报告在素材资源视图中发生了点击,请调用 对相应的 NativeCustomFormatAd 执行 performClick 方法,并传入 用户点击的素材资源的名称。例如,如果您在 名为“MainImage”的自定义格式并想报告 ImageView 时,您的代码将如下所示:

myCustomFormatAd.performClick("MainImage");

请注意,您不需要为 。如果您有另一个名为“Caption”的字段原本的 显示但用户并未点击或点按,您的应用就不需要 针对该素材资源的视图调用 performClick

响应自定义点击操作

当用户点击自定义格式的广告时,可能出现以下三种情况 响应,按以下顺序进行尝试:

  1. AdLoader 调用 OnCustomClickListener(如果已提供)。
  2. 对于每个广告的深层链接网址,尝试查找内容解析器 然后启动第一个可以解析的订单
  3. 打开浏览器并转到广告的传统目标网址。

forCustomFormatAd 方法接受 OnCustomClickListener。如果您 传入监听器对象后,SDK 会改为调用其 onCustomClick 方法 并且不会采取进一步措施。但是,如果您传递一个 null 值作为监听器, SDK 会回退到在 SDK 中注册的深层链接和/或目标网址, 。

借助自定义点击监听器,您的应用可以决定 更新界面、启动新活动 而只是记录点击。下面的例子只记录了发生点击的 地点:

Java

AdLoader adLoader = new AdLoader.Builder(context, "/21775744923/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, "/21775744923/example/native")
    .forCustomFormatAd("10063170",
        { ad ->
            // Display the ad.
        },
        { ad, assetName ->
                Log.i("MyApp", "A custom click just happened for $assetName!")
    }).build()

乍一看,存在自定义点击监听器似乎很奇怪。毕竟, 您的应用刚刚告诉 SDK 发生了一次点击,为什么 SDK 应该改为 并向应用报告?

这种信息流的用处有多个,但最重要的是 让 SDK 可以保持对点击响应的控制。它可以 系统会自动对为 以及在后台处理其他任务,无需任何 额外的代码