Share your feedback about the Google Mobile Ads SDK! Take the annual survey.

Display-type Custom Native Ad Formats and Open Measurement

This guide explains how to integrate custom native ad formats with open measurement.

Prerequisites

  • You've read Open Measurement with the Mobile Ads SDK.
  • You know how to use custom native ad formats.
  • You've configured your viewability provider for your Ad Manager network.
  • You're using version 18.1.1 or later of the Google Mobile Ads SDK.

Integration

If you plan to use open measurement with custom native ad formats that don’t contain a video asset, you'll be responsible for calling the Open Measurement APIs yourself. Open measurement for display-type custom native ad formats is only supported by the Mobile Ads SDK version 18.1.1 and later.

If you're using custom native ad formats with a video asset, you don't need to follow this guide; the Mobile Ads SDK tracks viewability of the video asset on your behalf.

Let's now look at how to use open measurement with NativeCustomTemplateAd. In general, you need to alert the Mobile Ads SDK when you want to begin viewability measurement and to register your custom ad view.

Load an ad

Loading an ad is the same whether you're using open measurement or not. Here is a method that demonstrates how to request a NativeCustomTemplateAd:

public void loadAd() {
   AdLoader adLoader = new AdLoader.Builder(context, "YOUR-AD-UNIT-ID")
    .forCustomTemplateAd("YOUR-TEMPLATE-ID",
      new NativeCustomTemplateAd.OnCustomTemplateAdLoadedListener() {
          @Override
          public void onCustomTemplateAdLoaded(NativeCustomTemplateAd ad) {
              // Register your view and begin open measurement.
              // We will define this method below.
              MyActivity.this.registerOpenMeasurementAndShowAd(ad);
          }
      },
      new NativeCustomTemplateAd.OnCustomClickListener() {
          @Override
          public void onCustomClick(NativeCustomTemplateAd ad, String s) {
              // Handle the click action
          }
      })
    .withAdListener( ... )
    .withNativeAdOptions( ... )
    .build();
}

Register your view and begin measuring

To enable open measurement on a NativeCustomTemplateAd, register your custom ad view with the DisplayOpenMeasurement object associated with the NativeCustomTemplateAd. The DisplayOpenMeasurement object provides the setView() method to register your container view with open measurement.

You also need to explicitly tell the SDK to begin measuring your ad. To do this call the start() method on the DisplayOpenMeasurement object of your NativeCustomTemplateAd. The start() method must be called from the main thread, and subsequent calls have no effect.

Here's what it looks like:

public void  registerOpenMeasurementAndShowAd(NativeCustomTemplateAd ad) {

...
// Show ad
...
displayCustomTemplateAd(ad)
// Begin Open Measurement
// The FrameLayout that will contain your native custom ad
FrameLayout customTemplateAdFrame = (FrameLayout) findViewById(R.id.custom_ad_frame);
ad.getDisplayOpenMeasurement().setView(customTemplateAdFrame);
ad.getDisplayOpenMeasurement().start();

}

Note that you should display your ad before registering your view. Displaying your ad is covered in Custom Native Ad Formats.

The customTemplateAdFrame layout is the ad container for the NativeCustomTemplateAd and must contain all assets within the bounding box of the view.

Certify with IAB

Once you release your app, you will begin receiving measurement data; however, your data won't be certified until you complete the IAB certification process.