Combining Custom-Rendered Native Ad and Banner Ad Requests

With a few changes to your code, you can combine native and banner ads in your ad requests.

Prerequisites

  • Version 7.20.0 or higher of the Google Mobile Ads SDK
  • Complete the Get Started guide

Loading an ad

Custom-rendered native ads are loaded via GADAdLoader objects. The GADAdLoader object can also be configured to make ad requests that can result in either a banner or native ad. Adding GADAdLoaderAdTypeGAMBanner to the adTypes array parameter, along with native ad types such as GADAdLoaderAdTypeNative when creating the GADAdLoader object specifies that banner ads should compete with native ads to fill the request.

Swift

adLoader = GADAdLoader(adUnitID: "/21775744923/example/native-and-banner",
    rootViewController: self,
    adTypes: [.native, .gamBanner],
    options: [... ad loader options objects ...])
adLoader.delegate = self

Objective-C

self.adLoader = [[GADAdLoader alloc]
      initWithAdUnitID:@"/21775744923/example/native-and-banner"
    rootViewController:rootViewController
               adTypes:@[ GADAdLoaderAdTypeNative, GADAdLoaderAdTypeGAMBanner ]
               options:@[ ... ad loader options objects ... ]];
self.adLoader.delegate = self;

GAMBannerAdLoaderDelegate

When requesting banner ads via the GADAdLoader, the ad loader delegate must conform to the GAMBannerAdLoaderDelegate protocol. This protocol includes a message that's sent when a banner ad has loaded:

Swift

public func adLoader(_ adLoader: GADAdLoader,
    didReceive GAMBannerView: GAMBannerView)

Objective-C

- (void)adLoader:(GADAdLoader *)adLoader didReceiveGAMBannerView:(GAMBannerView *)bannerView;

The ad loader delegate must also specify which banner ad sizes should be requested by responding to the validBannerSizesForAdLoader message as shown below.

Swift

public func validBannerSizes(for adLoader: GADAdLoader) -> [NSValue] {
  return [NSValueFromGADAdSize(GADAdSizeBanner),
    NSValueFromGADAdSize(GADAdSizeMediumRectangle),
    NSValueFromGADAdSize(GADAdSizeFromCGSize(CGSize(width: 120, height: 20)))]
}

Objective-C

- (NSArray *)validBannerSizesForAdLoader:(GADAdLoader *)adLoader {
  return @[
    @(GADAdSizeBanner),
    @(GADAdSizeMediumRectangle),
    @(GADAdSizeFromCGSize(CGSizeMake(120, 20)))
  ];
}

Manual impression counting

To enable manual impression counting on banner ads loaded through GADAdLoader, set a GAMBannerViewOptions with enableManualImpressions set to YES when initializing GADAdLoader.

Swift

let bannerViewOptions = GAMBannerViewOptions()
bannerViewOptions.enableManualImpressions = true
adLoader = GADAdLoader(
  adUnitID: "/21775744923/example/native-and-banner", rootViewController: self,
  adTypes: [.native, .gamBanner], options: [bannerViewOptions])

Objective-C

GAMBannerViewOptions *bannerViewOptions = [[GAMBannerViewOptions alloc] init];
bannerViewOptions.enableManualImpressions = YES;
self.adLoader = [[GADAdLoader alloc]
      initWithAdUnitID:@"/21775744923/example/native-and-banner"
    rootViewController:self
               adTypes:@[ GADAdLoaderAdTypeNative, GADAdLoaderAdTypeGAMBanner ]
               options:@[ bannerViewOptions ]];

If a banner ad loads, you can call recordManualImpression when you determine that an ad has been successfully returned and is on-screen to manually fire an impression:

Swift

bannerView.recordImpression()

Objective-C

[self.bannerView recordImpression];