Initialize the MoPub SDK for Unity

The MoPub 5.0 SDK introduces a new API for initialization called MoPub.InitializeSdk(). This API is required for a number of new functionalities, including:

v5.0.0 to v5.4.1:

  • GDPR consent mechanisms
  • Rewarded video pre-initialization

v5.5.0 and above:

  • GDPR consent mechanisms
  • Mediation SDKs initialization
  • Logging
  • Advanced Bidding
  • Legitimate Interest

Instructions

You only need to initialize once using any valid ad unit ID from your app. Ad requests should only be made once the SDK initialization has completed. You can simply register for the OnSdkInitializedEvent callback event and create your own handler as shown in the example below.

  1. In your app’s Start(), instantiate a MoPub.SdkConfiguration object with any valid ad unit ID from your app, your mediation settings (optional), (optional) log level, (optional) map of configurations for networks, and (optional) a list of adapter configuration class names to initialize.
  2. Call MoPub.InitializeSdk() and pass in the MoPub.SdkConfiguration object.

Network SDK initialization

SDK initialization will automatically attempt to initialize all known networks. However, publishers will be given the option to specify additional networks via the SDK configuration object as shown in the example below. Note: you can use NetworkConfiguration to pass network configurations required for pre-initialization. Otherwise, your first attempt to pre-initialize mediated networks will be a no-op because no network data is cached at this point. All subsequent attempts to pre-initialize should succeed for the duration of the app’s lifecycle.

Logging Information

The MoPub 5.5.0 SDK enhances the logging experience to include more detailed information such as adapter version, SDK version and ad life cycle events and operations. You can set Debug, Info or None using MoPubBase.LogLevel during initialization.

Legitimate Interest

The MoPub SDK 5.5.0 now enables mediated networks to collect your user’s personal data based on legitimate interest basis when publishers opt-in. For more details read here. You can enable the legitimate interest flag using the API AllowLegitimateInterest during initialization.

Initializing custom ad networks not listed on Supported Mediation Partners

In order to initialize the custom network SDKs, follow the steps below:

  • Follow this guide to write adapter configuration class for your network for Android.
  • Follow this guide to write adapter configuration class for your network for iOS.
  • Set the network configuration options for a given mediated network class name using MoPub.MediatedNetwork. Refer to the example below.

Example

private void Start()
    {
        // NOTE: the MoPub SDK needs to be initialized on Start() to ensure all other objects have been enabled first.
        var anyAdUnitId = _bannerAdUnits[0];
        MoPub.InitializeSdk(new MoPub.SdkConfiguration {
            AdUnitId = anyAdUnitId,

            // Set desired log level here to override default level of MPLogLevelNone
            LogLevel = MoPubBase.LogLevel.MPLogLevelDebug,

            // Uncomment the following line to allow supported SDK networks to collect user information on the basis
            // of legitimate interest.
            //AllowLegitimateInterest = true,

            // Specify the mediated networks you are using here:
            MediatedNetworks = new MoPub.MediatedNetwork[]
            {
                // Example using AdMob.  Follow this template for other supported networks as well.
                // Note that keys must be strings, and values must be JSON-serializable (strings only, for MoPubRequestOptions).
                new MoPub.SupportedNetwork.AdMob
                {
                    // Network adapter configuration settings (initialization).
                    NetworkConfiguration = new Dictionary<string,object> {
                        { "key1", value },
                        { "key2", value },
                    },

                    // Global mediation settings (per ad request).
                    MediationSettings = new Dictionary<string,object> {
                        { "key1", value },
                        { "key2", value },
                    },

                    // Additional options to pass to the MoPub servers (per ad request).
                    MoPubRequestOptions = new Dictionary<string,object> {
                        { "key1", "value" },
                        { "key2", "value" },
                    }
                },

                // Example using a custom network adapter:
                new MoPub.MediatedNetwork
                {
                    // Specify the class name that implements the AdapterConfiguration interface.
                #if UNITY_ANDROID
                    AdapterConfigurationClassName = "com.mopub.mobileads.YourNetworkAdapterConfiguration",  // include the full package name.
                #else // UNITY_IOS
                    AdapterConfigurationClassName = "MyNetworkAdapterConfiguration",
                #endif

                    // Specify the class name that implements the MediationSettings interface.
                    // Note: Custom network mediation settings are currently not supported on Android.
                #if UNITY_IOS
                    MediationSettingsClassName = "classname",
                #endif

                    // Fill in settings and configuration options the same way as for supported networks:

                    NetworkConfiguration = new Dictionary<string,object> {
                        { "key1", value },
                        { "key2", value },
                    },

                #if UNITY_IOS  // See note above.
                    MediationSettings    = new Dictionary<string,object> {
                        { "key1", value },
                        { "key2", value },
                    },
                #endif

                    MoPubRequestOptions  = new Dictionary<string,object> {
                        { "key1", value },
                        { "key2", value },
                    },
                }
            },
        });

        // register for initialized callback event in the app
        MoPubManager.OnSdkInitializedEvent += OnSdkInitializedEvent;

        // create your handler
        private void OnSdkInitializedEvent(string adUnitId)
    	{
        	// The SDK is initialized here. Ready to make ad requests.
    	}

Last updated May 30, 2019

TWITTER, MOPUB, and the Bird logo are trademarks of Twitter, Inc. or its affiliates. All third party logos and trademarks included are the property of their respective owners.

© 2019 MoPub (a division of Twitter, Inc.)