應用程式開啟頁面廣告是一種特殊廣告格式,適合希望藉由應用程式載入畫面賺取收益的發布商。應用程式開啟頁面廣告會在使用者將應用程式切換至前景時出現,而且使用者隨時可以關閉。
應用程式開啟頁面廣告會自動顯示一小塊畫面,向使用者顯示應用程式的品牌資訊。以下是應用程式開啟頁面廣告的範例:
必要條件
- 完成入門指南。
- Unity 外掛程式 7.1.0 以上版本。
一律使用測試廣告進行測試
以下程式碼範例包含可用於請求測試廣告的廣告單元 ID。其經過特別設定,為每個請求傳回測試廣告,而非製作實際廣告,因此可以安全使用。
不過,您已在Ad Manager 網頁介面中註冊應用程式,並建立自己的廣告單元 ID 以用於應用程式,請在開發期間明確將裝置設定為測試裝置。
/6499/example/app-open
導入作業
整合應用程式開啟頁面廣告的主要步驟如下:
- 建立公用程式類別
- 載入應用程式開啟頁面廣告
- 監聽應用程式開啟頁面廣告事件
- 考慮廣告效期
- 監聽應用程式狀態事件
- 顯示應用程式開啟頁面廣告
- 清除應用程式開啟頁面廣告
- 預先載入下一個應用程式開啟頁面廣告
建立公用程式類別
建立名為 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 = "/6499/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()
{
}
}
載入應用程式開啟頁面廣告
您可以在 AppOpenAd
類別上使用靜態 Load()
方法載入應用程式開啟頁面廣告。載入方法需要廣告單元 ID、AdManagerAdRequest
物件,以及廣告載入成功或失敗時呼叫的完成處理常式。系統會在完成處理常式中以參數形式提供載入的 AppOpenAd
物件。以下範例說明如何載入 AppOpenAd
。
// This ad unit is configured to always serve test ads.
private string _adUnitId = "/6499/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
狀態且 IsAdAvailable
為 true
時,我們會呼叫 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();
}
}
}
顯示應用程式開啟頁面廣告
如要顯示已載入的應用程式開啟頁面廣告,請在 AppOpenAd
執行個體上呼叫 Show()
方法。廣告每次載入只能顯示一次。使用 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
遭到刪除,事件會停止觸發。
其他資源
- HelloWorld 範例:幾乎所有廣告格式的導入方式。