擷取廣告回應的相關資訊

如果是偵錯和記錄目的,成功載入的廣告會提供 GADResponseInfo 物件。這個物件包含載入的廣告的相關資訊,以及用於載入廣告的中介服務刊登序列相關資訊。

如果廣告成功載入,廣告物件具有 GADResponseInfo 屬性。舉例來說,GADInterstitialAd.responseInfo 會取得已載入插頁式廣告的回應資訊。

如果廣告無法載入且只有錯誤,可以使用錯誤 userInfo 字典上的 GADErrorUserInfoKeyResponseInfo 金鑰存取 GADResponseInfo

Swift

fileprivate func loadInterstitial() {
  GADInterstitialAd.load(
    withAdUnitID: "/6499/example/interstitial", request: request
  ) { (ad, error) in
    if let error = error {
      let responseInfo = (error as NSError).userInfo[GADErrorUserInfoKeyResponseInfo] as? GADResponseInfo
      print("\(String(describing: responseInfo))")
      return
    }
    let responseInfo = ad?.responseInfo
    print("\(String(describing: responseInfo))")
  }
}

Objective-C

- (void)loadInterstitial {
  [GADInterstitialAd
   loadWithAdUnitID:@"/6499/example/interstitial"
   request:request
   completionHandler:^(GADInterstitialAd *ad, NSError *error) {
    if (error) {
      GADResponseInfo *responseInfo = error.userInfo[GADErrorUserInfoKeyResponseInfo];
      NSLog(@"%@", responseInfo.description);
      return;
    }
    GADResponseInfo *responseInfo = ad.responseInfo;
    NSLog(@"%@", responseInfo.description);
  }];
}

回覆資訊

以下是輸出廣告傳回的偵錯資料範例:

(  ** Response Info **
    Response ID: CLz5r-KMtfoCFQvv7QodfGAMHw
    Network: GADMAdapterGoogleAdMobAds

  ** Loaded Adapter Response **
    Network: GADMAdapterGoogleAdMobAds
    Ad Source Name:Reservation campaign
    Ad Source ID:7068401028668408324
    Ad Source Instance Name:[DO NOT EDIT] Publisher Test Interstitial
    Ad Source Instance ID:[DO NOT EDIT] Publisher Test Interstitial
    AdUnitMapping:
{
}
    Error: (null)
    Latency: 0.357

  ** Extras Dictionary **
    {
        "mediation_group_name" = Campaign;
    }

  ** Mediation line items **
    Entry (1)
    Network: GADMAdapterGoogleAdMobAds
    Ad Source Name:Reservation campaign
    Ad Source ID:7068401028668408324
    Ad Source Instance Name:[DO NOT EDIT] Publisher Test Interstitial
    Ad Source Instance ID:[DO NOT EDIT] Publisher Test Interstitial
    AdUnitMapping:
{
}
    Error: (null)
    Latency: 0.357
)

GADResponseInfo 中的屬性包括:

屬性 說明
adNetworkInfoArray 傳回 GADAdNetworkResponseInfo 清單,其中包含廣告回應中每個轉接程式的中繼資料。可用於對刊登序列中介服務和出價執行作業進行偵錯。清單的順序與這個廣告請求的中介服務刊登序列順序相符。

詳情請參閱轉接程式回應資訊

loadedAdNetworkResponseInfo 傳回與載入廣告轉接程式的對應 GADAdNetworkResponseInfo
adNetworkClassName 傳回載入廣告廣告聯播網的中介服務轉接程式類別名稱。
responseIdentifier 回應 ID 是廣告回應的專屬 ID,這個 ID 可用來在廣告審核中心內找出並封鎖廣告。
extrasDictionary

傳回與廣告回應有關的額外資訊。

Swift

fileprivate func loadInterstitial() {
  GADInterstitialAd.load(
    withAdUnitID: "/6499/example/interstitial", request: request
  ) { (ad, error) in
    let responseInfo = ad?.responseInfo

    let responseIdentifier = responseInfo?.responseIdentifier
    let adNetworkClassName = responseInfo?.adNetworkClassName
    let adNetworkInfoArray = responseInfo?.adNetworkInfoArray
    let loadedAdNetworkResponseInfo = responseInfo?.loadedAdNetworkResponseInfo
  }
}

Objective-C

