Skip to content

GAM (DFP) MRect Ads

Can Soykarafakılı edited this page Nov 30, 2023 · 5 revisions

MRect Ads will be rendered using GAMBannerView through the HyBidGAMMRectCustomEvent adapter.

Requirements

  • Ad Zone ID from the PubNative Publisher Dashboard
  • GAM 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 GAMBannerView

  1. Declare a GAMBannerView *gamMrect property.

Swift:

var gamMrect: GAMBannerView!

Objective-C:

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

Swift:

self.gamMrect = GAMBannerView(adSize: GADAdSizeMediumRectangle)
self.gamMrect.adUnitID = <YOUR GAM AD UNIT ID HERE>

Objective-C:

self.gamMrect = [[GAMBannerView alloc] initWithAdSize:GADAdSizeMediumRectangle];
self.gamMrect.adUnitID = <YOUR GAM AD UNIT ID HERE>;
  1. Register your view controller as the gamMrect's delegate (GADBannerViewDelegate).

Swift:

self.gamMrect.delegate = self

Objective-C:

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

Swift:

self.gamMrect.rootViewController = self

Objective-C:

self.gamMrect.rootViewController = self;

For more information about GAM MRect integration, you can refer to the GAM Guide as well.

Create HyBid Ad Request

  1. Import HyBid into your class.

Swift:

import HyBid

Objective-C:

#import <HyBid/HyBid.h>
#if __has_include(<HyBid/HyBid-Swift.h>)
    #import <HyBid/HyBid-Swift.h>
#else
    #import "HyBid-Swift.h"
#endif
  1. Declare a HyBidAdRequest *mRectAdRequest property.

Swift:

var mRectAdRequest =  HyBidAdRequest()

Objective-C:

@property (nonatomic, strong) HyBidAdRequest *mRectAdRequest;
  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 mRectAdRequest's delegate (HyBidAdRequestDelegate).

Swift:

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

Objective-C:

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

Request Ad from GAM

After the ad is successfully received from PubNative, the request should be made to GAM with some parameters that will help the ad be chosen properly in the GAM 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 = GAMRequest()
        request.customTargeting = HyBidHeaderBiddingUtils.createHeaderBiddingKeywordsDictionary(with: ad, with: TWO_DECIMAL_PLACES)
        self.gamMrect.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.mRectAdRequest) {
        GAMRequest *request = [GAMRequest request];
        request.customTargeting = [HyBidHeaderBiddingUtils createHeaderBiddingKeywordsDictionaryWithAd:ad withKeywordMode:TWO_DECIMAL_PLACES];
        [self.gamMrect loadRequest:request];
    }
}

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

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


<- Banner Ads            -> Interstitial Ads

Home

Installations & Configurations

Displaying Ads with HyBid

Displaying Ads with Header Bidding with GAM

Mediation

Advanced Setup

Integration Checklists for Publishers

Misc

Clone this wiki locally