應用程式開啟頁面廣告

應用程式開啟頁面廣告是一種特殊的廣告格式,適合想透過發布商營利的發布商 他們的應用程式載入畫面應用程式開啟頁面廣告可以隨時關閉, 要在使用者將應用程式移至前景時顯示。

應用程式開啟頁面廣告會自動顯示一小塊品牌區域,讓使用者知道他們位於 以下是應用程式開啟頁面廣告的範例:

必要條件

  • 完成入門指南
  • Unity 外掛程式 7.1.0 以上版本。

一律使用測試廣告進行測試

以下程式碼範例包含廣告單元 ID,供您請求 測試廣告該功能已設定為傳回測試廣告,而不是 導入及導入廣告,以備不時之需。

不過,在 Ad Manager 網頁介面,並建立自己的廣告單元 用於應用程式的 ID,請明確將裝置設為測試裝置 裝置

/21775744923/example/app-open

導入作業

整合應用程式開啟頁面廣告的主要步驟如下:

  1. 建立公用程式類別
  2. 載入應用程式開啟頁面廣告
  3. 監聽應用程式開啟頁面廣告事件
  4. 考慮廣告效期
  5. 監聽應用程式狀態事件
  6. 顯示應用程式開啟頁面廣告
  7. 清除應用程式開啟頁面廣告
  8. 預先載入下一個應用程式開啟頁面廣告

建立公用程式類別

建立名為 AppOpenAdController 的新類別載入廣告。本課程 控制例項變數,以便追蹤已載入的廣告和廣告單元 ID 各個平台。

using System;
using UnityEngine;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;

/// <summary>
/// Demonstrates how to use the Google Mobile Ads app open ad format.
/// </summary>
[AddComponentMenu("GoogleMobileAds/Samples/AppOpenAdController")]
public class AppOpenAdController : MonoBehaviour
{

    // This ad unit is configured to always serve test ads.
    private string _adUnitId = "/21775744923/example/app-open";

    public bool IsAdAvailable
    {
        get
        {
            return _appOpenAd != null;
        }
    }

    public void Start()
    {
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize((InitializationStatus initStatus) =>
        {
            // This callback is called once the MobileAds SDK is initialized.
        });
    }

    /// <summary>
    /// Loads the app open ad.
    /// </summary>
    public void LoadAppOpenAd()
    {
    }

    /// <summary>
    /// Shows the app open ad.
    /// </summary>
    public void ShowAppOpenAd()
    {
    }
}

載入應用程式開啟頁面廣告

系統會使用靜態 Load() 方法,在 AppOpenAd 類別。載入方法需要廣告單元 ID、 AdManagerAdRequest 物件和完成處理常式 廣告載入成功或失敗時呼叫。載入的 AppOpenAd 物件 以參數形式提供給完成處理常式。以下範例說明 載入 AppOpenAd


  // This ad unit is configured to always serve test ads.
  private string _adUnitId = "/21775744923/example/app-open";

  private AppOpenAd appOpenAd;

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

      Debug.Log("Loading the app open ad.");

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

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

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

              appOpenAd = ad;
              RegisterEventHandlers(ad);
          });
  }

監聽應用程式開啟頁面廣告事件

如要進一步自訂廣告行為,您可以連結至 事件中的事件 (例如開啟和關閉等等)。監聽 來註冊這些事件,如下所示。

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

考慮廣告效期

為確保您不顯示過期廣告,請在 AppOpenAdController 可檢查廣告載入至今已經過多久。 接著,用這個方法檢查廣告是否仍然有效。

應用程式開啟頁面廣告會在 4 小時內逾時。在 _expireTime 中快取載入時間 變數。

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

        // If the operation completed successfully, no error is returned.
        Debug.Log("App open ad loaded with response : " + ad.GetResponseInfo());

        // App open ads can be preloaded for up to 4 hours.
        _expireTime = DateTime.Now + TimeSpan.FromHours(4);

        _appOpenAd = ad;
    });

更新 IsAdAvailable 屬性以檢查 _expireTime,確認已載入 廣告仍然有效。

public bool IsAdAvailable
{
    get
    {
        return _appOpenAd != null
               && _appOpenAd.IsLoaded()
               && DateTime.Now < _expireTime;
    }
}

監聽應用程式狀態事件

