אירועים בהתאמה אישית של מודעות מתגמלות

דרישות מוקדמות

משלימים את הגדרת האירועים המותאמים אישית.

שליחת בקשה להצגת מודעה מתגמלת

כשמגיעים לפריט המותאם אישית של האירוע בשרשרת ה-Waterfall לבחירת רשת, מתבצעת קריאה לשיטה loadRewardedAd() בשם המחלקה שציינתם כשיצרתם אירוע מותאם אישית. במקרה הזה, ה-method נמצא ב-SampleCustomEvent, ואז הוא קורא ל-method‏ loadRewardedAd() ב-SampleRewardedCustomEventLoader.

כדי לבקש מודעה מתגמלת, צריך ליצור או לשנות מחלקה שמרחיבה את Adapter כדי להטמיע את loadRewardedAd(). בנוסף, צריך ליצור כיתה חדשה כדי להטמיע את MediationRewardedAd.

בדוגמה לאירוע מותאם אישית, המחלקה SampleCustomEvent מרחיבה את המחלקה Adapter ואז מעבירה את ההרשאה אל SampleRewardedCustomEventLoader.

Java

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

import com.google.android.gms.ads.mediation.Adapter;
import com.google.android.gms.ads.mediation.MediationRewardedAdConfiguration;
import com.google.android.gms.ads.mediation.MediationAdConfiguration;
import com.google.android.gms.ads.mediation.MediationAdLoadCallback;
import com.google.android.gms.ads.mediation.MediationRewardedAd;
import com.google.android.gms.ads.mediation.MediationRewardedAdCallback;
...

public class SampleCustomEvent extends Adapter {

  private SampleNativeCustomEventLoader nativeLoader;

  @Override
  public void loadRewardedAd(
      @NonNull MediationRewardedAdConfiguration mediationRewardedAdConfiguration,
      @NonNull
          MediationAdLoadCallback<MediationRewardedAd, MediationRewardedAdCallback>
              mediationAdLoadCallback) {
    rewardedLoader =
        new SampleRewardedCustomEventLoader(
            mediationRewardedAdConfiguration, mediationAdLoadCallback);
    rewardedLoader.loadAd();
  }
}

SampleRewardedCustomEventLoader אחראי למשימות הבאות:

  • טעינת המודעה המתגמלת

  • הטמעת הממשק של MediationRewardedAd.

  • קבלת קריאות חוזרות (callback) של אירועים שקשורים למודעות ודיווח עליהן אל Google Mobile Ads SDK

הפרמטר האופציונלי שמוגדר בממשק המשתמש של AdMob נכלל בהגדרת המודעה. אפשר לגשת לפרמטר באמצעות 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.MediationRewardedAdConfiguration;
import com.google.android.gms.ads.mediation.MediationAdLoadCallback;
import com.google.android.gms.ads.mediation.MediationRewardedAd;
import com.google.android.gms.ads.mediation.MediationRewardedAdCallback;
...

