広告レスポンスに関する情報を取得する

広告の読み込みが完了または失敗したら、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);
      }
    }
  }
}