使用 AppStateEventNotifier 監聽應用程式前景,並 背景事件。此類別會在每次發生時引發 AppStateChanged 事件 應用程式前景或背景。

private void Awake()
{
    // Use the AppStateEventNotifier to listen to application open/close events.
    // This is used to launch the loaded ad when we open the app.
    AppStateEventNotifier.AppStateChanged += OnAppStateChanged;
}

private void OnDestroy()
{
    // Always unlisten to events when complete.
    AppStateEventNotifier.AppStateChanged -= OnAppStateChanged;
}

當我們處理 AppState.Foreground 狀態和 IsAdAvailabletrue,我們呼叫 ShowAppOpenAd() 以顯示廣告。

private void OnAppStateChanged(AppState state)
{
    Debug.Log("App State changed to : "+ state);

    // if the app is Foregrounded and the ad is available, show it.
    if (state == AppState.Foreground)
    {
        if (IsAdAvailable)
        {
            ShowAppOpenAd();
        }
    }
}

顯示應用程式開啟頁面廣告

如要顯示已載入的應用程式開啟頁面廣告,請在以下項目中呼叫 Show() 方法: AppOpenAd 執行個體。廣告每次載入時只能顯示一次。使用「CanShowAd()」 方法,確認廣告已經準備好可以放送。

/// <summary>
/// Shows the app open ad.
/// </summary>
public void ShowAppOpenAd()
{
    if (appOpenAd != null && appOpenAd.CanShowAd())
    {
        Debug.Log("Showing app open ad.");
        appOpenAd.Show();
    }
    else
    {
        Debug.LogError("App open ad is not ready yet.");
    }
}

清除應用程式開啟頁面廣告

完成 AppOpenAd 後,請務必呼叫 Destroy() 方法,再放置您的參照:

appOpenAd.Destroy();

這會通知外掛程式,說明該物件已不再使用,以及該物件的記憶體 可收回這些物件。無法呼叫此方法會導致記憶體流失。

預先載入下一個應用程式開啟頁面廣告

AppOpenAd 是一次性的物件。也就是說,放送應用程式開啟頁面廣告後 以再次使用物件如想請求另一個應用程式開啟頁面廣告, 您必須建立新的 AppOpenAd 物件。

如要為下一個曝光商機做好準備,請預先載入 應用程式開啟頁面廣告 (OnAdFullScreenContentClosed) 或 會引發 OnAdFullScreenContentFailed 個廣告事件。

private void RegisterReloadHandler(AppOpenAd ad)
{
    ...
    // Raised when the ad closed full screen content.
    ad.OnAdFullScreenContentClosed += ()
    {
        Debug.Log("App open ad full screen content closed.");

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

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

冷啟動和載入畫面

目前為止,本說明文件假設您只有在使用者 前景。「冷啟動」發生在 應用程式已啟動,但之前未在記憶體中暫停。

舉例來說,使用者首次開啟應用程式時,就是冷啟動的例子。 如果選擇冷啟動,您就不會看到先前載入的應用程式開啟頁面廣告 就會立即顯示從送出廣告請求到收到廣告之間的延遲時間 讓使用者能先短暫使用您的應用程式 很容易讓人感到驚訝應避免這麼做,因為 使用者體驗不佳

如要在冷啟動時放送應用程式開啟頁面廣告,建議做法是使用載入畫面。 載入您的遊戲或應用程式素材資源,而且只在載入過程中顯示廣告 。如果您的應用程式已載入完成,並已將使用者導向主要應用程式 請不要顯示廣告

最佳做法

應用程式開啟頁面廣告可讓您在應用程式首次載入時,透過應用程式的載入畫面營利 因此,請務必遵循以下最佳做法 確保使用者能盡情使用您的應用程式

  • 等使用者用過應用程式幾次之後,再放送第一則應用程式開啟頁面廣告。
  • 把握使用者等待的時段,向他們放送應用程式開啟頁面廣告 載入您的應用程式。
  • 如果應用程式開啟頁面廣告和載入畫面顯示載入畫面 使用者於廣告關閉前完成載入,關閉載入畫面 OnAdDidDismissFullScreenContent 事件處理常式。
  • 在 iOS 平台上,AppStateEventNotifier 會將 AppStateEventClient GameObject。必須為活動提供這個GameObject 所以不要摧毀它如果 GameObject 為 已刪除。

其他資源