必要條件
完成自訂事件設定。
請求插頁式廣告
到達刊登序列中介服務鏈中的自訂事件委刊項時,
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 的廣告事件回呼
在 Ad Manager 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()
。
系統會透過傳入實作的類別執行個體,呼叫 onSuccess()
MediationInterstitialAd
。
通常,這些方法是在來自
您的轉接程式導入的第三方 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 行動服務
廣告 SDK。
請務必讓自訂事件盡可能順利轉送這些回呼,例如: ,讓應用程式從 Google Play 遊戲收到這些對等事件 Mobile Ads SDK以下是回呼的使用範例:
Java
@Override public void onAdFullScreen() { interstitialAdCallback.reportAdImpression(); interstitialAdCallback.onAdOpened(); } @Override public void onAdClosed() { interstitialAdCallback.onAdClosed(); }
這樣就能完成插頁式廣告的自訂事件導入。完整 範例提供 GitHub。 您可以搭配目前支援的廣告聯播網使用,也可以將其修改為 多媒體自訂事件插頁式廣告。