מודעות מתגמלות

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

ניתן לקרוא כמה סיפורי הצלחה של לקוחות: מקרה לדוגמה 1, מקרה לדוגמה 2.

במדריך הזה מוסבר איך לשלב מודעות מתגמלות באפליקציית Unity.

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

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

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

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

Android

ca-app-pub-3940256099942544/5224354917

iOS

ca-app-pub-3940256099942544/1712485313

מפעילים את 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() במחלקה RewardedAd. האובייקט RewardedAd שנטען מסופק כפרמטר ב-handler להשלמה. הדוגמה הבאה ממחישה איך לטעון RewardedAd.


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

  private RewardedAd _rewardedAd;

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

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

      // create our request used to load the ad.
      var adRequest = new AdRequest();

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

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

              _rewardedAd = ad;
          });
  }

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

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

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

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

    // If the operation completed successfully, no error is returned.
    Debug.Log("Rewarded 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 ShowRewardedAd()
{
    const string rewardMsg =
        "Rewarded ad rewarded the user. Type: {0}, amount: {1}.";

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

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

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

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

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

כשמסיימים להשתמש ב-RewardedAd, חשוב להפעיל את השיטה Destroy() לפני שמשחררים את ההפניה אליה:

_rewardedAd.Destroy();

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

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

RewardedAd הוא אובייקט חד-פעמי. המשמעות היא שברגע שמודעה מתגמלת מוצגת, אי אפשר להשתמש באובייקט שוב. כדי לבקש עוד מודעה מתגמלת, צריך ליצור אובייקט RewardedAd חדש.

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

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

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

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

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