出于调试和日志记录目的,成功加载的广告会提供 GADResponseInfo
对象。此对象包含有关它所加载的广告的信息。每个广告格式类都具有一个用于获取响应信息的属性。例如,对于插页式广告,可以使用 responseInfo
属性获取此信息。
如果广告无法加载且系统只显示了一条错误,可使用错误的 userInfo
字典中的键 GADErrorUserInfoKeyResponseInfo
来提供 GADResponseInfo
。
Swift
func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) { let responseInfo = error.userInfo[GADErrorUserInfoKeyResponseInfo] as? GADResponseInfo }
Objective-C
- (void)adView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(GADRequestError *)error { GADResponseInfo *responseInfo = error.userInfo[GADErrorUserInfoKeyResponseInfo]; }
响应信息的属性
GADResponseInfo
对象的属性包括:
- responseIdentifier
- 广告响应的唯一标识符,可用于在广告查看中心 (ARC)
- adNetworkClassName
- 提取了当前广告的广告联盟的类名称。可通过此属性返回的值包括:
广告来源 类名称 Google Ads GADGoogleAdNetworkClassName
的值。激励广告自定义事件 您的自定义事件的类名称。 所有其他自定义事件 GADCustomEventAdNetworkClassName
的值。中介 中介适配器的类名称。 - adNetworkInfoArray
GADAdNetworkResponseInfo
的数组。此数组表示广告请求的中介广告瀑布流中收到的响应。对于广告瀑布流中的每个广告联盟,
GADAdNetworkResponseInfo
提供了以下内容:属性 说明 adNetworkClassName
用于标识广告联盟的类名称。 credentials
在 AdMob 界面中设置的广告联盟配置。 error
与向广告联盟发送的广告请求相关的错误。如果广告联盟成功加载广告或未尝试加载广告,则为 nil。 latency
广告联盟加载广告所花费的时间。如果广告联盟未尝试加载广告,则为 0
。通过查询此属性,您可以深入了解中介广告瀑布流针对每个广告请求的结果。
示例代码
以下是摘自 GADBannerViewDelegate
回调实现的示例代码段:
Swift
func adViewDidReceiveAd(_ bannerView: GADBannerView) { print("adViewDidReceiveAd") if let responseInfo = bannerView.responseInfo { print(responseInfo) } } func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) { print("didFailToReceiveAdWithError: \(error.localizedDescription)") // GADBannerView has the responseInfo property but this demonstrates accessing // response info from a returned NSError. if let responseInfo = error.userInfo[GADErrorUserInfoKeyResponseInfo] as? GADResponseInfo { print(responseInfo) } }
Objective-C
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView { NSLog(@"adViewDidReceiveAd"); NSLog(@"\n%@", responseInfo); } - (void)adView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(GADRequestError *)error { NSLog(@"didFailToReceiveAdWithError: %@", error.localizedDescription); // GADBannerView has the responseInfo property but this demonstrates accessing response info // from a returned NSError. GADResponseInfo *responseInfo = error.userInfo[GADErrorUserInfoKeyResponseInfo]; NSLog(@"\n%@", responseInfo); }
在使用 Google 移动广告 SDK 7.60.0 版或更高版本时,此代码会输出如下格式的显示内容:
** Response Info **
Response ID: 4a_iXpjAJcyN5LcPx6y4mAc
Network: GADMAdapterGoogleAdMobAds
** Mediation line items **
Entry (1)
Network: GADMAdapterGoogleAdMobAds
Credentials:
{
}
Error: (null)
Latency: 0.252