- (void)loadInterstitial {
  [GADInterstitialAd
   loadWithAdUnitID:@"/6499/example/interstitial"
   request:request
   completionHandler:^(GADInterstitialAd *ad, NSError *error) {
    GADResponseInfo *responseInfo = ad.responseInfo;

    NSString *responseIdentifier = responseInfo.responseIdentifier;
    NSString *adNetworkClassName = responseInfo.adNetworkClassName;
    NSArray *adNetworkInfoArray = responseInfo.adNetworkInfoArray;
    GADAdNetworkResponseInfo *loadedAdNetworkResponseInfo = responseInfo.loadedAdNetworkResponseInfo;
  }];
}

轉接器回應資訊

GADAdNetworkResponseInfo 包含廣告回應中每個轉接程式的中繼資料,可用於偵錯刊登序列中介服務和出價。清單的順序與廣告請求中介服務刊登序列的順序相符。

以下是 GADAdNetworkResponseInfo 輸出範例:

(    Network: GADMAdapterGoogleAdMobAds
    Ad Source Name:Reservation campaign
    Ad Source ID:7068401028668408324
    Ad Source Instance Name:[DO NOT EDIT] Publisher Test Interstitial
    Ad Source Instance ID:[DO NOT EDIT] Publisher Test Interstitial
    AdUnitMapping:
{
}
    Error: (null)
    Latency: 0.277
)

每個廣告聯播網的 GADAdNetworkResponseInfo 都會提供下列屬性:

屬性 說明
error 與網路要求相關聯的錯誤。如果網路已成功載入廣告,或是未嘗試網路,則傳回 nil
adSourceId 與這個轉接程式回應相關聯的廣告來源 ID。
adSourceInstanceId 與這個轉接程式回應相關聯的廣告來源執行個體 ID。
adSourceInstanceName 與這個轉接程式回應相關聯的廣告來源執行個體名稱。
adSourceName 代表提供曝光的特定廣告聯播網。
adNetworkClassName 用來識別廣告聯播網的類別名稱。
adUnitMapping 透過 Ad Manager UI 設定的網路設定。
latency 廣告聯播網載入廣告的時間長度。如未嘗試網路,則傳回 0

Swift

fileprivate func loadInterstitial() {
  GADInterstitialAd.load(
    withAdUnitID: "/6499/example/interstitial", request: request
  ) { (ad, error) in
    let responseInfo = ad?.responseInfo
    let loadedAdNetworkResponseInfo = responseInfo?.loadedAdNetworkResponseInfo

    let adNetworkError = loadedAdNetworkResponseInfo?.error
    let adSourceId = loadedAdNetworkResponseInfo?.adSourceID
    let adSourceInstanceId = loadedAdNetworkResponseInfo?.adSourceInstanceID
    let adSourceInstanceName = loadedAdNetworkResponseInfo?.adSourceInstanceName
    let adSourceName = loadedAdNetworkResponseInfo?.adSourceName
    let adNetworkClassName = loadedAdNetworkResponseInfo?.adNetworkClassName
    let adUnitMapping = loadedAdNetworkResponseInfo?.adUnitMapping
    let latency = loadedAdNetworkResponseInfo?.latency
  }
}

Objective-C

- (void)loadInterstitial {
  [GADInterstitialAd
   loadWithAdUnitID:@"/6499/example/interstitial"
   request:request
   completionHandler:^(GADInterstitialAd *ad, NSError *error) {
    GADResponseInfo *responseInfo = ad.responseInfo;
    GADAdNetworkResponseInfo *loadedAdNetworkResponseInfo = responseInfo.loadedAdNetworkResponseInfo;

    NSError *adNetworkError = loadedAdNetworkResponseInfo.error;
    NSString *adSourceId = loadedAdNetworkResponseInfo.adSourceID;
    NSString *adSourceInstanceId = loadedAdNetworkResponseInfo.adSourceInstanceID;
    NSString *adSourceInstanceName = loadedAdNetworkResponseInfo.adSourceInstanceName;
    NSString *adSourceName = loadedAdNetworkResponseInfo.adSourceName;
    NSString *adNetworkClassName = loadedAdNetworkResponseInfo.adNetworkClassName;
    NSDictionary *adUnitMapping = loadedAdNetworkResponseInfo.adUnitMapping;
    NSTimeInterval latency = loadedAdNetworkResponseInfo.latency;
  }];
}