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

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

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

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

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

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

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

הפעלה של 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.
        });
    }
}

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

הטמעה

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

  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 = "/21775744923/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;
          });
  }

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

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

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