擷取廣告回應的相關資訊

廣告載入或載入失敗後,請檢查 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) 輸入這個 ID,找出並封鎖特定廣告。

讀取廣告來源回應資訊

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 UI 中為這個轉接程式設定的伺服器端參數。
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);
      }
    }
  }
}