public class SampleRewardedCustomEventLoader extends SampleRewardedAdListener
    implements MediationRewardedAd {

  /** Configuration for requesting the rewarded ad from the third-party network. */
  private final MediationRewardedAdConfiguration mediationRewardedAdConfiguration;

  /**
   * A {@link MediationAdLoadCallback} that handles any callback when a Sample
   * rewarded ad finishes loading.
   */
  private final MediationAdLoadCallback<MediationRewardedAd, MediationRewardedAdCallback>
      mediationAdLoadCallback;

  /** Callback for rewarded ad events. */
  private MediationRewardedAdCallback rewardedAdCallback;

  /** Constructor. */
  public SampleRewardedCustomEventLoader(
      @NonNull MediationRewardedAdConfiguration mediationRewardedAdConfiguration,
      @NonNull MediationAdLoadCallback<MediationRewardedAd, MediationRewardedAdCallback>
              mediationAdLoadCallback) {
    this.mediationRewardedAdConfiguration = mediationRewardedAdConfiguration;
    this.mediationAdLoadCallback = mediationAdLoadCallback;
  }

  /** Loads the rewarded 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 AdMob UI when defining the custom event.
    Log.i("RewardedCustomEvent", "Begin loading rewarded ad.");
    String serverParameter = mediationRewardedAdConfiguration
        .getServerParameters()
        .getString(MediationConfiguration
        .CUSTOM_EVENT_SERVER_PARAMETER_FIELD);
    Log.d("RewardedCustomEvent", "Received server parameter.");
    SampleAdRequest request = createSampleRequest(mediationRewardedAdConfiguration);
    sampleRewardedAd = new SampleRewardedAd(serverParameter);
    sampleRewardedAd.setListener(this);
    Log.i("RewardedCustomEvent", "Start fetching rewarded ad.");
    sampleRewardedAd.loadAd(request);
  }

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

בהתאם לכך שהמודעה מאוחזרת בהצלחה או שמתרחשת שגיאה, צריך להפעיל את onSuccess() או את onFailure(). הפונקציה onSuccess() מופעלת על ידי העברת מופע של המחלקה שמטמיעה את MediationRewardedAd.

בדרך כלל, השיטות האלה מיושמות בתוך קריאות חוזרות (callback) מ-SDK של צד שלישי שהמתאם שלכם מטמיע. בדוגמה הזו, ל-Sample SDK יש SampleAdListener עם קריאות חוזרות רלוונטיות:

Java

@Override
public void onRewardedAdLoaded() {
  rewardedAdCallback = mediationAdLoadCallback.onSuccess(this);
}

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

כדי להציג את המודעה, צריך להטמיע את השיטה showAd() ב-MediationRewardedAd:

Java

@Override
public void showAd(Context context) {
  if (!(context instanceof Activity)) {
    rewardedAdCallback.onAdFailedToShow(
        SampleCustomEventError.createCustomEventNoActivityContextError());
    return;
  }
  Activity activity = (Activity) context;

  if (!sampleRewardedAd.isAdAvailable()) {
    rewardedAdCallback.onAdFailedToShow(
        SampleCustomEventError.createCustomEventAdNotAvailableError());
    return;
  }
  sampleRewardedAd.showAd(activity);
}

העברת אירועי תהליך בחירת רשת מודעות אל Google Mobile Ads SDK

אחרי שמפעילים את onSuccess(), אפשר להשתמש באובייקט MediationRewardedAdCallback שמוחזר כדי שהמתאם יעביר אירועי הצגה מ-SDK של צד שלישי אל Google Mobile Ads SDK. המחלקות SampleRewardedCustomEventLoader מרחיבות את הממשק SampleAdListener כדי להעביר קריאות חוזרות מרשת המודעות לדוגמה אל Google Mobile Ads SDK.

חשוב שהאירוע המותאם אישית יעביר כמה שיותר מהקריאות החוזרות האלה, כדי שהאפליקציה תקבל את האירועים המקבילים האלה מ-Google Mobile Ads SDK. דוגמה לשימוש בפונקציות קריאה חוזרת:

Java

@Override
public void onAdRewarded(final String rewardType, final int amount) {
  RewardItem rewardItem =
      new RewardItem() {
        @Override
        public String getType() {
          return rewardType;
        }

        @Override
        public int getAmount() {
          return amount;
        }
      };
  rewardedAdCallback.onUserEarnedReward(rewardItem);
}

@Override
public void onAdClicked() {
  rewardedAdCallback.reportAdClicked();
}

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

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

@Override
public void onAdCompleted() {
  rewardedAdCallback.onVideoComplete();
}

כך מסיימים את ההטמעה של אירועים מותאמים אישית במודעות מתגמלות. הדוגמה המלאה זמינה ב-GitHub. אפשר להשתמש בו עם רשת מודעות שכבר נתמכת, או לשנות אותו כדי להציג מודעות מתגמלות עם אירועים בהתאמה אישית.