必要條件
完成自訂事件設定。
要求橫幅廣告
當瀑布式中介鏈中到達自訂事件明細時,系統會根據您在建立自訂事件時提供的類別名稱,呼叫 loadBannerAd()
方法。在本例中,該方法位於 SampleCustomEvent
中,然後呼叫 SampleBannerCustomEventLoader
中的 loadBannerAd()
方法。
如要要求橫幅廣告,請建立或修改可擴充 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 回呼中實作。在本例中,Sample 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 Mobile Ads SDK 接收這些對等事件。以下是使用回呼的範例:
Java
@Override public void onAdFullScreen() { bannerAdCallback.onAdOpened(); bannerAdCallback.reportAdClicked(); } @Override public void onAdClosed() { bannerAdCallback.onAdClosed(); }
這就完成了橫幅廣告的自訂事件實作。您可以在 GitHub 上找到完整範例。您可以搭配已支援的廣告聯播網使用,也可以修改廣告活動,以便顯示自訂事件橫幅廣告。