DFP Leaderboard Ads (Pre Google Mobile Ads SDK v: 8.0.0) - pubnative/pubnative-hybid-ios-sdk GitHub Wiki

Leaderboard Ads will be rendered using DFPBannerView through the HyBidDFPHeaderBiddingLeaderboardCustomEvent adapter.

Requirements

  • Ad Zone ID from the PubNative Publisher Dashboard
  • DFP Ad Unit ID for the Ad Placement that you want to request.

Demo App

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

Create a DFPBannerView

  1. Declare a DFPBannerView *dfpLeaderboard property.

Swift:

var dfpLeaderboard: DFPBannerView!

Objective-C:

@property (nonatomic, strong) DFPBannerView *dfpLeaderboard;
  1. Instantiate the property that you have declared, and also set your DFP Ad Unit ID.

Swift:

self.dfpLeaderboard = DFPBannerView(adSize: kGADAdSizeLeaderboard)
self.dfpLeaderboard.adUnitID = <YOUR DFP AD UNIT ID HERE>

Objective-C:

self.dfpLeaderboard = [[DFPBannerView alloc] initWithAdSize:kGADAdSizeLeaderboard];
self.dfpLeaderboard.adUnitID = <YOUR DFP AD UNIT ID HERE>;
  1. Register your view controller as the dfpLeaderboard's delegate (GADBannerViewDelegate).

Swift:

self.dfpLeaderboard.delegate = self

Objective-C:

self.dfpLeaderboard.delegate = self;
  1. Register your view controller as the dfpLeaderboard's rootViewController.

Swift:

self.dfpLeaderboard.rootViewController = self

Objective-C:

self.dfpLeaderboard.rootViewController = self;

For more information about DFP Leaderboard integration, you can refer to the DFP Guide as well.

Create HyBid Ad Request

  1. Import HyBid into your class.

Swift:

import HyBid

Objective-C:

#import <HyBid/HyBid.h>
  1. Declare a HyBidAdRequest *leaderboardAdRequest property.

Swift:

var leaderboardAdRequest =  HyBidAdRequest()

Objective-C:

@property (nonatomic, strong) HyBidAdRequest *leaderboardAdRequest;
  1. Instantiate the property that you have declared. Before making a request, set its adSize property. After this, you can request an Ad by, passing your Ad Zone ID and also your registered view controller as the leaderboardAdRequest's delegate (HyBidAdRequestDelegate).

Swift:

self.leaderboardAdRequest.adSize = HyBidAdSize.size_728x90
self.leaderboardAdRequest.requestAd(with: self, withZoneID: <YOUR AD ZONE ID HERE>)

Objective-C:

self.leaderboardAdRequest = [[HyBidAdRequest alloc] init];
self.leaderboardAdRequest.adSize = HyBidAdSize.SIZE_728x90;
[self.leaderboardAdRequest requestAdWithDelegate:self withZoneID:<YOUR AD ZONE ID HERE>];

Request Ad from 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 HyBidHeaderBiddingUtils must be used so that the SDK can generate the proper keywords for the received ad.

Swift:

extension ViewController : HyBidAdRequestDelegate
{
    func requestDidStart(_ request: HyBidAdRequest!)
    {
        print("Request\(request) started")   
    }
    
    func request(_ request: HyBidAdRequest!, didLoadWith ad: HyBidAd!)
    {
        print("Request loaded with ad: \(ad)")
        let request = DFPRequest()
        request.customTargeting = HyBidHeaderBiddingUtils.createHeaderBiddingKeywordsDictionary(with: ad, with: TWO_DECIMAL_PLACES)
        self.dfpLeaderboard.load(request)
    }
    
    func request(_ request: HyBidAdRequest!, didFailWithError error: Error!)
    {
        print("Request\(request) failed with error: \(error.localizedDescription)")  
    }
}

Objective-C:


#pragma mark - HyBidAdRequestDelegate

- (void)requestDidStart:(HyBidAdRequest *)request
{
    NSLog(@"Request %@ started:",request);
}

- (void)request:(HyBidAdRequest *)request didLoadWithAd:(HyBidAd *)ad
{
    NSLog(@"Request loaded with ad: %@",ad);
    
    if (request == self.leaderboardAdRequest) {
        DFPRequest *request = [DFPRequest request];
        request.customTargeting = [HyBidHeaderBiddingUtils createHeaderBiddingKeywordsDictionaryWithAd:ad withKeywordMode:TWO_DECIMAL_PLACES];
        [self.dfpLeaderboard loadRequest:request];
    }
}

- (void)request:(HyBidAdRequest *)request didFailWithError:(NSError *)error
{
    NSLog(@"Request %@ failed with error: %@",request,error.localizedDescription);
}

After making this request to DFP, it will run its waterfall and if the line item targeted by our keywords gets chosen, the HyBidDFPHeaderBiddingLeaderboardCustomEvent adapter will be called to render the ad.


<- Setup DFP            -> Banner Ads (Pre Google Mobile Ads SDK v: 8.0.0)