設定

使用自訂事件,即可為非支援的廣告聯播網新增刊登序列中介服務。方法是為要整合的廣告聯播網導入自訂事件轉接程式。

您可以在我們的 GitHub 存放區中找到完整的範例自訂事件專案。

必要條件

您必須先在應用程式中整合下列其中一種廣告格式,才能建立自訂事件:

在 UI 中建立自訂事件

您必須先在 AdMob UI 中建立自訂事件。相關操作說明請參閱: 新增自訂事件

您需要提供下列資訊:

類別名稱

實作自訂事件轉接器的類別完整名稱,例如 SampleCustomEvent;如果您的類別是透過 Swift 導入,則為 MediationExample.SampleCustomEventSwift

如果專案中有多個目標,或是 專案名稱與目標名稱不同。有了目標名稱 看起來會像這樣:appName_targetName.className。此外,請記得將任何非英數字元 (例如連字號) 替換為底線。示例

標籤

用於定義廣告來源的專屬名稱。

參數

傳送至自訂事件轉接程式的選用字串引數。

導入 GADMediationAdapter

建立自訂事件的第一步是導入 GADMediationAdapter 通訊協定,如本例中的 SampleCustomEvent 類別所示。

這個類別負責接收來自 AdMob 的訊息,並委派建立正確廣告格式的責任。

初始化轉接器

Google Mobile Ads SDK 初始化時 setUpWithConfiguration:completionHandler: 會在所有支援的第三方轉接程式和自訂事件上叫用 調整設定使用這個方法,針對自訂事件的必要第三方 SDK 執行任何必要的設定或初始化作業。

Swift

import GoogleMobileAds

class SampleCustomEvent: NSObject, GADMediationAdapter {

  static func setUpWith(
    _ configuration: GADMediationServerConfiguration,
    completionHandler: @escaping GADMediationAdapterSetUpCompletionBlock
  ) {
    // This is where you will initialize the SDK that this custom event is built
    // for. Upon finishing the SDK initialization, call the completion handler
    // with success.
    completionHandler(nil)
  }
}

Objective-C

#import "SampleCustomEvent.h"

@implementation SampleCustomEvent

+ (void)setUpWithConfiguration:(nonnull GADMediationServerConfiguration *)configuration
             completionHandler:(nonnull GADMediationAdapterSetUpCompletionBlock)completionHandler {
  // This is where you initialize the SDK that this custom event is built
  // for. Upon finishing the SDK initialization, call the completion handler
  // with success.
  completionHandler(nil);
}

回報版本號碼

所有自訂事件都必須向 Google Mobile Ads SDK 回報自訂事件轉接器本身的版本,以及自訂事件介面所連結的第三方 SDK 版本。版本回報為 GADVersionNumber 物件:

Swift

static func adSDKVersion() -> GADVersionNumber {
  let versionComponents = String(SampleSDKVersion).components(
    separatedBy: ".")

  if versionComponents.count >= 3 {
    let majorVersion = Int(versionComponents[0]) ?? 0
    let minorVersion = Int(versionComponents[1]) ?? 0
    let patchVersion = Int(versionComponents[2]) ?? 0

    return GADVersionNumber(
      majorVersion: majorVersion, minorVersion: minorVersion, patchVersion: patchVersion)
  }

  return GADVersionNumber()
}

static func adapterVersion() -> GADVersionNumber {
  let versionComponents = String(SampleAdSDK.SampleAdSDKVersionNumber).components(
    separatedBy: ".")
  var version = GADVersionNumber()
  if versionComponents.count == 4 {
    version.majorVersion = Int(versionComponents[0]) ?? 0
    version.minorVersion = Int(versionComponents[1]) ?? 0
    version.patchVersion = Int(versionComponents[2]) * 100 + Int(versionComponents[3])
  }
  return version
}

Objective-C

+ (GADVersionNumber)adSDKVersion {
  NSArray *versionComponents =
      [SampleSDKVersion componentsSeparatedByString:@"."];
  GADVersionNumber version = {0};
  if (versionComponents.count >= 3) {
    version.majorVersion = [versionComponents[0] integerValue];
    version.minorVersion = [versionComponents[1] integerValue];
    version.patchVersion = [versionComponents[2] integerValue];
  }
  return version;
}

+ (GADVersionNumber)adapterVersion {
  NSArray *versionComponents =
      [SampleCustomEventAdapterVersion componentsSeparatedByString:@"."];
  GADVersionNumber version = {0};
  if (versionComponents.count == 4) {
    version.majorVersion = [versionComponents[0] integerValue];
    version.minorVersion = [versionComponents[1] integerValue];
    version.patchVersion = [versionComponents[2] integerValue] * 100 +
                           [versionComponents[3] integerValue];
  }
  return version;
}

請求廣告

如要要求廣告,請參閱廣告格式的操作說明: