广告加载或加载失败后,请检查 ResponseInfo 对象。此对象详细说明了已加载的广告以及用于处理请求的中介广告瀑布流的元数据。
如果广告加载成功,广告实例会包含一个 getResponseInfo 方法。例如,interstitialAd.getResponseInfo 方法会返回已加载的 InterstitialAd 对象的响应信息。
如果广告加载失败,请访问 LoadAdError 对象上的可选 responseInfo 属性,例如 error.responseInfo。
检查响应信息对象
ResponseInfo 对象提供从广告请求返回的诊断详细信息。
以下示例展示了已加载广告的响应信息输出:
Android
{
"responseId": "COOllLGxlPoCFdAx4Aod-Q4A0g",
"responseExtras": {},
"loadedAdSourceResponseInfo": {
"adapterClassName": "com.google.ads.mediation.admob.AdMobAdapter",
"latencyMillis": 328,
"name": "Reservation campaign",
"id": "7068401028668408324",
"instanceName": "[DO NOT EDIT] Publisher Test Interstitial",
"instanceId": "4665218928925097",
"credentials": {},
"mediationAdError": null
},
"adSourceResponses": [
{
"adapterClassName": "com.google.ads.mediation.admob.AdMobAdapter",
"latencyMillis": 328,
"name": "Reservation campaign",
"id": "7068401028668408324",
"instanceName": "[DO NOT EDIT] Publisher Test Interstitial",
"instanceId": "4665218928925097",
"credentials": {},
"mediationAdError": null
}
]
}
iOS
{
"responseId": "COOllLGxlPoCFdAx4Aod-Q4A0g",
"responseExtras": {},
"loadedAdSourceResponseInfo": {
"adapterClassName": "GADMAdapterGoogleAdMobAds",
"latencyMillis": 328,
"name": "Reservation campaign",
"id": "7068401028668408324",
"instanceName": "[DO NOT EDIT] Publisher Test Interstitial",
"instanceId": "4665218928925097",
"credentials": {},
"mediationAdError": null
},
"adSourceResponses": [
{
"adapterClassName": "GADMAdapterGoogleAdMobAds",
"latencyMillis": 328,
"name": "Reservation campaign",
"id": "7068401028668408324",
"instanceName": "[DO NOT EDIT] Publisher Test Interstitial",
"instanceId": "4665218928925097",
"credentials": {},
"mediationAdError": null
}
]
}
ResponseInfo 对象提供以下属性:
| 属性 | 类型 | 说明 |
|---|---|---|
adSourceResponses |
readonly AdSourceResponseInfo[] |
AdSourceResponseInfo 对象列表,表示中介广告瀑布流中的每个广告来源尝试。 |
loadedAdSourceResponseInfo |
AdSourceResponseInfo | undefined |
成功加载广告的广告来源适配器的元数据。如果广告未加载,则此值为 undefined。 |
responseExtras |
Readonly<Record<string, unknown>> |
广告响应返回的额外元数据,例如 mediation_group_name 键。 |
responseId |
string |
广告响应的唯一标识符。使用此 ID 可在广告审核中心 (ARC) 中识别要屏蔽的广告。 |
读取广告来源响应信息
AdSourceResponseInfo 对象提供广告响应中包含的适配器的元数据。这些对象会按照中介广告瀑布流中每个广告来源的调用顺序进行列出。
以下示例展示了已加载广告的广告来源响应信息输出:
Android
{
"adapterClassName": "com.google.ads.mediation.admob.AdMobAdapter",
"latencyMillis": 328,
"name": "Reservation campaign",
"id": "7068401028668408324",
"instanceName": "[DO NOT EDIT] Publisher Test Interstitial",
"instanceId": "4665218928925097",
"credentials": {},
"mediationAdError": null
}
iOS
{
"adapterClassName": "GADMAdapterGoogleAdMobAds",
"latencyMillis": 328,
"name": "Reservation campaign",
"id": "7068401028668408324",
"instanceName": "[DO NOT EDIT] Publisher Test Interstitial",
"instanceId": "4665218928925097",
"credentials": {},
"mediationAdError": null
}
对于每个广告来源,AdSourceResponseInfo 都提供以下属性:
| 属性 | 类型 | 说明 |
|---|---|---|
adapterClassName |
string |
广告联盟适配器的类名称。 |
credentials |
Readonly<Record<string, string>> |
在 AdMob 界面中为此适配器配置的服务器端参数。 |
id |
string |
广告来源的唯一 ID。 |
instanceId |
string |
广告来源实例的唯一 ID。 |
instanceName |
string |
广告来源实例的名称。 |
latencyMillis |
number |
网络尝试所用的时间(以毫秒为单位)。 |
mediationAdError |
MediationAdError | undefined |
如果广告联盟尝试加载广告失败,此属性会描述错误。如果成功,此值为 undefined。 |
name |
string |
广告来源的名称,例如 "Reservation campaign"。 |
记录完整的响应信息
本部分介绍了如何检查已加载广告的完整响应信息。
以下示例会在广告加载或加载失败时记录并检查响应信息:
import {InterstitialAd, LoadAdError} from '@google/mobile-ads';
import type {AdSourceResponseInfo, ResponseInfo} from '@google/mobile-ads';
export async function logResponseInfo(adUnitId: string): Promise<void> {
try {
const interstitial = await InterstitialAd.load({adUnitId});
const responseInfo: ResponseInfo = interstitial.getResponseInfo();
console.log('Ad Loaded Response ID:', responseInfo.responseId);
console.log(
'Loaded Adapter Class Name:',
responseInfo.loadedAdSourceResponseInfo?.adapterClassName,
);
// Inspect each ad source response in the mediation waterfall
responseInfo.adSourceResponses.forEach(
(adSourceResponse: AdSourceResponseInfo, index: number) => {
console.log(
`Waterfall index ${index}:`,
adSourceResponse.adapterClassName,
`Latency: ${adSourceResponse.latencyMillis}ms`,
adSourceResponse.mediationAdError
? `Error: ${adSourceResponse.mediationAdError.message}`
: 'Success',
);
},
);
} catch (error) {
if (error instanceof LoadAdError) {
console.error('Ad failed to load:', error.message);
if (error.responseInfo) {
console.log('Failed Response ID:', error.responseInfo.responseId);
}
}
}
}