插頁式廣告自訂事件

必要條件

完成自訂事件設定

請求插頁式廣告

當瀑布式中介鏈中到達自訂事件明細時,系統會根據您在建立自訂事件時提供的類別名稱,呼叫 loadInterstitialAd() 方法。在本例中,該方法位於 SampleCustomEvent 中,然後在 SampleInterstitialCustomEventLoader 中呼叫 loadInterstitialAd() 方法。

如要要求插頁式廣告,請建立或修改會擴充 Adapter 以實作 loadInterstitialAd() 的類別。此外,請建立新類別來實作 MediationInterstitialAd

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

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.MediationInterstitialAd;
import com.google.android.gms.ads.mediation.MediationInterstitialAdCallback;
...

public class SampleCustomEvent extends Adapter {
  private SampleInterstitialCustomEventLoader interstitialLoader;
  @Override
  public void loadInterstitialAd(
      @NonNull MediationInterstitialAdConfiguration adConfiguration,
      @NonNull
          MediationAdLoadCallback<MediationInterstitialAd, MediationInterstitialAdCallback>
              callback) {
    interstitialLoader = new SampleInterstitialCustomEventLoader(adConfiguration, callback);
    interstitialLoader.loadAd();
  }
}

SampleInterstitialCustomEventLoader 負責執行以下工作:

  • 載入插頁式廣告,並在載入完成後叫用 MediationAdLoadCallback 方法。

  • 實作 MediationInterstitialAd 介面。

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

AdMob 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.MediationInterstitialAdConfiguration;
import com.google.android.gms.ads.mediation.MediationAdLoadCallback;
import com.google.android.gms.ads.mediation.MediationInterstitialAd;
import com.google.android.gms.ads.mediation.MediationInterstitialAdCallback;
...

public class SampleInterstitialCustomEventLoader extends SampleAdListener
    implements MediationInterstitialAd {

  /** A sample third-party SDK interstitial ad. */
  private SampleInterstitial sampleInterstitialAd;

  /** Configuration for requesting the interstitial ad from the third-party network. */
  private final MediationInterstitialAdConfiguration mediationInterstitialAdConfiguration;

  /** Callback for interstitial ad events. */
  private MediationInterstitialAdCallback interstitialAdCallback;

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

  /** Constructor. */
  public SampleInterstitialCustomEventLoader(
      @NonNull MediationInterstitialAdConfiguration mediationInterstitialAdConfiguration,
      @NonNull MediationAdLoadCallback<MediationInterstitialAd, MediationInterstitialAdCallback>
              mediationAdLoadCallback) {
    this.mediationInterstitialAdConfiguration = mediationInterstitialAdConfiguration;
    this.mediationAdLoadCallback = mediationAdLoadCallback;
  }

  /** Loads the interstitial 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("InterstitialCustomEvent", "Begin loading interstitial ad.");
    String serverParameter = mediationInterstitialAdConfiguration.getServerParameters().getString(
        MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD);
    Log.d("InterstitialCustomEvent", "Received server parameter.");

    sampleInterstitialAd =
        new SampleInterstitial(mediationInterstitialAdConfiguration.getContext());
    sampleInterstitialAd.setAdUnit(serverParameter);

    // Implement a SampleAdListener and forward callbacks to mediation.
    sampleInterstitialAd.setAdListener(this);

    // Make an ad request.
    Log.i("InterstitialCustomEvent", "start fetching interstitial ad.");
    sampleInterstitialAd.fetchAd(
        SampleCustomEvent.createSampleRequest(mediationInterstitialAdConfiguration));
  }

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

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

一般來說,這些方法會在轉接程式實作的第三方 SDK 回呼中實作。在本例中,Sample SDK 具有含有相關回呼的 SampleAdListener

Java

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

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

MediationInterstitialAd 需要實作 showAd() 方法才能顯示廣告:

Java

@Override
public void showAd(@NonNull Context context) {
  sampleInterstitialAd.show();
}

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

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

請務必盡可能轉寄這些回呼,讓應用程式從 Google Mobile Ads SDK 接收這些對等事件。以下是使用回呼的範例:

Java

@Override
public void onAdFullScreen() {
  interstitialAdCallback.reportAdImpression();
  interstitialAdCallback.onAdOpened();
}

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

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