將廣告回應資訊 ID 記錄至 Crashlytics

Firebase Crashlytics 是一款輕量級的即時當機回報工具,可讓您輕鬆管理應用程式中的穩定性問題。Crashlytics 會自動將異常終止事件分組,並突顯導致異常終止的情況,協助您節省排解問題的時間。

本指南將說明如何將 Crashlytics 整合至 Unity 專案,以便記錄廣告回應 ID。日後,當您要排解應用程式當機問題時,可以查詢廣告回應 ID,並使用 AdMob 中的廣告審核中心找出並封鎖廣告。

步驟 1:將 Firebase 新增至 Unity 應用程式

請按照 Firebase Unity 整合指南將 Firebase Crashlytics 整合至 Unity。

步驟 2:記錄廣告回應 ID

  1. 建立 MonoBehaviour 指令碼,並初始化 AdMob 和 Firebase SDK。使用布林值 isCrashlyticsInitialized 監控 Crashlytics 初始化的時間。

    using GoogleMobileAds.Api;
    using Fabric.Crashlytics;
    ...
    public class GameObjectScript : MonoBehaviour
    {
        bool isCrashlyticsInitialized = false;
        public void Start()
        {
            ....
            // Initialize the Google Mobile Ads SDK.
            MobileAds.Initialize(initStatus => { });
            ....
            // Initialize Firebase
            Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
                Firebase.DependencyStatus dependencyStatus = task.Result;
                if (dependencyStatus == Firebase.DependencyStatus.Available)
                {
                    Firebase.FirebaseApp app = Firebase.FirebaseApp.DefaultInstance;
                    isCrashlyticsInitialized = true;
                }
                else
                {
                    UnityEngine.Debug.LogError(System.String.Format(
                      "Could not resolve all Firebase dependencies: {0}",dependencyStatus));
                    // Firebase Unity SDK is not safe to use here.
                }
            }
        }
    }
    
  2. 請求橫幅廣告。

    using GoogleMobileAds.Api;
    using Fabric.Crashlytics;
    ...
    public class GameObjectScript : MonoBehaviour
    {
    
        public void Start()
        {
            ...
            // Initialize the Google Mobile Ads SDK.
            MobileAds.Initialize(initStatus => { });
    
            // Initialize Firebase.
            Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
                Firebase.DependencyStatus dependencyStatus = task.Result;
                if (dependencyStatus == Firebase.DependencyStatus.Available)
                {
                    // Create and hold a reference to your FirebaseApp,
                    // where app is a Firebase.FirebaseApp property of your
                    // application class.
                    // Crashlytics will use the DefaultInstance, as well;
                    // this ensures that Crashlytics is initialized.
                    Firebase.FirebaseApp app = Firebase.FirebaseApp.DefaultInstance;
                    isCrashlyticsInitialized = true;
                }
                else
                {
                    UnityEngine.Debug.LogError(System.String.Format(
                        "Could not resolve all Firebase dependencies: {0}",dependencyStatus));
                    // Firebase Unity SDK is not safe to use here.
                }
            });
    
            // Request Banner View.
            this.RequestBanner();
            ...
        }
    
        public void RequestBanner()
        {
            #if UNITY_ANDROID
                string adUnitId = "ca-app-pub-3940256099942544/6300978111";
            #elif UNITY_IPHONE
                string adUnitId = "ca-app-pub-1220882738324941/1255739139";
            #else
                string adUnitId = "unexpected_platform";
            #endif
            // Create a 320x50 banner at the top of the screen.
            this.bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
            AdRequest request = new AdRequest();
            this.bannerView.LoadAd(request);
            // Called when an ad request has successfully loaded.
            this.bannerView.OnAdLoaded += this.HandleOnAdLoaded;
        }
    }
    
  3. 取得 ResponseInfo 物件 OnAdLoaded,並將回應 ID 記錄到 Crashlytics。

    public void HandleOnAdLoaded(object sender, EventArgs args)
    {
        ResponseInfo responseInfo = this.bannerView.GetResponseInfo();
        if (responseInfo != null)
        {
            String adResponseId = responseInfo.GetResponseId();
            // Log to Crashlytics.
            if (isCrashlyticsInitialized)
            {
                Crashlytics.SetCustomKey("banner_ad_response_id", adResponseId);
            }
        }
    }
    

大功告成!您現在可以在 Crashlytics 資訊主頁的當機工作階段「重要」部分中,查看最近的 banner_ad_response_id。請注意,部分鍵可能需要四小時才會顯示在資訊主頁上。

回應 ID