Display-type custom native ad formats and Open Measurement

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

Prerequisites

Before you continue, do the following:

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.

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.

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 custom native ad:

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

Register your view and begin measuring

To enable open measurement on a custom native ad, register your custom ad view with the DisplayOpenMeasurement object associated with the custom native ad. 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 custom native ad. 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(NativeCustomFormatAd ad) {

...
// Show ad
...
displayCustomFormatAd(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();

}