NextGen SDK | Android | MoPub Integration
Integration Guide for Publishers with MoPub as a Primary Ad Server
(Android NextGen SDK version 21.5.7)
MoPub Dashboard setup
1. Go to the MoPub Dashboard –https://app.mopub.com/networks
2. Click on “New network” button
3. Choose “Custom SDK network” from the bottom of the list
4. Set the desired parameters in the next two tabs.
5. On “App & ad unit setup” tab, set the following:
-
- Custom event classes:
- For Banner:
com.mopub.mobileads.SMAMoPubSmaatoBannerAdapter
- For Interstitial:
com.mopub.mobileads.SMAMoPubSmaatoInterstitialAdapter
- For Rewarded Video:
com.mopub.mobileads.SMAMoPubSmaatoRewardedVideoAdapter
- For Native:
com.mopub.nativeads.SmaatoMoPubNativeAdapter
- For Banner:
- Custom event classes:
-
- Custom event class data:
{"adspaceId":"<YOUR_SPX_ADSPACE_ID>"}
- Custom event class data:
Integrate the NextGen SDK
Set up your account on SPX
Configure your Android project
Add the following repository setup to your project’s main build.gradle file:
allprojects {
repositories {
google()
jcenter()
maven {
url "https://s3.amazonaws.com/smaato-sdk-releases/"
}
}
}
Set the compile options to Java 8, (i.e. place the following line into your application module build.gradle file):
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
Add dependencies to your application module build.gradle file.
For Banner ads, then make sure the following dependencies are in place:
implementation 'com.smaato.android.sdk:mopub-banner-adapter:21.5.7'
For Interstitial ads, then make sure the following dependencies are in place:
implementation 'com.smaato.android.sdk:mopub-interstitial-adapter:21.5.7'
For Rewarded ads, then make sure the following dependencies are in place:
implementation 'com.smaato.android.sdk:mopub-rewarded-video-adapter:21.5.7'
For Native ads, make sure the following dependencies are in place:
implmentation 'com.smaato.android.sdk:mopub-native-adapter:21.5.7'
Important: If your application uses MoPub 5.13.1 and above, make sure your application is running on Android 19 and above.
For Banner ads, then make sure the following dependencies are in place:
implementation 'com.smaato.android.sdk:mopub-banner-adapter-5-13:21.5.7'
For Interstitial ads, then make sure the following dependencies are in place:
implementation 'com.smaato.android.sdk:mopub-interstitial-adapter-5-13:21.5.7'
For Rewarded ads, then make sure the following dependencies are in place:
implementation 'com.smaato.android.sdk:mopub-rewarded-video-adapter-5-13:21.5.7'
For Native ads, make sure the following dependencies are in place:
implmentation 'com.smaato.android.sdk:mopub-native-adapter:21.5.7'
Add the following permissions to your application AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
If your application targets Android 5.0 (API level 21) or higher, then you need to add the following line to your application AndroidManifest.xml file:
<uses-feature android:name="android.hardware.location.network" />
If your application targets Android 9 Pie (API level 28) or higher, in order to send HTTP requests (so that more Ads can be shown), then you need to configure the networkSecurityConfig attribute in the application tag in AndroidManifest.xml:
android:networkSecurityConfig="@xml/network_security_config"
Now create the network_security_config.xml in the XML resource directory with:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
For more details regarding the Network security configuration, please see the official Google documentation: https://developer.android.com/training/articles/security-config
Proguard Configuration
If you’re using Proguard in your project, please add the following lines to your Proguard config file, as per your requirements:
-keep public class com.smaato.sdk.** { *; }
-keep public interface com.smaato.sdk.** { *; }
Initialize the NextGen SDK
Add the following to your YourApplication.OnCreate() method, to initialize the Smaato SDK. Additionally, it is recommended to configure the SDK with user information. This is an optional step and can be implemented at a later time.
import android.app.Application;
import com.smaato.sdk.core.AdContentRating;
import com.smaato.sdk.core.Config;
import com.smaato.sdk.core.Gender;
import com.smaato.sdk.core.SmaatoSdk;
import com.smaato.sdk.core.log.LogLevel;
public class YourApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// // initialize SDK first!
Config config = Config.builder()
// log errors only
.setLogLevel(LogLevel.ERROR)
// allow HTTPS traffic only
.setHttpsOnly(true)
.build();
// initialize the Smaato SDK
SmaatoSdk.init(this, config, "SMAATO_PUBLISHER_ID");
// You can also initialize the Smaato SDK without configuration:
// SmaatoSdk.init(this, "SMAATO_PUBLISHER_ID");
// optional configuration
SmaatoSdk.setSearchQuery("bitcoin, lamborghini, san-francisco");
SmaatoSdk.setGender(Gender.MALE); // usually set after user logs in
SmaatoSdk.setAge(40); // usually set after user logs in
// allow the Smaato SDK to automatically get the user's location
SmaatoSdk.setGPSEnabled(true);
}
}
Important Note: Starting from NextGen SDK version 21.2.1, the new GPSEnabled flag allows the SDK to automatically get the user’s location when permission is given by the user. By default, this flag is set to off and the user location will not be provided in the ad request. To enable this the GPSEnabled flag must be set to yes.
SDK Initialization Using MoPub SDK Mediation Configuration Class
Map smaatoAdapterConfiguration = new HashMap<>();
smaatoAdapterConfiguration.put(SmaatoAdapterConfiguration.KEY_ENABLE_LOGGING, String.valueOf(true));
smaatoAdapterConfiguration.put(SmaatoAdapterConfiguration.KEY_HTTPS_ONLY, String.valueOf(true));
smaatoAdapterConfiguration.put(SmaatoAdapterConfiguration.KEY_LOG_LEVEL, String.valueOf(LogLevel.DEBUG));
smaatoAdapterConfiguration.put(SmaatoAdapterConfiguration.KEY_MAX_AD_CONTENT_RATING, String.valueOf(AdContentRating.MAX_AD_CONTENT_RATING_UNDEFINED));
smaatoAdapterConfiguration.put(SmaatoAdapterConfiguration.KEY_PUBLISHER_ID, "SMAATO_PUBLISHER_ID");
SdkConfiguration sdkConfiguration = new SdkConfiguration.Builder("MOPUB_AD_UNIT_ID")
.withLogLevel(MoPubLog.LogLevel.DEBUG)
.withAdditionalNetwork(SmaatoAdapterConfiguration.class.getName())
.withMediatedNetworkConfiguration(SmaatoAdapterConfiguration.class.getName(), smaatoAdapterConfiguration)
.build();
MoPub.initializeSdk(this, sdkConfiguration, () -> {
SmaatoSdk.setGPSEnabled(true);
SmaatoSdk.setGender(Gender.FEMALE);
SmaatoSdk.setAge(30);
SmaatoSdk.setKeywords("yoga, vegan, san+francisco");
});
- Here, SmaatoAdapterConfiguration.KEY_PUBLISHER_ID is a mandatory param while others are optional.
- For more information, visit https://developers.mopub.com/publishers/android/initialize/#initialize-custom-ad-networks
Important Details About GDPR
The General Data Protection Regulation (GDPR) was created to provide European users with greater transparency and control over their personal information. As a publisher, you should integrate a Consent Management Platform (CMP) and request for user, vendor, and purpose consents as outlined in IAB Europe’s IAB Tech Lab – CMP API v2. You can find an example implementation of a web-based CMP and the corresponding native wrappers here in the IAB’s GDPR-Transparency-and-Consent-Framework.
You can also make your own custom CMP. The collected end-user consent information needs to be stored in SharedPreferences using the following keys:
Transparency and Consent Framework v2:
Key | Type | Description |
---|---|---|
IABTCF_gdprApplies | Number |
1 = Subject to GDPR |
IABTCF_TCString | String |
Base64-encoded consent string as defined in IAB Tech Lab – Consent string and vendor list formats v2 |
Sample of GDPR Saving in SharedPreferences
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
...
// User is not subject to GDPR
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context_here);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("IABTCF_gdprApplies", "0");
editor.commit();
// Use this example above to set values for the rest of the GDPR keys
Important Details About CCPA
The California Consumer Privacy Act (CCPA) was created to provide California consumers with greater transparency and control over their personal information.
For more information about the CCPA regulation, please check out the Smaato FAQ. You can also review the IAB’s U.S. Privacy String documentation.
For Publishers with California-Based Users
As a publisher, you need to make sure to request consent from California-based users (to give or refuse consent / to opt-out or opt-in ) about private data transfer. This answer should be saved in SharedPreferences with key “IABUSPrivacy_String” in the US Privacy String format (CCPA Opt-Out Storage Format).
The Smaato NextGen SDK reads this value in the key “IABUSPrivacy_String” if it exists and uses this as an optional parameter for all ad requests.
Sample of CCPA Saving in SharedPreferences
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
...
// save the user's consent
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context_here);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("IABUSPrivacy_String", "1YNN"); // for example "1YNN"
editor.commit();
Further Examples of the U.S. Privacy String 1YNN: User has not made a choice to opt-out 1NYY: User has made a choice to opt-out 1—: A Digital Property has determined to use a U.S. Privacy string version 1 and that CCPA does not apply to the transaction.
Ads for Testing Purposes
Adspace ID | Type | Description |
---|---|---|
130626424 | Rich Media | Banner / Med-rect / Leaderboard / Skyscraper |
130635694 | Static Image | Banner / Med-rect / Leaderboard / Skyscraper |
130635706 | MRAID | Banner / Med-rect / Leaderboard / Skyscraper |
130626426 | Rich Media / Video | Interstitial (Video with an end-card + Rich Media Interstitial for 320×480, 480×320, 1024×768 & 768×1024) |
130626427 | Video | Skippable Video |
130626428 | Rewarded | Rewarded Video |
130635048 | Rewarded | Rewarded Video without an end-card |
130783664 | Native | Native with static image main creative |
Supported Callbacks
Banners
Callback Message | Reason |
---|---|
onBannerLoaded | This will be invoked when an advertisement is successfully received. |
onBannerFailed | This will be invoked when an advertisement request is failed or time-to-live of the ad has been expired. |
onBannerClicked | This will be invoked when a click event is registered for an advertisement. |
onBannerImpression | This will be invoked when an impression occurred. |
Interstitials
Callback Message | Reason |
---|---|
onInterstitialLoaded | This will be invoked when an advertisement is successfully received |
onInterstitialFailed | This will be invoked when there was an error during loading/showing an ad or time-to-live of the ad has been expired. |
onInterstitialShown | This will be invoked when a Fullscreen Interstitial Ad ad is opened. |
onInterstitialDismissed | This will be invoked when a Fullscreen Interstitial Ad ad is closed. |
onInterstitialClicked | This will be invoked when a user clicks on an ad. |
onInterstitialImpression | This will be invoked when an impression occurred. |
Rewarded Video
Callback Message | Reason |
---|---|
onRewardedVideoLoadSuccess | This will be invoked when the Rewarded Video Ad is successfully received. |
onRewardedVideoLoadFailure | This will be invoked when there was an error during loading/showing an ad or time-to-live of the Rewarded Video Ad is expired. |
onRewardedVideoPlaybackError | This will be invoked when there is an error during the Rewarded Video Ad playback. |
onRewardedVideoClosed | This will be invoked when the Rewarded Video Ad is closed. |
onRewardedVideoClicked | This will be invoked when the Rewarded Video Ad is clicked. |
onRewardedVideoStarted | This will be invoked when the Rewarded Video Ad starts playing. |
onRewardedVideoCompleted | This will be invoked when the Rewarded Video Ad is completed and the user should be rewarded. |
Last Modified: January 14, 2021 at 3:25 am
© 2005-2020 Smaato, Inc. All Rights Reserved. Smaato® is a registered trademark of Smaato, Inc.