DFP Banners - pubnative/pubnative-hybid-android-sdk GitHub Wiki

Banners will be rendered using PublisherAdView through the HyBidDFPBannerCustomEvent adapter.

Requirements:

  • Ad Zone Id from the PubNative Publisher Dashboard
  • DFP Ad Unit Id for the ad placement that you want to request.

Code sample

You can find a demo app with code samples for this type of integration here.

Creating the ad view

Create a PublisherAdView attribute to hold the reference to the UI element.

private PublisherAdView mDFPBanner;

There are two ways to add the PublisherAdView to your user interface.

Create view in XML layout

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_banner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="DFP_AD_UNIT_ID"/>

Create a reference to the ad view in your code

mDFPBanner = findViewById(R.id.dfp_banner);

Create view programmatically

Create a container for the ad view inside your layout file

<FrameLayout
    android:id="@+id/dfp_banner_container"
    android:layout_width="320dp"
    android:layout_height="50dp"
    android:layout_gravity="center_horizontal"/>

Create a reference to the container in your code and the PublisherAdView programmatically

FrameLayout mDFPBannerContainer = findViewById(R.id.dfp_banner_container);
mDFPBanner = new PublisherAdView(this);
mDFPBanner.setAdUnitId("DFP_AD_UNIT_ID");
mDFPBanner.setAdSizes(AdSize.BANNER);
mDFPBannerContainer.addView(dfpBanner)

Set up the DFP ad event listener

Add an instance of AdListener to your ad view

mDFPBanner.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 HyBid Ad Request

Create a RequestManager to handle the request to the ad server.

private void loadBanner() {
    RequestManager bannerRequestManager = new BannerRequestManager();
    bannerRequestManager.setZoneId("ZONE_ID");
    bannerRequestManager.setRequestListener(new RequestManager.RequestListener() {
        @Override
        public void onRequestSuccess(Ad ad) {
            // Here you will handle the request to DFP. 
        }

        @Override
        public void onRequestFail(Throwable throwable) {

        }
    });

    bannerRequestManager.requestAd();
}

Request ad to DFP

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 HeaderBiddingUtils must be used so the SDK can generate the proper keywords for the received ad.

bannerRequestManager.setRequestListener(new RequestManager.RequestListener() {
    @Override
    public void onRequestSuccess(Ad ad) {
        PublisherAdRequest.Builder builder = new PublisherAdRequest.Builder();

        Set<String> keywordSet = HeaderBiddingUtils.getHeaderBiddingKeywordsSet(ad, KeywordMode.TWO_DECIMALS);
        for (String key: keywordSet) {
            builder.addKeyword(key);
        }

        Bundle keywordBundle = HeaderBiddingUtils.getHeaderBiddingKeywordsBundle(ad, KeywordMode.TWO_DECIMALS);
        for (String key: keywordBundle.keySet()) {
            builder.addCustomTargeting(key, keywordBundle.getString(key));
        }

        PublisherAdRequest adRequest = builder.build();
        mDFPBanner.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 HyBidDFPBannerCustomEvent adapter will be called to render the ad.


-> Rectangle Banners

⚠️ **GitHub.com Fallback** ⚠️