设置

借助自定义事件,您可以为不受支持的广告联盟添加广告瀑布流中介。为此,您可以为要集成的广告联盟实现自定义事件适配器。

前提条件

您必须先将以下广告格式之一集成到您的应用中,然后才能创建自定义事件:

在界面中创建自定义事件

您必须先在 Ad Manager 界面中创建自定义事件。请参阅创建和管理收益组中的说明。

您需要提供以下信息:

类名称

实现自定义事件适配器的类的完全限定名称,例如 com.google.ads.mediation.sample.customevent.SampleCustomEvent。我们建议的最佳做法是,为所有自定义事件广告格式使用同一适配器类。

标签

定义广告来源的唯一名称。

参数

传递给自定义事件适配器的可选字符串参数。

初始化适配器

Google 移动广告 SDK 初始化时,系统会针对在 Ad Manager 界面中为应用配置的所有支持的第三方适配器和自定义事件调用 initialize()。您可以使用此方法对自定义事件所需的第三方 SDK 执行任何必要的设置或初始化操作。

Java

package com.google.ads.mediation.sample.customevent;

import com.google.android.gms.ads.mediation.Adapter;
import com.google.android.gms.ads.mediation.InitializationCompleteCallback;
import com.google.android.gms.ads.mediation.MediationConfiguration;

public class SampleAdNetworkCustomEvent extends Adapter {
  private static final String SAMPLE_AD_UNIT_KEY = "parameter";

  @Override
  public void initialize(Context context,
      InitializationCompleteCallback initializationCompleteCallback,
      List<MediationConfiguration> mediationConfigurations) {
    // 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.
    initializationCompleteCallback.onInitializationSucceeded();
  }
}

Kotlin

package com.google.ads.mediation.sample.customevent

import com.google.android.gms.ads.mediation.Adapter
import com.google.android.gms.ads.mediation.InitializationCompleteCallback
import com.google.android.gms.ads.mediation.MediationConfiguration

class SampleCustomEvent : Adapter() {
  private val SAMPLE_AD_UNIT_KEY = "parameter"

  override fun initialize(
    context: Context,
    initializationCompleteCallback: InitializationCompleteCallback,
    mediationConfigurations: List<MediationConfiguration>
  ) {
    // 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.
    initializationCompleteCallback.onInitializationSucceeded()
  }
}

报告版本号

所有自定义事件都必须向 Google 移动广告 SDK 报告自定义事件适配器本身的版本,以及与该自定义事件对接的第三方 SDK 的版本。报告版本时使用的是 VersionInfo 对象:

Java

package com.google.ads.mediation.sample.customevent;

public class SampleCustomEvent extends Adapter {

  @Override
  public VersionInfo getVersionInfo() {
    String versionString = new VersionInfo(1, 2, 3);
    String[] splits = versionString.split("\\.");

    if (splits.length >= 4) {
      int major = Integer.parseInt(splits[0]);
      int minor = Integer.parseInt(splits[1]);
      int micro = Integer.parseInt(splits[2]) * 100 + Integer.parseInt(splits[3]);
      return new VersionInfo(major, minor, micro);
    }

    return new VersionInfo(0, 0, 0);
  }

  @Override
  public VersionInfo getSDKVersionInfo() {
    String versionString = SampleAdRequest.getSDKVersion();
    String[] splits = versionString.split("\\.");

    if (splits.length >= 3) {
      int major = Integer.parseInt(splits[0]);
      int minor = Integer.parseInt(splits[1]);
      int micro = Integer.parseInt(splits[2]);
      return new VersionInfo(major, minor, micro);
    }

    return new VersionInfo(0, 0, 0);
  }
}

Kotlin

package com.google.ads.mediation.sample.customevent

class SampleCustomEvent : Adapter() {
  override fun getVersionInfo(): VersionInfo {
    val versionString = VersionInfo(1,2,3).toString()
    val splits: List<String> = versionString.split("\\.")

    if (splits.count() >= 4) {
      val major = splits[0].toInt()
      val minor = splits[1].toInt()
      val micro = (splits[2].toInt() * 100) + splits[3].toInt()
      return VersionInfo(major, minor, micro)
    }

    return VersionInfo(0, 0, 0)
  }

  override fun getSDKVersionInfo(): VersionInfo {
    val versionString = VersionInfo(1,2,3).toString()
    val splits: List<String> = versionString.split("\\.")

    if (splits.count() >= 3) {
      val major = splits[0].toInt()
      val minor = splits[1].toInt()
      val micro = splits[2].toInt()
      return VersionInfo(major, minor, micro)
    }

    return VersionInfo(0, 0, 0)
  }
}

请求广告

若要请求广告,请参阅适用于具体广告格式的操作说明: