Display-type Custom Native Ad Formats and Open Measurement

If you plan to use open measurement with custom native ad formats that don't contain a video asset, you'll be responsible for calling the Open Measurement APIs yourself. Open measurement for display-type custom native ad formats is only supported by version 7.43.0 and later. If you're using custom native ad formats with a video asset, you don't need to follow this guide—the Google Mobile Ads SDK tracks viewability of the video asset on your behalf.

Prerequisites

Load an ad

Loading an ad is the same whether you're using open measurement or not. In this case we'll use a simple ViewController to demonstrate loading a GADNativeCustomTemplateAd:

@interface OpenMeasurementNativeCustomTemplateAdViewController ()
    <GADNativeCustomTemplateAdLoaderDelegate> {
  IBOutlet UIView *_parentView;
  GADAdLoader *_adLoader;
  GADNativeCustomTemplateAd *_customTemplateAd;
  MySimpleNativeAdView *_simpleNativeAdView;
}

@end

@implementation OpenMeasurementNativeCustomTemplateAdViewController

- (void) viewDidLoad {
  [super viewDidLoad];

  _adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"your ad unit ID"
                                 rootViewController:self
                                            adTypes:@[ kGADAdLoaderAdTypeNativeCustomTemplate ]
                                            options:nil];
  _adLoader.delegate = self;
  [self loadAd];
}

- (void) loadAd {
  GAMRequest *request = [GAMRequest request];
  [_adLoader loadRequest:request];
}
...
@end

Register your view and begin measuring

When you show a GADNativeCustomTemplateAd, you need to register your custom ad view with the GADNativeTemplateAd using the displayAdMeasurement.view property.

You also need to explicitly tell the SDK to begin measuring your ad. To do this, call the startWithError: method on the displayAdMeasurement property of your GADNativeCustomTemplateAd. startWithError: must be called from the main thread, and subsequent calls have no effect.

@implementation OpenMeasurementNativeCustomTemplateAdViewController
...
#pragma mark - GADNativeCustomTemplateAdLoaderDelegate

- (void) adLoader:(GADAdLoader *) adLoader
    didReceiveNativeCustomTemplateAd:(GADNativeCustomTemplateAd *)nativeCustomTemplateAd {
  NSLog(@"Received custom native ad: %@", nativeCustomTemplateAd);

  _customTemplateAd = nativeCustomTemplateAd;

  // Put the custom native ad on screen.
  _simpleNativeAdView =
    [[NSBundle mainBundle] loadNibNamed:@"SimpleCustomNativeAdView"
                                  owner:nil
                                options:nil]
    .firstObject;
  [_parentView addSubview:_simpleNativeAdView];
  [_simpleNativeAdView populateWithCustomNativeAd:_customTemplateAd];


  // Set the top-level native ad view on the GADNativeCustomTemplateAd so the
  // Google Mobile Ads SDK can track viewability for that view.
  _customTemplateAd.displayAdMeasurement.view = _simpleNativeAdView;
  // Begin measuring your impressions and clicks.
  NSError *error = nil;
  [_customTemplateAd.displayAdMeasurement startWithError:&error];

  if (error) {
    NSLog(@"Failed to start the display measurement.");
  }
}
...
@end

That's all there is to it. Once you release your app you will begin receiving measurement data, however your data won't be certified until you go through the IAB certification process.