設定

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

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

必要條件

建立自訂事件之前,必須先將下列廣告格式整合至應用程式:

在使用者介面中建立自訂事件

您必須先在 AdMobUI 中建立自訂事件。請參閱 新增自訂事件中的操作說明。

您必須提供以下資訊:

類別名稱

實作自訂事件轉接程式的類別完整名稱,例如 SampleCustomEvent;或者您的類別是以 Swift、MediationExample.SampleCustomEventSwift 來實作。

如果專案中有多個目標,或是專案名稱與目標名稱不同,就必須提供目標名稱。若是目標名稱,看起來會像這樣:appName_targetName.className。此外,請記得將所有非英數字元 (如破折號) 替換為底線。範例

標籤

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

參數

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

導入 GADMediationAdapter

建立自訂事件的第一步是實作 GADMediationAdapter 通訊協定,如範例所示 SampleCustomEvent 類別所示。

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

初始化轉接器

Google Mobile Ads SDK 初始化時,系統會針對 AdMob UI 內為應用程式設定的所有支援第三方轉接程式和自訂事件叫用 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;
}

請求廣告

如要提出廣告請求,請參閱該廣告格式的專屬操作說明: