โฆษณาคั่นระหว่างหน้า

โฆษณาคั่นระหว่างหน้าเป็นโฆษณาแบบเต็มหน้าจอที่บดบังอินเทอร์เฟซของแอปโฮสต์ โฆษณามักแสดงในจุดเปลี่ยนหน้าปกติในขณะที่แอปดำเนินอยู่ เช่น ในช่วงหยุดชั่วคราวระหว่างเปลี่ยนด่านในเกม เมื่อแอปแสดงโฆษณาคั่นระหว่างหน้า ผู้ใช้สามารถเลือกที่จะแตะโฆษณาแล้วไปยังปลายทาง หรือปิดแล้วกลับไปที่แอปก็ได้

คู่มือนี้จะอธิบายวิธีผสานรวมโฆษณาคั่นระหว่างหน้าเข้ากับแอป Unity

ข้อกำหนดเบื้องต้น

ทดสอบด้วยโฆษณาทดสอบเสมอ

โค้ดตัวอย่างต่อไปนี้มีรหัสหน่วยโฆษณาที่คุณใช้เพื่อขอโฆษณาทดสอบได้ เครื่องมือนี้ได้รับการกำหนดค่ามาเป็นพิเศษให้ส่งคืนโฆษณาทดสอบแทนที่จะเป็นโฆษณาจริงสำหรับทุกคำขอ ซึ่งทำให้ใช้งานได้อย่างปลอดภัย

แต่หลังจากที่คุณลงทะเบียนแอปใน Ad Manager อินเทอร์เฟซเว็บและสร้างรหัสผ่านหน่วยโฆษณาของคุณเองสำหรับใช้ในแอปแล้ว ให้กำหนดค่าอุปกรณ์ของคุณเป็นอุปกรณ์ทดสอบอย่างชัดเจนระหว่างการพัฒนา

/6499/example/interstitial

เริ่มต้น SDK โฆษณาในอุปกรณ์เคลื่อนที่

ก่อนที่จะโหลดโฆษณา ให้แอปเริ่มต้น 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.
        });
    }
}

หากคุณใช้สื่อกลาง ให้รอจนกว่าการเรียกกลับจะเกิดขึ้นก่อนที่จะโหลดโฆษณา เนื่องจากวิธีนี้จะช่วยให้อะแดปเตอร์สื่อกลางทั้งหมดเริ่มทำงานแล้ว

การใช้งาน

ขั้นตอนหลักในการผสานรวมโฆษณาคั่นระหว่างหน้ามีดังนี้

  1. โหลดโฆษณาคั่นระหว่างหน้า
  2. แสดงโฆษณาคั่นระหว่างหน้า
  3. ฟังเหตุการณ์โฆษณาคั่นระหว่างหน้า
  4. ล้างโฆษณาคั่นระหว่างหน้า
  5. โหลดโฆษณาคั่นระหว่างหน้าถัดไปล่วงหน้า

โหลดโฆษณาคั่นระหว่างหน้า

การโหลดโฆษณาคั่นระหว่างหน้าทำได้โดยใช้เมธอด Load() แบบคงที่ในคลาส InterstitialAd วิธีการโหลดต้องใช้รหัสหน่วยโฆษณา ออบเจ็กต์ AdManagerAdRequest และตัวแฮนเดิลการทำงานที่สมบูรณ์ซึ่งจะเรียกใช้เมื่อการโหลดโฆษณาสำเร็จหรือล้มเหลว ออบเจ็กต์ AdManagerInterstitialAd ที่โหลดจะระบุเป็นพารามิเตอร์ในเครื่องจัดการความสมบูรณ์ ตัวอย่างด้านล่างแสดงวิธีโหลด 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;
          });
  }

แสดงโฆษณาคั่นระหว่างหน้า

หากต้องการแสดงโฆษณาคั่นระหว่างหน้าที่โหลดไว้ ให้เรียกใช้เมธอด Show() ในอินสแตนซ์ AdManagerInterstitialAd โฆษณาอาจแสดงได้ 1 ครั้งต่อการโหลด ใช้เมธอด 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>

แนวทางปฏิบัติแนะนำ

พิจารณาว่าโฆษณาคั่นระหว่างหน้าเป็นประเภทโฆษณาที่เหมาะกับแอปของคุณหรือไม่
โฆษณาคั่นระหว่างหน้าทำงานได้ดีที่สุดในแอปที่มีจุดเปลี่ยนที่เป็นธรรมชาติ บทสรุปของงานภายในแอป เช่น การแชร์รูปภาพหรือการเล่นเกมผ่านด่านหนึ่งๆ ทำให้เกิดจุดดังกล่าว อย่าลืมคำนึงถึงจุดใดในการทำงานของแอป ในการแสดงโฆษณาคั่นระหว่างหน้า Display ที่ดีที่สุด และวิธีที่ผู้ใช้มีแนวโน้มที่จะตอบสนอง
หยุดการดำเนินการชั่วคราวเมื่อแสดงโฆษณาคั่นระหว่างหน้า
โฆษณาคั่นระหว่างหน้ามีหลายประเภท เช่น ข้อความ รูปภาพ หรือวิดีโอ คุณควรตรวจสอบว่าเมื่อแอปแสดงโฆษณาคั่นระหว่างหน้า แอปจะระงับการใช้ทรัพยากรบางอย่างเพื่อให้โฆษณาได้ประโยชน์ด้วย ตัวอย่างเช่น เมื่อคุณเรียกให้แสดงโฆษณาคั่นระหว่างหน้า อย่าลืมหยุดเอาต์พุตเสียงที่แอปสร้างขึ้นชั่วคราว คุณสามารถกลับมาเล่นเสียงต่อได้ในเหตุการณ์ OnAdFullScreenContentClosed() ซึ่งจะเรียกใช้ได้เมื่อผู้ใช้โต้ตอบกับโฆษณาเสร็จแล้ว นอกจากนี้ ให้พิจารณาหยุดงานการประมวลผลที่เข้มข้นไว้ชั่วคราว เช่น การทำ Game Loop ชั่วคราวในขณะที่แสดงโฆษณาอยู่ วิธีนี้ช่วยให้มั่นใจได้ว่าผู้ใช้จะไม่พบปัญหากราฟิกที่ช้าหรือไม่ตอบสนองหรือวิดีโอกระตุก
อย่าแสดงโฆษณาต่อผู้ใช้มากเกินไป
แม้ว่าการเพิ่มความถี่ของโฆษณาคั่นระหว่างหน้าในแอปอาจดู เป็นวิธีเพิ่มรายได้ที่ดี แต่ก็อาจกระทบต่อประสบการณ์ของผู้ใช้และอัตราการคลิกผ่านที่ต่ำลง ตรวจสอบว่าผู้ใช้ไม่ได้ถูกขัดจังหวะบ่อยๆ จนไม่สามารถเพลิดเพลินกับการใช้งานแอปได้อีกต่อไป

แหล่งข้อมูลเพิ่มเติม