“展示”类型的自定义原生广告格式和开放式衡量

如果您打算将开放式衡量与不含视频素材资源的自定义原生广告格式搭配使用,则需要自行调用开放式衡量 API。只有 7.43.0 及更高版本支持针对展示类型的自定义原生广告格式使用开放式衡量。如果您将自定义原生广告格式与视频素材资源搭配使用,则无需遵循本指南,因为 Google 移动广告 SDK 会代表您跟踪视频素材资源的可见度。

前提条件

加载广告

无论您是否使用公开衡量,广告加载方式都是相同的。在本例中,我们将使用一个简单的 ViewController 来演示如何加载 GADNativeCustomFormatAd

@interface OpenMeasurementNativeCustomFormatAdViewController ()
   
<GADNativeCustomFormatAdLoaderDelegate> {
 
IBOutlet UIView *_parentView;
 
GADAdLoader *_adLoader;
 
GADNativeCustomFormatAd *_customTemplateAd;
 
MySimpleNativeAdView *_simpleNativeAdView;
}

@end

@implementation OpenMeasurementNativeCustomFormatAdViewController

- (void) viewDidLoad {
 
[super viewDidLoad];

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

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

注册您的视图并开始衡量

展示 GADNativeCustomFormatAd 时,您需要使用 displayAdMeasurement.view 属性将自定义广告视图注册到 GADNativeTemplateAd

您还需要明确告知 SDK 开始衡量广告。为此,请对 GADNativeCustomFormatAddisplayAdMeasurement 属性调用 startWithError: 方法。必须从主线程调用 startWithError:,后续调用将不起作用。

@implementation OpenMeasurementNativeCustomFormatAdViewController
...
#pragma mark - GADNativeCustomFormatAdLoaderDelegate

- (void) adLoader:(GADAdLoader *) adLoader
    didReceiveNativeCustomFormatAd
:(GADNativeCustomFormatAd *)nativeCustomFormatAd {
 
NSLog(@"Received custom native ad: %@", nativeCustomFormatAd);

  _customTemplateAd
= nativeCustomFormatAd;

 
// 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 GADNativeCustomFormatAd 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

这就是全部内容!发布应用后,您将开始接收衡量数据,不过,只有在您完成 IAB 认证流程后,您的数据才能获得认证。