DFP Leaderboard Banners - pubnative/pubnative-hybid-android-sdk GitHub Wiki
Rectangle Banners will be rendered using PublisherAdView through the HyBidDFPLeaderboardCustomEvent adapter.
Requirements:
- Ad Zone Id from the PubNative Publisher Dashboard
- DFP Ad Unit Id for the ad placement that you want to request.
Create a PublisherAdView attribute to hold the reference to the UI element.
private PublisherAdView mDFPLeaderboard;
There are two ways to add the PublisherAdView to your user interface.
Create a PublisherAdView view inside your layout file.
<com.google.android.gms.ads.doubleclick.PublisherAdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/dfp_leaderboard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="LEADERBOARD"
ads:adUnitId="DFP_AD_UNIT_ID"/>
Create a reference to the ad view in your code
mDFPLeaderboard = findViewById(R.id.dfp_leaderboard);
Create a container for the ad view inside your layout file
<FrameLayout
android:id="@+id/dfp_leaderboard_container"
android:layout_width="728dp"
android:layout_height="90dp"
android:layout_gravity="center_horizontal"/>
Create a reference to the container in your code and the PublisherAdView programmatically
FrameLayout mDFPLeaderboardContainer = findViewById(R.id.dfp_leaderboard_container);
mDFPLeaderboard = new PublisherAdView(this);
mDFPLeaderboard.setAdUnitId("DFP_AD_UNIT_ID");
mDFPLeaderboard.setAdSizes(AdSize.LEADERBOARD);
mDFPLeaderboardContainer.addView(mDFPLeaderboard)
Add an instance of AdListener to your ad view
mDFPLeaderboard.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
}
@Override
public void onAdFailedToLoad(int i) {
}
@Override
public void onAdImpression() {
}
@Override
public void onAdClicked() {
}
@Override
public void onAdOpened() {
}
@Override
public void onAdLeftApplication() {
}
@Override
public void onAdClosed() {
}
});
Create a RequestManager to handle the request to the ad server.
private void loadLeaderboard() {
RequestManager mLeaderboardRequestManager = new LeaderboardRequestManager();
mLeaderboardRequestManager.setZoneId("ZONE_ID");
mLeaderboardRequestManager.setRequestListener(new RequestManager.RequestListener() {
@Override
public void onRequestSuccess(Ad ad) {
// Here you will handle the request to DFP.
}
@Override
public void onRequestFail(Throwable throwable) {
}
});
mLeaderboardRequestManager.requestAd();
}
After the ad is successfully received from PubNative, the request should be made to DFP with some parameters that will help the ad be chosen properly in the DFP waterfall.
The PrebidUtils must be used so the SDK can generate the proper keywords for the received ad.
mLeaderboardRequestManager.setRequestListener(new RequestManager.RequestListener() {
@Override
public void onRequestSuccess(Ad ad) {
PublisherAdRequest.Builder builder = new PublisherAdRequest.Builder();
Set<String> keywordSet = PrebidUtils.getPrebidKeywordsSet(ad, KeywordMode.TWO_DECIMALS);
for (String key: keywordSet) {
builder.addKeyword(key);
}
Bundle keywordBundle = PrebidUtils.getPrebidKeywordsBundle(ad, KeywordMode.TWO_DECIMALS);
for (String key: keywordBundle.keySet()) {
builder.addCustomTargeting(key, keywordBundle.getString(key));
}
PublisherAdRequest adRequest = builder.build();
mDFPLeaderboard.loadAd(adRequest);
}
@Override
public void onRequestFail(Throwable throwable) {
}
});
After making this request to DFP, it will run it's waterfall and if the line item targeted by our keywords gets chosen, the HyBidDFPLeaderboardCustomEvent adapter will be called to render the ad.