מודעות מעברון מתגמלות

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

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

ביצוע בדיקות באופן קבוע באמצעות מודעות בדיקה

הקוד לדוגמה שבהמשך מכיל מזהה של יחידת מודעות, שאפשר להשתמש בו כדי לבקש מודעות לבדיקה. היא נקבעה במיוחד כדי להחזיר מודעות בדיקה לכל בקשה, ולא מודעות בשלב ההפקה, ולכן היא בטוחה לשימוש.

עם זאת, אחרי שרשמתם אפליקציה בAdMob ממשק האינטרנט ויצרתם מזהים של יחידות מודעות משלכם לשימוש באפליקציה, צריך להגדיר את המכשיר כמכשיר בדיקה באופן מפורש במהלך הפיתוח.

Android

ca-app-pub-3940256099942544/5354046379

iOS

ca-app-pub-3940256099942544/6978759866

מפעילים את Mobile Ads SDK

לפני שטוענים מודעות, צריך להתקשר אל MobileAds.Initialize() כדי להפעיל את Mobile Ads SDK באפליקציה. יש לעשות זאת פעם אחת בלבד, רצוי בעת השקת האפליקציה.

using GoogleMobileAds;
using GoogleMobileAds.Api;

public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    public void Start()
    {
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize((InitializationStatus initStatus) =>
        {
            // This callback is called once the MobileAds SDK is initialized.
        });
    }
}

אם משתמשים בגישור, כדאי להמתין עד שהקריאה החוזרת תתרחש לפני שטוענים מודעות, כי כך אפשר לוודא שכל המתאמים לגישור יאופסו.

הטמעה

השלבים העיקריים לשילוב מודעות מעברון מתגמלות הם:

  1. טעינה של מודעת המעברון המתגמל
  2. [אופציונלי] אימות קריאות חוזרות (callback) לאימות בצד השרת (SSV)
  3. הצגה של מודעת מעברון מתגמלת עם קריאה חוזרת לגבי התגמול
  4. האזנה לאירועים של מודעות מעברון מתגמלות
  5. פינוי מקום במודעות המעברון המתגמלות
  6. טעינה מראש של מודעת המעברון המתגמלת הבאה

טעינה של מודעת המעברון המתגמל

הטעינה של מודעת מעברון מתגמלת מתבצעת באמצעות השיטה Load() הסטטית במחלקה RewardedInterstitialAd. שיטת הטעינה מחייבת מזהה יחידת מודעות, אובייקט AdRequest ו-handler להשלמה, שנקרא כאשר טעינת המודעה מצליחה או נכשלת. האובייקט RewardedInterstitialAd שנטען מסופק כפרמטר ב-handler להשלמה. הדוגמה הבאה מראה איך לטעון RewardedInterstitialAd.


  // These ad units are configured to always serve test ads.
#if UNITY_ANDROID
  private string _adUnitId = "ca-app-pub-3940256099942544/5354046379";
#elif UNITY_IPHONE
  private string _adUnitId = "ca-app-pub-3940256099942544/6978759866";
#else
  private string _adUnitId = "unused";
#endif

  private RewardedInterstitialAd _rewardedInterstitialAd;

  /// <summary>
  /// Loads the rewarded interstitial ad.
  /// </summary>
  public void LoadRewardedInterstitialAd()
  {
      // Clean up the old ad before loading a new one.
      if (_rewardedInterstitialAd != null)
      {
            _rewardedInterstitialAd.Destroy();
            _rewardedInterstitialAd = null;
      }

      Debug.Log("Loading the rewarded interstitial ad.");

      // create our request used to load the ad.
      var adRequest = new AdRequest();
      adRequest.Keywords.Add("unity-admob-sample");

      // send the request to load the ad.
      RewardedInterstitialAd.Load(_adUnitId, adRequest,
          (RewardedInterstitialAd ad, LoadAdError error) =>
          {
              // if error is not null, the load request failed.
              if (error != null || ad == null)
              {
                  Debug.LogError("rewarded interstitial ad failed to load an ad " +
                                 "with error : " + error);
                  return;
              }

              Debug.Log("Rewarded interstitial ad loaded with response : "
                        + ad.GetResponseInfo());

              _rewardedInterstitialAd = ad;
          });
  }

[אופציונלי] אימות קריאות חוזרות (callback) לאימות בצד השרת (SSV)

באפליקציות שנדרשת עבורן נתונים נוספים באימות בצד השרת, יש להשתמש בתכונת הנתונים המותאמת אישית במודעות מעברון מתגמלות. כל ערך מחרוזת שמוגדר באובייקט של מודעה מתגמלת מועבר לפרמטר של השאילתה custom_data בקריאה החוזרת (SSV). אם לא הוגדר ערך לנתונים מותאמים אישית, ערך הפרמטר של השאילתה custom_data לא ייכלל בקריאה החוזרת של ה-SSV.

