Overview
Many people here (myself included) use AdMob Mediation to handle allocation of multiple banner ad networks. This service also supports interstitials, and allows you to integrate any number of ad networks using custom events.
But all of this can take a fair bit of work to set up. Sometimes all you need is a quick and easy solution, to choose between two different paths of code in your app. Here I’d like to demonstrate a simple mediation solution using the Remote Settings tool provided by AppBrain.
AppBrain Remote Settings
For those who aren’t already familiar with the service: AppBrain offers an SDK which developers can integrate into their apps. This SDK provides some basic stats about downloads and usage figures, which can be viewed through the AppBrain developer website. The SDK also offers an app wall which can be shown on exit.
What many people don’t know is that the SDK also has an additional feature. Developers can specify a number of settings (in the form of string-value pairs) on the AppBrain website. When the SDK is initialised, these settings are automatically synchronized to the phone’s local storage. They can then be accessed at any time (online or offline), using a simple function call. Click here to read more about the Remote Settings feature.
Example
Here’s a simple example. Let’s say we create an app, and add a setting via the web interface as follows:
[ul]
[li]Setting Name: “welcome_message”[/li][li]Setting Value: “Hello, World!”[/li][/ul]
Now within the app, we initialise the SDK using this function call:
AppBrain.initApp(this);
The remote settings will be downloaded and cached silently in the background. To use the setting somewhere else in the app, we can do something like this:
String welcomeMessage = AppBrain.getSettings().get("welcome_message", "Default text if setting not downloaded yet");
Toast.makeText(this, welcomeMessage, Toast.LENGTH_LONG).show();
If the setting has not been download yet, the default text will be returned. The SDK will only fetch remote settings once a day, so any further updates on the remote end will have to wait 24 hours before they are reflected in the app.
As you can see, this is a very simple and easy way to handle remote settings within your app.
Using Remote Settings For Mediation
Moving on from this example, let’s say you want to integrate app walls from both LeadBolt and SendDroid. It would be possible to write a custom event and use AdMob Mediation for this. But with AppBrain’s remote settings, it is much easier. Here’s how to do it.
[ol]
[li]Integrate the AppBrain SDK.[/li]To start, you’ll need to integrate AppBrain’s SDK by following these instructions. This includes calling init() in your activity’s onCreate() method.
[li]Integrate the two Ad Network SDKs.[/li]Next follow the integration instructions for whichever ad networks you want to use. This could include adding activities and permissions to AndroidManifest.xml, and also initialising the SDKs in your onCreate() method if required. Set it up as per the documentation, or however you normally integrate these networks.
[li]Decide the allocation percentage.[/li]For this example, let’s say we want to allocate 80% of traffic to LeadBolt, and 20% to SendDroid.
[li]Create the remote settings.[/li]Given the percentages above, define two remote settings in AppBrain:
[ul]
[li]Name: allocation_leadbolt[/li]Value: 80
[li]Name: allocation_senddroid[/li]Value: 20
[/ul]
[li]Decide which network to use at runtime.[/li]When you want to display an app wall, use the following code to decide between the two different networks.
// Get a random number <= 100
Random r = new Random();
Integer n = r.nextInt(100);
// Get the network allocations
Integer allocation_leadbolt = Integer.valueOf(AppBrain.getSettings().get("allocation_leadbolt", "50"))
Integer allocation_senddroid = Integer.valueOf(AppBrain.getSettings().get("allocation_senddroid", "50"))
// Choose which network to use
if (n <= allocation_leadbolt) {
// Display LeadBolt App Wall
// ...
} else {
// Display SendDroid App Wall
// ...
}
[/ol]
That’s it! You can use this code to easily switch between two different ad networks using AppBrain’s Remote Settings feature.
But hang on… you didn’t use the second allocation variable![SIZE=1][SIZE=2]
That’s right. In the example above I pulled in two remote settings, but only ended up using one of them (the LeadBolt allocation).
The reason I included the SendDroid allocation as a separate setting is because it’s really quite easy to expand this for 3, or 4 networks. You could do something like this:
[/SIZE][/SIZE]
// Choose which network to use
if (n <= allocation_leadbolt) {
// Display LeadBolt App Wall
// ...
} else if (n <= allocation_senddroid + allocation_leadbolt) {
// Display SendDroid App Wall
// ...
} else if (n <= allocation_airpush + allocation_senddroid + allocation_leadbolt) {
// Display Airpush App Wall
// ...[SIZE=1][SIZE=2]
} else {
[/SIZE][/SIZE]// Display backup ads
// ...[SIZE=1][SIZE=2]
}[/SIZE][/SIZE]
[SIZE=1][SIZE=2]
Of course, that code could be improved a lot. The use of multiple if/else-if statements is unweildy and doesn’t scale well. If you want to write a better version feel free to do so, and share it here!
This example is simply designed to give you an idea of how powerful remote settings can be.
[SIZE=3]Conclusion
Hopefully this tutorial has helped you understand how AppBrain Remote Settings work, and how they can be used to switch between multiple ad networks dynamically. The SDK is very simple, you can use it pretty much anywhere you would like to switch between different code paths.
[/SIZE][/SIZE][/SIZE]Feel free to reply with any questions, comments or improvements to the code samples I’ve given. And let us all know about the creative ways you’re using remote settings in your app!