מודעות מעברונים

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

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

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

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

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

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

/6499/example/interstitial

מפעילים את 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. הצגה של מודעת המעברון
  3. איך להאזין לאירועים של מודעות מעברון
  4. ניקוי מודעת המעברון
  5. טעינה מראש של מודעת המעברון הבאה

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

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


  // This ad unit is configured to always serve test ads.
  private string _adUnitId = "/6499/example/interstitial";

  private InterstitialAd _interstitialAd;

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

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

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

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

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

              _interstitialAd = ad;
          });
  }

הצגה של מודעת המעברון

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

/// <summary>
/// Shows the interstitial ad.
/// </summary>
public void ShowInterstitialAd()
{
    if (_interstitialAd != null && _interstitialAd.CanShowAd())
    {
        Debug.Log("Showing interstitial ad.");
        _interstitialAd.Show();
    }
    else
    {
        Debug.LogError("Interstitial ad is not ready yet.");
    }
}

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

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

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

ניקוי מודעת המעברון

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

_interstitialAd.Destroy();

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

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

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

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

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

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

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

אירועים באפליקציה

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

ניתן להאזין לאירועים ספציפיים של Ad Manager באפליקציה באמצעות AppEvent. האירועים האלה יכולים להתרחש בכל שלב במחזור החיים של המודעה, אפילו לפני הקריאה.

namespace GoogleMobileAds.Api.AdManager;

/// The App event message sent from the ad.
public class AppEvent
{
    // Name of the app event.
    string Name;
    // Argument passed from the app event.
    string Value;
}

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

_interstitialAd.OnAppEventReceived += (AppEvent args) =>
{
    Debug.Log($"Received app event from the ad: {args.Name}, {args.Value}.");
};

הנה דוגמה שמראה איך לשנות את צבע הרקע של אפליקציה בהתאם לאירוע באפליקציה עם שם צבע:

_interstitialAd.OnAppEventReceived += (AppEvent args) =>
{
  if (args.Name == "color")
  {
    Color color;
    if (ColorUtility.TryParseColor(arg.Value, out color))
    {
      gameObject.GetComponent<Renderer>().material.color = color;
    }
  }
};

והנה גם הקריאייטיב התואם ששולח אירוע אפליקציה צבעוני:

<html>
<head>
  <script src="//www.gstatic.com/afma/api/v1/google_mobile_app_ads.js"></script>
  <script>
    document.addEventListener("DOMContentLoaded", function() {
      // Send a color=green event when ad loads.
      admob.events.dispatchAppEvent("color", "green");

      document.getElementById("ad").addEventListener("click", function() {
        // Send a color=blue event when ad is clicked.
        admob.events.dispatchAppEvent("color", "blue");
      });
    });
  </script>
  <style>
    #ad {
      width: 320px;
      height: 50px;
      top: 0px;
      left: 0px;
      font-size: 24pt;
      font-weight: bold;
      position: absolute;
      background: black;
      color: white;
      text-align: center;
    }
  </style>
</head>
<body>
  <div id="ad">Carpe diem!</div>
</body>
</html>

שיטות מומלצות

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

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