橫幅廣告自訂事件

必要條件

完成自訂事件設定

要求橫幅廣告

到達刊登序列中介服務鏈中的自訂事件委刊項時, loadBannerAd() 方法會在您提供的類別名稱上呼叫 建立自訂 事件。在本例中 該方法位於 SampleCustomEvent 中,然後會呼叫 loadBannerAd() SampleBannerCustomEventLoader 中的方法。

如要請求橫幅廣告,請建立或修改可擴充 Adapter 的類別 實作 loadBannerAd()。此外,請建立要實作的新類別 MediationBannerAd

自訂事件範例中,SampleCustomEvent 會擴充 Adapter 類別,然後委派至 SampleBannerCustomEventLoader

Java

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

import com.google.android.gms.ads.mediation.Adapter;
import com.google.android.gms.ads.mediation.MediationAdConfiguration;
import com.google.android.gms.ads.mediation.MediationAdLoadCallback;
import com.google.android.gms.ads.mediation.MediationBannerAd;
import com.google.android.gms.ads.mediation.MediationBannerAdCallback;
...

public class SampleCustomEvent extends Adapter {
  private SampleBannerCustomEventLoader bannerLoader;
  @Override
  public void loadBannerAd(
      @NonNull MediationBannerAdConfiguration adConfiguration,
      @NonNull MediationAdLoadCallback<MediationBannerAd, MediationBannerAdCallback> callback) {
    bannerLoader = new SampleBannerCustomEventLoader(adConfiguration, callback);
    bannerLoader.loadAd();
  }
}

SampleBannerCustomEventLoader 負責下列工作:

  • 載入橫幅廣告,並在載入完成後叫用 MediationAdLoadCallback 方法。

  • 實作 MediationBannerAd 介面。

  • 接收並回報對 Google Mobile Ads SDK 的廣告事件回呼。

UI 中定義的選用參數為 加進廣告設定中參數可以透過 adConfiguration.getServerParameters().getString(MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD)。 這個參數通常是廣告聯播網 SDK 提供的廣告單元 ID 需要建立廣告物件的例項。

Java

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

import com.google.android.gms.ads.mediation.Adapter;
import com.google.android.gms.ads.mediation.MediationBannerAdConfiguration;
import com.google.android.gms.ads.mediation.MediationAdLoadCallback;
import com.google.android.gms.ads.mediation.MediationBannerAd;
import com.google.android.gms.ads.mediation.MediationBannerAdCallback;
...

public class SampleBannerCustomEventLoader extends SampleAdListener implements MediationBannerAd {

  /** View to contain the sample banner ad. */
  private SampleAdView sampleAdView;

  /** Configuration for requesting the banner ad from the third-party network. */
  private final MediationBannerAdConfiguration mediationBannerAdConfiguration;

  /** Callback that fires on loading success or failure. */
  private final MediationAdLoadCallback<MediationBannerAd, MediationBannerAdCallback>
      mediationAdLoadCallback;

  /** Callback for banner ad events. */
  private MediationBannerAdCallback bannerAdCallback;

  /** Constructor. */
  public SampleBannerCustomEventLoader(
      @NonNull MediationBannerAdConfiguration mediationBannerAdConfiguration,
      @NonNull MediationAdLoadCallback<MediationBannerAd, MediationBannerAdCallback>
              mediationAdLoadCallback) {
    this.mediationBannerAdConfiguration = mediationBannerAdConfiguration;
    this.mediationAdLoadCallback = mediationAdLoadCallback;
  }

  /** Loads a banner ad from the third-party ad network. */
  public void loadAd() {
    // All custom events have a server parameter named "parameter" that returns
    // back the parameter entered into the UI when defining the custom event.
    Log.i("BannerCustomEvent", "Begin loading banner ad.");
    String serverParameter =
        mediationBannerAdConfiguration.getServerParameters().getString(
        MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD);

    Log.d("BannerCustomEvent", "Received server parameter.");

    Context context = mediationBannerAdConfiguration.getContext();
    sampleAdView = new SampleAdView(context);

    // Assumes that the serverParameter is the ad unit of the Sample Network.
    sampleAdView.setAdUnit(serverParameter);
    AdSize size = mediationBannerAdConfiguration.getAdSize();

    // Internally, smart banners use constants to represent their ad size, which
    // means a call to AdSize.getHeight could return a negative value. You can
    // accommodate this by using AdSize.getHeightInPixels and
    // AdSize.getWidthInPixels instead, and then adjusting to match the device's
    // display metrics.
    int widthInPixels = size.getWidthInPixels(context);
    int heightInPixels = size.getHeightInPixels(context);
    DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
    int widthInDp = Math.round(widthInPixels / displayMetrics.density);
    int heightInDp = Math.round(heightInPixels / displayMetrics.density);

    sampleAdView.setSize(new SampleAdSize(widthInDp, heightInDp));
    sampleAdView.setAdListener(this);

    SampleAdRequest request = createSampleRequest(mediationBannerAdConfiguration);
    Log.i("BannerCustomEvent", "Start fetching banner ad.");
    sampleAdView.fetchAd(request);
  }

  public SampleAdRequest createSampleRequest(
      MediationAdConfiguration mediationAdConfiguration) {
    SampleAdRequest request = new SampleAdRequest();
    request.setTestMode(mediationAdConfiguration.isTestRequest());
    request.setKeywords(mediationAdConfiguration.getMediationExtras().keySet());
    return request;
  }
}

視廣告是否成功擷取或發生錯誤而定 會將 onSuccess()onFailure()。 您可以傳入實作 MediationBannerAd 的類別例項,呼叫 onSuccess()

通常,這些方法會在轉接程式實作的第三方 SDK 回呼中實作。在這個範例中 具有包含相關回呼的 SampleAdListener

Java

@Override
public void onAdFetchSucceeded() {
  bannerAdCallback = mediationAdLoadCallback.onSuccess(this);
}

@Override
public void onAdFetchFailed(SampleErrorCode errorCode) {
  mediationAdLoadCallback.onFailure(SampleCustomEventError.createSampleSdkError(errorCode));
}

MediationBannerAd 需要實作 View getter 方法:

Java

@Override
@NonNull
public View getView() {
  return sampleAdView;
}

將中介服務事件轉送至 Google Mobile Ads SDK

呼叫 onSuccess() 後,轉接器就能使用傳回的 MediationBannerAdCallback 物件,將第三方 SDK 的呈現事件轉送至 Google Mobile Ads SDK。SampleBannerCustomEventLoader 類別會擴充 SampleAdListener 介面,將來自範例廣告聯播網的回呼轉送至 Google Mobile Ads SDK。

請務必讓自訂事件盡可能順利轉送這些回呼,例如: ,讓應用程式從 Google Play 遊戲收到這些對等事件 Mobile Ads SDK以下是回呼的使用範例:

Java

@Override
public void onAdFullScreen() {
  bannerAdCallback.onAdOpened();
  bannerAdCallback.reportAdClicked();
}

@Override
public void onAdClosed() {
  bannerAdCallback.onAdClosed();
}

這樣就能完成橫幅廣告的自訂事件導入。您可以在 GitHub 上找到完整範例。您可以將其與已支援的廣告聯播網搭配使用,也可以修改該廣告單元,以便顯示自訂事件橫幅廣告。