This document describes the Prebid Mobile (PBM) Native In App Rendering capability. Though PBM has the ability to render native ad components through its Banner Ad object, PBM’s Native In App Rendering solution enables publishers to render the native assets in native code.
At a high level the in app rendering process works like this:
These instructions will enable you to create a creative template in either Google Ad Manager or MoPub that can then be applied to native ads in your app.
Delivery
and then Native
Create native ad
.Android & iOS app code
.ADD VARIABLE
and add the following variable names and placeholders.Variable Name | Place Holder |
---|---|
isPrebid | [%isPrebid%] |
hb_cache_id_local | [%hb_cache_id_local%] |
Make sure to indicate that the variables are required.
Delivery > Creatives
, and create a creative with Native Format
, choosing the template you created. In the user-defined variables you just created, set the following values:Variable Name | Value |
---|---|
isPrebid | 1 |
hb_cache_id_local | %%PATTERN:hb_cache_id_local%% |
hb_pb key-values
. Associate the creative you added in steps 4 thru 8 (making sure to choose your native format as expected creatives on the line item) to the ad unit you created in the second step.hb_pb key-value
.Native
.Easy Form
choose Manual JSON
.
{
"mainimage": "https://dummyimage.com/600x400/000/fff",
"isPrebid": true,
"hb_cache_id_local": "%%KEYWORD:hb_cache_id_local%%"
}
Save
.The PrebidNativeAdListener
interface provides three methods to handle the display and check the validity of the returned native ad.
onPrebidNativeLoaded
Use this method to pass a PrebidNativeAd
to inflate.
Parameters
Name | Scope | Type | Description |
---|---|---|---|
PrebidnativeAd | Required | ad | A PrebidNativeAd |
onPrebidNativeNotFound
Use this method when a Prebid Native ad is not found in the server returned response. The ad should be displayed as a regular AdUnit type.
onPrebidNativeNotValid
Use this method when a Prebid native ad was returned, but a PrebidNativeAd
object was not able to be created from the cached assets. Display different content.
An object representing the PrebidNativeAd
to be displayed.
Setting this option to true
, in your instance of Prebid Mobile, enables you to add an id for each asset in the assets array. The default setting is false
Kotlin
PrebidMobile.assignNativeAssetID(true)
Java
PrebidMobile.assignNativeAssetID(true);
registerView
Takes a View
that will handle the display of the native asset image and a listener
object.
Parameters
Name | Scope | Type | Description |
---|---|---|---|
view | Required | View | The view to display the native asset image in. |
final | Required | Listener | A PrebidNativeAdListener object. |
registerViewList
Parameters
Name | Scope | Type | Description |
---|---|---|---|
view | Required | View | The view to display the native asset image in. |
viewList | Required | List | A list of component views of native assets (title etc.) |
final | Required | Listener | A PrebidNativeAdListener object. |
unregister
Unregisters the View
stored for displaying the native asset’s image.
Use these getters to return various components of the native ad.
Name | Returns | Description |
---|---|---|
getTitle | String | Returns the title of the native ad. |
getDescription | String | Returns the description of the native ad. |
getIconUrl | String | Returns the path for the icon of the native ad. |
getImageUrl | String | Returns the path for the image of the native ad. |
getCallToAction | String | Returns the type of action of the native ad |
getClickUrl | String | Returns the click url of the native ad |
This object provides methods for searching for a PrebidNativeAd
in the response and displaying the image from the image url in the response.
findNative
This method searches for the variable isPrebid
in the native response. If the variable is located and its value is true
the PrebidNativeAd
instance is created and passed back to PrebidNativeAdListener
.
Parameters
Name | Scope | Type | Description |
---|---|---|---|
Object | Required | Object | |
PrebidNativeAdListener | Required | Listener |
PrebidNativeAdListener to pass the PrebidNativeAd if isPrebid = true. |
loadImage
This method displays the image from a url in the native response in an imageView supplied by the publisher.
Parameters
Name | Scope | Type | Description |
---|---|---|---|
imageView | Required | ImageView | The view that the image will be displayed in. |
imageUrl | Required | String | The path of the image to be displayed. |
private void inflatePrebidNativeAd(final PrebidNativeAd ad) {
LinearLayout nativeContainer = new LinearLayout(MainActivity.this);
nativeContainer.setOrientation(LinearLayout.VERTICAL);
LinearLayout iconAndTitle = new LinearLayout(MainActivity.this);
iconAndTitle.setOrientation(LinearLayout.HORIZONTAL);
ImageView icon = new ImageView(MainActivity.this);
icon.setLayoutParams(new LinearLayout.LayoutParams(160, 160));
Util.loadImage(icon, ad.getIconUrl());
iconAndTitle.addView(icon);
TextView title = new TextView(MainActivity.this);
title.setTextSize(20);
title.setText(ad.getTitle());
iconAndTitle.addView(title);
nativeContainer.addView(iconAndTitle);
ImageView image = new ImageView(MainActivity.this);
image.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Util.loadImage(image, ad.getImageUrl());
nativeContainer.addView(image);
TextView description = new TextView(MainActivity.this);
description.setTextSize(18);
description.setText(ad.getDescription());
nativeContainer.addView(description);
Button cta = new Button(MainActivity.this);
cta.setText(ad.getCallToAction());
cta.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(ad.getClickUrl()));
startActivity(browserIntent);
}
});
nativeContainer.addView(cta);
// add nativeContainer to the screen
// e.g. ((FrameLayout) MainActivity.this.findViewById(R.id.adFrame)).addView(nativeContainer);
}
AdLoader adLoader = new AdLoader.Builder(MainActivity.this, "/19968336/Wei_test_native_native")
.forPublisherAdView(new OnPublisherAdViewLoadedListener() {
@Override
public void onPublisherAdViewLoaded(PublisherAdView publisherAdView) {
adView = publisherAdView;
((FrameLayout) findViewById(R.id.adFrame)).addView(publisherAdView);
}
}, AdSize.BANNER)
.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
@Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
MainActivity.this.unifiedNativeAd = unifiedNativeAd;
// display unifiedNativeAd
}
})
.forCustomTemplateAd("11885766", new NativeCustomTemplateAd.OnCustomTemplateAdLoadedListener() { // You should be using your native format's template id
@Override
public void onCustomTemplateAdLoaded(NativeCustomTemplateAd nativeCustomTemplateAd) {
Util.findNative(nativeCustomTemplateAd, new PrebidNativeAdListener() {
@Override
public void onPrebidNativeLoaded(PrebidNativeAd ad) {
inflatePrebidNativeAd(ad);
}
@Override
public void onPrebidNativeNotFound() {
// inflate nativeCustomTemplateAd
}
@Override
public void onPrebidNativeNotValid() {
// show your own content
// this happens when not able to create PrebidNativeAd from hb_cache_id
}
});
}
}, new NativeCustomTemplateAd.OnCustomClickListener() {
@Override
public void onCustomClick(NativeCustomTemplateAd nativeCustomTemplateAd, String s) {
}
})
.withAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
}
})
.build();
PublisherAdRequest request = new PublisherAdRequest.Builder().build();
nativeAdUnit.fetchDemand(adView, new OnCompleteListener() {
@Override
public void onComplete(ResultCode resultCode) {
adLoader.loadAd(request);
}
});
MoPubNative mMoPubNative = new MoPubNative(MainActivity.this, "2674981035164b2db5ef4b4546bf3d49", new MoPubNative.MoPubNativeNetworkListener() {
@Override
public void onNativeLoad(final NativeAd nativeAd) {
MainActivity.this.ad = nativeAd;
Util.findNative(nativeAd, new PrebidNativeAdListener() {
@Override
public void onPrebidNativeLoaded(final PrebidNativeAd ad) {
inflatePrebidNativeAd(ad);
}
@Override
public void onPrebidNativeNotFound() {
infalteMoPubNativeAd(nativeAd);
}
@Override
public void onPrebidNativeNotValid() {
// should not show the NativeAd on the screen, do something else
}
});
}
@Override
public void onNativeFail(NativeErrorCode errorCode) {
}
});
mMoPubNative.registerAdRenderer(new MoPubStaticNativeAdRenderer(null));
RequestParameters mRP = new RequestParameters.Builder().build();
nativeAdUnit.fetchDemand(mRP, new OnCompleteListener() {
@Override
public void onComplete(ResultCode resultCode) {
mMoPubNative.makeRequest(mRP);
}
});