Logging Ad Response Info ID to Crashlytics

Firebase Crashlytics is a lightweight, realtime crash reporter that makes it easy for you to manage stability issues in your app. Crashlytics saves you troubleshooting time by intelligently grouping crashes and highlighting the circumstances that lead up to them.

This guide describes how to integrate Crashlytics into your Unity project so that you can log ad response IDs. Later, when you troubleshoot crashes in your app, you can look up the ad response IDs and use the Ad Review Center in AdMob to find and block the ads.

Step 1: Add Firebase to your Unity app

Follow the Firebase Unity integration guide to integrate Firebase Crashlytics into Unity.

Step 2: Log the ad response ID

  1. Create a MonoBehaviour script and initialize both AdMob and Firebase SDKs. Use the boolean isCrashlyticsInitialized to monitor when Crashlytics initializes.

    using GoogleMobileAds.Api;
    using Fabric.Crashlytics;
    ...
    public class GameObjectScript : MonoBehaviour
    {
        bool isCrashlyticsInitialized = false;
        public void Start()
        {
            ....
            // Initialize the Google Mobile Ads SDK.
            MobileAds.Initialize((InitializationStatus 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. Request a banner ad.

    using GoogleMobileAds.Api;
    using Fabric.Crashlytics;
    ...
    public class GameObjectScript : MonoBehaviour
    {
        public void Start()
        {
            ...
            // Initialize the Google Mobile Ads SDK.
            MobileAds.Initialize((InitializationStatus 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);
    
            // Called when an ad request has successfully loaded.
            this.bannerView.OnAdLoaded += this.HandleOnAdLoaded;
    
            AdRequest request = new AdRequest();
            this.bannerView.LoadAd(request);
        }
    }
    
  3. Get the ResponseInfo object OnAdLoaded and log the response ID to Crashlytics.

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

That's it! You can now see the most recent banner_ad_response_id in the key section of crash sessions on your Crashlytics dashboard. Note that some keys may take up to four hours to become visible on your dashboard.

response id