מודעת מעברון מתגמלת (בטא)

בחירת פלטפורמה: Android iOS Unity Flutter

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

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

תמיד כדאי לבצע בדיקות באמצעות מודעות בדיקה

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

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

Android

ca-app-pub-3940256099942544/5354046379

iOS

ca-app-pub-3940256099942544/6978759866

הפעלה של Mobile Ads SDK

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

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.
        });
    }
}

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

הטמעה

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

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

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

טעינה של מודעת מעברון מתגמלת מתבצעת באמצעות השיטה הסטטית Load() במחלקה RewardedInterstitialAd. השיטה load דורשת מזהה יחידת מודעות, אובייקט AdRequest ומטפל השלמה שמופעל כשטעינת המודעה מצליחה או נכשלת. אובייקט 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)

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

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

בדוגמה הבאה מוצגת השיטה הכי טובה להצגת מודעה מתגמלת מסוג interstitial.

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, חשוב לקרוא לשיטה Destroy() לפני שמבטלים את ההפניה אליה:

_rewardedInterstitialAd.Destroy();

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

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

RewardedInterstitialAd הוא אובייקט לשימוש חד-פעמי. המשמעות היא שאחרי שמציגים מודעת מעברון עם תגמול, אי אפשר להשתמש שוב באובייקט. כדי לבקש מודעה מתגמלת מסוג interstitial אחרת, צריך לטעון אובייקט 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();
    };
}

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