דוגמת הקוד הבאה מדגימה איך להגדיר את אפשרויות ה-SSV אחרי הטעינה של מודעת המעברון המתגמלת.

// send the request to load the ad.
RewardedInterstitialAd.Load(_adUnitId,
                            adRequest,
                            (RewardedInterstitialAd ad, LoadAdError error) =>
    {
        // If the operation failed, an error is returned.
        if (error != null || ad == null)
        {
            Debug.LogError("Rewarded interstitial ad failed to load an ad " +
                           " with error : " + error);
            return;
        }

        // If the operation completed successfully, no error is returned.
        Debug.Log("Rewarded interstitial ad loaded with response : " +
                   ad.GetResponseInfo());
        
        // Create and pass the SSV options to the rewarded ad.
        var options = new ServerSideVerificationOptions
                              .Builder()
                              .SetCustomData("SAMPLE_CUSTOM_DATA_STRING")
                              .Build()
        ad.SetServerSideVerificationOptions(options);
        
});

אם ברצונך להגדיר מחרוזת תגמול מותאמת אישית, עליך לעשות זאת לפני הצגת המודעה.

הצגה של מודעת מעברון מתגמלת עם קריאה חוזרת לגבי התגמול

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

הקוד הבא מציג את השיטה הטובה ביותר להצגה של מודעת מעברון מתגמלת.

public void ShowRewardedInterstitialAd()
{
    const string rewardMsg =
        "Rewarded interstitial ad rewarded the user. Type: {0}, amount: {1}.";

    if (rewardedInterstitialAd != null && rewardedInterstitialAd.CanShowAd())
    {
        rewardedInterstitialAd.Show((Reward reward) =>
        {
            // TODO: Reward the user.
            Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
        });
    }
}

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

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

private void RegisterEventHandlers(RewardedInterstitialAd ad)
{
    // Raised when the ad is estimated to have earned money.
    ad.OnAdPaid += (AdValue adValue) =>
    {
        Debug.Log(String.Format("Rewarded interstitial ad paid {0} {1}.",
            adValue.Value,
            adValue.CurrencyCode));
    };
    // Raised when an impression is recorded for an ad.
    ad.OnAdImpressionRecorded += () =>
    {
        Debug.Log("Rewarded interstitial ad recorded an impression.");
    };
    // Raised when a click is recorded for an ad.
    ad.OnAdClicked += () =>
    {
        Debug.Log("Rewarded interstitial ad was clicked.");
    };
    // Raised when an ad opened full screen content.
    ad.OnAdFullScreenContentOpened += () =>
    {
        Debug.Log("Rewarded interstitial ad full screen content opened.");
    };
    // Raised when the ad closed full screen content.
    ad.OnAdFullScreenContentClosed += () =>
    {
        Debug.Log("Rewarded interstitial ad full screen content closed.");
    };
    // Raised when the ad failed to open full screen content.
    ad.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogError("Rewarded interstitial ad failed to open " +
                       "full screen content with error : " + error);
    };
}

פינוי מקום במודעות המעברון המתגמלות

כשמסיימים להשתמש ב-RewardedInterstitialAd, חשוב להקפיד לקרוא ל-method Destroy() לפני שמשחררים את ההפניה אליה:

_rewardedInterstitialAd.Destroy();

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

טעינה מראש של מודעת המעברון המתגמלת הבאה

RewardedInterstitialAd הוא אובייקט חד-פעמי. המשמעות היא שברגע שמודעת מעברון מתגמלת מוצגת, לא ניתן להשתמש באובייקט שוב. כדי לבקש מודעת מעברון מתגמלת נוספת, תצטרכו לטעון אובייקט RewardedInterstitialAd חדש.

כדי להכין מודעת מעברון מתגמלת להזדמנות בחשיפה הבאה, יש לטעון מראש את מודעת המעברון המתגמלת אחרי שמעלים את האירוע של המודעה OnAdFullScreenContentClosed או OnAdFullScreenContentFailed.

private void RegisterReloadHandler(RewardedInterstitialAd ad)
{
    // Raised when the ad closed full screen content.
    ad.OnAdFullScreenContentClosed += ()
    {
        Debug.Log("Rewarded interstitial ad full screen content closed.");

        // Reload the ad so that we can show another as soon as possible.
        LoadRewardedInterstitialAd();
    };
    // Raised when the ad failed to open full screen content.
    ad.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogError("Rewarded interstitial ad failed to open " +
                       "full screen content with error : " + error);

        // Reload the ad so that we can show another as soon as possible.
        LoadRewardedInterstitialAd();
    };
}

מקורות מידע נוספים