Rectangle Banners will be rendered using PublisherAdView through the HyBidDFPMRectCustomEvent 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 mDFPMRect;

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

Create a reference to the ad view in your code

mDFPMRect = findViewById(R.id.dfp_mrect);

Create view programmatically

Create a container for the ad view inside your layout file

<FrameLayout
    android:id="@+id/dfp_mrect_container"
    android:layout_width="300dp"
    android:layout_height="250dp"
    android:layout_gravity="center_horizontal"/>

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

FrameLayout mDFPMRectContainer = findViewById(R.id.dfp_mrect_container);
mDFPMRect = new PublisherAdView(this);
mDFPMRect.setAdUnitId("DFP_AD_UNIT_ID");
mDFPMRect.setAdSizes(AdSize.MEDIUM_RECTANGLE);
mDFPMRectContainer.addView(mDFPMRect)

Set up the DFP ad event listener

Add an instance of AdListener to your ad view

mDFPMRect.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 loadMRect() {
    RequestManager mRectRequestManager = new MRectRequestManager();
    mRectRequestManager.setZoneId("ZONE_ID");
    mRectRequestManager.setRequestListener(new RequestManager.RequestListener() {
        @Override
        public void onRequestSuccess(Ad ad) {
            // Here you will handle the request to DFP. 
        }

        @Override
        public void onRequestFail(Throwable throwable) {

        }
    });

    mRectRequestManager.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.

mRectRequestManager.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();
        mDFPMRect.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 HyBidDFPMRectCustomEvent adapter will be called to render the ad.

Return to the main page