This guide explains how to integrate custom native ad formats with open measurement.
Prerequisites
- Google Mobile Ads SDK version 18.1.1 or higher.
- Read Open Measurement with the Mobile Ads SDK.
- Integrate custom native ad formats.
- Configure a viewability provider and assign it to your line item.
- Enter your partner name when creating a custom format in the Ad Manager UI.
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.
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 NativeCustomFormatAd
:
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 below.
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 NativeCustomFormatAd
, register your custom
ad view with the DisplayOpenMeasurement
object associated with the
NativeCustomFormatAd
. 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
NativeCustomFormatAd
. 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();
}
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
NativeCustomFormatAd
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.