الأحداث المخصّصة لإعلانات البانر
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
المتطلبات الأساسية
أكمِل إعداد الأحداث المخصّصة.
طلب إعلان بانر
عند الوصول إلى عنصر العرض الخاص بالحدث المخصّص في سلسلة "توسّط العرض الإعلاني بدون انقطاع"، يتم استدعاء الطريقة 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
تلقّي عمليات ردّ الاتصال الخاصة بأحداث الإعلانات وإعداد تقارير عنها في حزمة "SDK لإعلانات Google على الأجهزة الجوّالة"
يتم تضمين المَعلمة الاختيارية المحدّدة في واجهة المستخدم في إعدادات الإعلان. يمكن الوصول إلى المَعلمة من خلال
adConfiguration.getServerParameters().getString(MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD)
.
تكون هذه المَعلمة عادةً معرّف وحدة إعلانية تتطلّبه حزمة تطوير البرامج (SDK) الخاصة بشبكة إعلانية عند إنشاء عنصر إعلان.
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()
.
يتم استدعاء onSuccess()
من خلال تمرير مثيل للفئة التي تنفّذ
MediationBannerAd
.
عادةً، يتم تنفيذ هذه الطرق داخل عمليات ردّ الاتصال من حزمة تطوير البرامج (SDK) التابعة لجهة خارجية التي ينفّذها المحوّل. في هذا المثال، تحتوي حزمة تطوير البرامج (SDK) النموذجية على SampleAdListener
مع عمليات رد اتصال ذات صلة:
Java
@Override
public void onAdFetchSucceeded() {
bannerAdCallback = mediationAdLoadCallback.onSuccess(this);
}
@Override
public void onAdFetchFailed(SampleErrorCode errorCode) {
mediationAdLoadCallback.onFailure(SampleCustomEventError.createSampleSdkError(errorCode));
}
يتطلّب MediationBannerAd
تنفيذ طريقة الحصول على View
:
Java
@Override
@NonNull
public View getView() {
return sampleAdView;
}
بعد استدعاء onSuccess()
، يمكن للمحوّل استخدام العنصر MediationBannerAdCallback
الذي تم عرضه
لإعادة توجيه أحداث العرض من حزمة تطوير البرامج (SDK) التابعة لجهة خارجية إلى حزمة تطوير البرامج (SDK) لـ "إعلانات Google على الأجهزة الجوّالة". يمتد الصف
SampleBannerCustomEventLoader
إلى واجهة SampleAdListener
لإعادة توجيه عمليات رد الاتصال من شبكة الإعلانات النموذجية إلى حزمة تطوير البرامج (SDK) لإعلانات Google على الأجهزة الجوّالة.
من المهم أن يعيد توجيه الحدث المخصّص أكبر عدد ممكن من عمليات معاودة الاتصال هذه، كي يتلقّى تطبيقك الأحداث المكافئة من حزمة SDK لإعلانات Google على الأجهزة الجوّالة. في ما يلي مثال على استخدام عمليات معاودة الاتصال:
Java
@Override
public void onAdFullScreen() {
bannerAdCallback.onAdOpened();
bannerAdCallback.reportAdClicked();
}
@Override
public void onAdClosed() {
bannerAdCallback.onAdClosed();
}
بهذا تكون قد أكملت عملية تنفيذ الأحداث المخصّصة لـ "إعلانات البانر". يتوفّر المثال الكامل على GitHub.
يمكنك استخدامها مع شبكة إعلانية متوافقة أو تعديلها لعرض إعلانات بانر للأحداث المخصّصة.
إنّ محتوى هذه الصفحة مرخّص بموجب ترخيص Creative Commons Attribution 4.0 ما لم يُنصّ على خلاف ذلك، ونماذج الرموز مرخّصة بموجب ترخيص Apache 2.0. للاطّلاع على التفاصيل، يُرجى مراجعة سياسات موقع Google Developers. إنّ Java هي علامة تجارية مسجَّلة لشركة Oracle و/أو شركائها التابعين.
تاريخ التعديل الأخير: 2025-08-31 (حسب التوقيت العالمي المتفَّق عليه)
[null,null,["تاريخ التعديل الأخير: 2025-08-31 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\u003cp\u003eTo request banner ads with custom events, extend the \u003ccode\u003eAdapter\u003c/code\u003e class and implement the \u003ccode\u003eloadBannerAd()\u003c/code\u003e method, delegating the ad loading process to a separate loader class.\u003c/p\u003e\n"],["\u003cp\u003eThe loader class is responsible for fetching the ad, handling ad events, and implementing the \u003ccode\u003eMediationBannerAd\u003c/code\u003e interface to interact with the Google Mobile Ads SDK.\u003c/p\u003e\n"],["\u003cp\u003eCustom events receive server parameters from Ad Manager, allowing them to be configured with ad unit IDs or other necessary information.\u003c/p\u003e\n"],["\u003cp\u003eUpon successful ad fetch, the loader class invokes \u003ccode\u003eonSuccess()\u003c/code\u003e on the \u003ccode\u003eMediationAdLoadCallback\u003c/code\u003e, providing an instance of the \u003ccode\u003eMediationBannerAd\u003c/code\u003e implementation.\u003c/p\u003e\n"],["\u003cp\u003eIt's crucial for custom events to forward relevant ad events from the third-party ad network to the Google Mobile Ads SDK using the \u003ccode\u003eMediationBannerAdCallback\u003c/code\u003e.\u003c/p\u003e\n"]]],["To request a banner ad, extend the `Adapter` class, implementing `loadBannerAd()`, and create a new class implementing `MediationBannerAd`. The `loadBannerAd()` method calls a loader class (e.g., `SampleBannerCustomEventLoader`) to load the ad, manage configuration, and handle callbacks. This loader class also implements `MediationBannerAd`, loading the banner and invoking `MediationAdLoadCallback` methods. The UI-defined custom event parameters are accessed via ad configuration. Upon success or failure, `onSuccess()` or `onFailure()` are invoked, and the `MediationBannerAdCallback` is utilized to forward third-party SDK presentation events.\n"],null,["Prerequisites\n\nComplete the [custom events setup](/ad-manager/mobile-ads-sdk/android/custom-events/setup).\n\nRequest a banner ad\n\nWhen the custom event line item is reached in the waterfall mediation chain,\nthe `loadBannerAd()` method is called on the class name you provided when\n[creating a custom\nevent](custom-events/setup#create). In this case,\nthat method is in `SampleCustomEvent`, which then calls the `loadBannerAd()`\nmethod in `SampleBannerCustomEventLoader`.\n\nTo request a banner ad, create or modify a class that extends `Adapter` to\nimplement `loadBannerAd()`. Additionally, create a new class to implement\n`MediationBannerAd`.\n\nIn our [custom event example](//github.com/googleads/googleads-mobile-android-mediation/blob/main/Example/customevent/src/main/java/com/google/ads/mediation/sample/customevent/SampleCustomEvent.java),\n`SampleCustomEvent` extends the `Adapter` class and then delegates to\n`SampleBannerCustomEventLoader`. \n\nJava \n\n```java\npackage com.google.ads.mediation.sample.customevent;\n\nimport com.google.android.gms.ads.mediation.Adapter;\nimport com.google.android.gms.ads.mediation.MediationAdConfiguration;\nimport com.google.android.gms.ads.mediation.MediationAdLoadCallback;\nimport com.google.android.gms.ads.mediation.MediationBannerAd;\nimport com.google.android.gms.ads.mediation.MediationBannerAdCallback;\n...\n\npublic class SampleCustomEvent extends Adapter {\n private SampleBannerCustomEventLoader bannerLoader;\n @Override\n public void loadBannerAd(\n @NonNull MediationBannerAdConfiguration adConfiguration,\n @NonNull MediationAdLoadCallback\u003cMediationBannerAd, MediationBannerAdCallback\u003e callback) {\n bannerLoader = new SampleBannerCustomEventLoader(adConfiguration, callback);\n bannerLoader.loadAd();\n }\n}\n```\n\n`SampleBannerCustomEventLoader` is responsible for the following tasks:\n\n- Loading the banner ad and invoking a\n [`MediationAdLoadCallback`](/ad-manager/mobile-ads-sdk/android/reference/com/google/android/gms/ads/mediation/MediationAdLoadCallback)\n method once loading completes.\n\n- Implementing the `MediationBannerAd` interface.\n\n- Receiving and reporting ad event callbacks to Google Mobile Ads SDK.\n\nThe optional parameter defined in the UI is\nincluded in the ad configuration. The parameter can be accessed through\n`adConfiguration.getServerParameters().getString(MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD)`.\nThis parameter is typically an ad unit identifier that an ad network SDK\nrequires when instantiating an ad object. \n\nJava \n\n```java\npackage com.google.ads.mediation.sample.customevent;\n\nimport com.google.android.gms.ads.mediation.Adapter;\nimport com.google.android.gms.ads.mediation.MediationBannerAdConfiguration;\nimport com.google.android.gms.ads.mediation.MediationAdLoadCallback;\nimport com.google.android.gms.ads.mediation.MediationBannerAd;\nimport com.google.android.gms.ads.mediation.MediationBannerAdCallback;\n...\n\npublic class SampleBannerCustomEventLoader extends SampleAdListener implements MediationBannerAd {\n\n /** View to contain the sample banner ad. */\n private SampleAdView sampleAdView;\n\n /** Configuration for requesting the banner ad from the third-party network. */\n private final MediationBannerAdConfiguration mediationBannerAdConfiguration;\n\n /** Callback that fires on loading success or failure. */\n private final MediationAdLoadCallback\u003cMediationBannerAd, MediationBannerAdCallback\u003e\n mediationAdLoadCallback;\n\n /** Callback for banner ad events. */\n private MediationBannerAdCallback bannerAdCallback;\n\n /** Constructor. */\n public SampleBannerCustomEventLoader(\n @NonNull MediationBannerAdConfiguration mediationBannerAdConfiguration,\n @NonNull MediationAdLoadCallback\u003cMediationBannerAd, MediationBannerAdCallback\u003e\n mediationAdLoadCallback) {\n this.mediationBannerAdConfiguration = mediationBannerAdConfiguration;\n this.mediationAdLoadCallback = mediationAdLoadCallback;\n }\n\n /** Loads a banner ad from the third-party ad network. */\n public void loadAd() {\n // All custom events have a server parameter named \"parameter\" that returns\n // back the parameter entered into the UI when defining the custom event.\n Log.i(\"BannerCustomEvent\", \"Begin loading banner ad.\");\n String serverParameter =\n mediationBannerAdConfiguration.getServerParameters().getString(\n MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD);\n\n Log.d(\"BannerCustomEvent\", \"Received server parameter.\");\n\n Context context = mediationBannerAdConfiguration.getContext();\n sampleAdView = new SampleAdView(context);\n\n // Assumes that the serverParameter is the ad unit of the Sample Network.\n sampleAdView.setAdUnit(serverParameter);\n AdSize size = mediationBannerAdConfiguration.getAdSize();\n\n // Internally, smart banners use constants to represent their ad size, which\n // means a call to AdSize.getHeight could return a negative value. You can\n // accommodate this by using AdSize.getHeightInPixels and\n // AdSize.getWidthInPixels instead, and then adjusting to match the device's\n // display metrics.\n int widthInPixels = size.getWidthInPixels(context);\n int heightInPixels = size.getHeightInPixels(context);\n DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();\n int widthInDp = Math.round(widthInPixels / displayMetrics.density);\n int heightInDp = Math.round(heightInPixels / displayMetrics.density);\n\n sampleAdView.setSize(new SampleAdSize(widthInDp, heightInDp));\n sampleAdView.setAdListener(this);\n\n SampleAdRequest request = createSampleRequest(mediationBannerAdConfiguration);\n Log.i(\"BannerCustomEvent\", \"Start fetching banner ad.\");\n sampleAdView.fetchAd(request);\n }\n\n public SampleAdRequest createSampleRequest(\n MediationAdConfiguration mediationAdConfiguration) {\n SampleAdRequest request = new SampleAdRequest();\n request.setTestMode(mediationAdConfiguration.isTestRequest());\n request.setKeywords(mediationAdConfiguration.getMediationExtras().keySet());\n return request;\n }\n}\n```\n\nDepending on whether the ad is successfully fetched or encounters an error, you\nwould call either\n[`onSuccess()`](/ad-manager/mobile-ads-sdk/android/reference/com/google/android/gms/ads/mediation/MediationAdLoadCallback#onSuccess(MediationAdT))\nor\n[`onFailure()`](/ad-manager/mobile-ads-sdk/android/reference/com/google/android/gms/ads/mediation/MediationAdLoadCallback#onFailure(com.google.android.gms.ads.AdError)).\n`onSuccess()` is called by passing in an instance of the class that implements\n`MediationBannerAd`.\n\nTypically, these methods are implemented inside callbacks from the\nthird-party SDK your adapter implements. For this example, the Sample SDK\nhas a `SampleAdListener` with relevant callbacks: \n\nJava \n\n```java\n@Override\npublic void onAdFetchSucceeded() {\n bannerAdCallback = mediationAdLoadCallback.onSuccess(this);\n}\n\n@Override\npublic void onAdFetchFailed(SampleErrorCode errorCode) {\n mediationAdLoadCallback.onFailure(SampleCustomEventError.createSampleSdkError(errorCode));\n}\n```\n\n`MediationBannerAd` requires implementing a `View` getter method: \n\nJava \n\n```java\n@Override\n@NonNull\npublic View getView() {\n return sampleAdView;\n}\n```\n\nForward mediation events to Google Mobile Ads SDK\n\nOnce `onSuccess()` is called, the returned `MediationBannerAdCallback` object\ncan then be used by the adapter to forward presentation events from the\nthird-party SDK to Google Mobile Ads SDK. The\n`SampleBannerCustomEventLoader` class extends the `SampleAdListener` interface\nto forward callbacks from the sample ad network to Google Mobile Ads SDK.\n\nIt's important that your custom event forwards as many of these callbacks as\npossible, so that your app receives these equivalent events from the Google\nMobile Ads SDK. Here's an example of using callbacks: \n\nJava \n\n```java\n@Override\npublic void onAdFullScreen() {\n bannerAdCallback.onAdOpened();\n bannerAdCallback.reportAdClicked();\n}\n\n@Override\npublic void onAdClosed() {\n bannerAdCallback.onAdClosed();\n}\n```\n\nThis completes the custom events implementation for banner ads. The full example\nis available on\n[GitHub](//github.com/googleads/googleads-mobile-android-mediation/tree/master/Example/customevent/src/main/java/com/google/ads/mediation/sample/customevent).\nYou can use it with an ad network that is already supported or modify it to\ndisplay custom event banner ads."]]