Download the Code Sample
While some developers create mobile applications as a hobby or as a way to see their names in lights, for others it’s all about the money. As you may already know, the Windows Phone Marketplace offers a simple way to get paid for your hard work by selling apps to users. However, there’s also another, complementary way for you to make money from your Windows Phone 7 applications: advertising. The Microsoft Advertising SDK makes the process of incorporating ads into your apps easy—and you’ll get paid when consumers use your app, even if it’s free to download.
This article will:
The reality is that mobile users are, for the lack of a better word, “thrifty.” Many who won’t hesitate for a second to spend $4 on a double latte will agonize over a 99-cent app purchase. Advertising enables you to still make money from those users who aren’t ready or willing to spend their hard-earned pennies to buy your app. Free apps, “lite” versions of paid apps and trials of paid apps all present ad monetization opportunities. Given that downloads of free and trial apps greatly outpace those of paid apps in the Windows Phone Marketplace, showing ads in your app is simply good math.
The Microsoft Advertising SDK is a free download and the service is free to use. Furthermore, the ads shown when you use the Microsoft Ad Control aren’t just from Microsoft adCenter. Instead, Microsoft has created a real-time ad exchange where top-tier mobile ad networks bid for the right to show their ads in your app. This improves ad rates overall and ensures that advertisers are paying top dollar for ads in mobile apps on Windows Phone 7. You get paid 70 percent of the revenue the ad impressions in your app generate monthly.
As you’ll see later in this article, using the Microsoft Ad Control is easy enough that even part-time developers and students are getting into the act of building ad-supported Windows Phone 7 apps. Your mileage may vary, of course; if your app engages end users and they use it often, the money-making potential goes far beyond pocket change. Many developers have made more from advertising than they have through app sales, and Microsoft has published a couple success stories that highlight a few of these experiences.
Getting started with the Microsoft Ad Control is easy and, even if you’re still a novice Windows Phone 7 developer, you can be up and running in no time. Here are the steps to include Microsoft Advertising in your application:
Once your app is live in the Windows Phone Marketplace and starts getting used by end users, it will automatically start generating ad impressions. You’ll get paid for showing these ads on a CPM-basis (cost per thousand views; see “Advertising 101” for more on the cost breakdown) and after you’ve hit a minimum threshold of $50, you’ll automatically get issued a check or direct deposit—your choice—from Microsoft.
Your first step in working with ads from Microsoft is to sign up at the Microsoft Advertising pubCenter portal at pubcenter.microsoft.com using your Windows Live ID (see Figure 1).
Figure 1 The Microsoft Advertising pubCenter Portal
You’ll be asked to enter some information about yourself or your company, then you can jump right in and register your first application and “ad unit.”
In order to define a new application in pubCenter, all you have to do is give it a friendly name. Later on, if you have more than one ad-supported application, you’ll be able to break out the statistics to see how the ads in each of your applications are performing.
On the same screen, you can also define your first ad unit. Each ad unit has a name and represents a location in your app where advertising will be shown. Ad units can be associated with up to three categories that best describe the content surrounding them (see Figure 2).
Figure 2 Registering Your App and Creating an Ad Unit
The categories you select will be one of the many factors that determine which ads are displayed in your app.
Once you click Save, you’ll see a summary of the information for your new pubCenter account and also the IDs for the application and ad unit you just created. When you’re ready to go live, you’ll use these values with the Ad Control to identify your app to the Microsoft Advertising servers.
At this point, you’re done with pubCenter for the time being and are ready to dive into Visual Studio. You can return to pubCenter at any time to define more applications or ad units, see in-depth advertising reports and configure advanced settings such as competitive exclusions.
If you’re not using the latest Windows Phone Developer Tools yet, you can download the newest version of the Microsoft Advertising SDK from pubCenter. This SDK includes the Microsoft Ad Control as well as documentation and code samples to help you get started. The June 2011 version of the Ad Control offers rich media support and an improved API set. Make sure you have it installed so that you can follow along. Let’s look at how to use it from the Visual Studio Designer.
Start by creating a new Windows Phone 7 Silverlight project with File | New, and opening up the XAML surface where you want to show ads. Pick a suitable spot where the ad can be frequently shown, but won’t annoy the user; typically, the bottom or top of the screen is best. Drag the AdControl (note: “Ad Control” refers to the Microsoft product, while “AdControl” refers to the class or object) directly from the Toolbox pane to add it directly to a Grid control (see Figure 3).
Figure 3 Placing the AdControl
Once the AdControl is on the page, you need to set some properties for it to work correctly in your application. First, you’ll need to double-check the width and height of the AdControl. It’s recommended to keep the control at the standard dimension of 480 x 80 pixels for mobile ads. Setting a smaller size will prevent standard-sized ads from being displayed on your page. Setting a larger size will mean that there will be some empty space between where the ad is rendered and the border of the AdControl.
Next, you’ll need to link the AdControl with the Application ID and Ad Unit ID you created in the pubCenter portal. It helps to have the portal Web page open so you can copy and paste these values into the Visual Studio window.
Finally, there are two properties you’ll want to set to meet your app’s needs. If set to true, the IsAutoRefreshEnabled property automatically shows a new ad every 60 seconds. This enables you to increase the number of ads a user sees, which should lead to better revenue for your app (see “Advertising 101” for more on ad revenue). Set the IsAutoCollapseEnabled property to true if you want the AdControl to automatically hide itself from view before an ad is downloaded.
The XAML generated by the Visual Studio Designer should look something like this:
<Grid x:Name="ContentPanel" Grid.Row="1">
Now hit Run to see your first ads.
If you’re finding that you want to do some fancier things, such as controlling the hiding or showing of ads, or you want to have a bit more control over when the ad shows in your application, you can chose to instantiate the AdControl directly in your code.
But before we get there, let’s start by dissecting how the AdControl works (see Figure 4). Behind the scenes, when your app starts and the AdControl gets initialized, it requests an ad from the Microsoft Advertising servers. The request is formed with the Application ID and Ad Unit ID that you specified in your code.
Figure 4 How the AdControl Works
It’s critical that this information matches what you copied from the pubCenter portal or to the test IDs provided in the documentation. The request also contains some other useful information about your app, such as the region to which ads will be matched.
If the ad it receives back uses images or HTML-based rich media, the AdControl will reconnect with a Web server to download the advertising content (such as JPEG, GIF, HTML or PNG files). Once the user has seen the ad, the AdControl may connect again with the server to record the ad impression. This whole cycle repeats when the ad gets refreshed and a new ad gets downloaded.
There isn’t much in the way of rocket science when it comes to the code required to use the AdControl. You’ll need to start by adding the Microsoft.Advertising.Mobile.UI assembly as a reference in your project. You’ll also want to add a using statement for the Microsoft.Advertising.Mobile.UI namespace. And in your page’s constructor, you’ll place the code to create the AdControl. You’ll then need to add the AdControl instance into the page’s visual tree. In this case, we’re adding it to a grid that we have in our layout (see Figure 5). With the code in Figure 5 in place, your app should be ready to build and show ads using the code you’ve written.
Figure 5 Adding the AdControl to the Visual Tree
AdControl adControl = new AdControl();
// Add the control to a grid control
Grid grid = (Grid)this.LayoutRoot.Children;
// Insert real values from pubCenter before
// submitting your app to Windows Phone Marketplace!
adControl.ApplicationId = "test_client";
adControl.AdUnitId = "Image480_80";
// Use your real Application ID and Ad Unit ID here
adControl.ApplicationId = "12ab456c-de7f-89ab-0123-4567890c1d23";
adControl.AdUnitId = "12345";
// Make the AdControl fit the standard 480 x 80 dimension
adControl.Width = 480;
adControl.Height = 80;
// Let the AdControl collapse and refresh automatically
adControl.IsAutoRefreshEnabled = true;
adControl.IsAutoCollapseEnabled = true;
Here are a couple of important things to keep in mind when working with the AdControl in your app, and a few ways—such as targeting— that will help you maximize the value of the advertising in your app.
First, always make sure you can see both “test” and “real” ads running in your application. To view test ads, set the ApplicationID to “test_client” and Ad Unit ID to a standard test value such as “Image480_80.” A well-placed #ifdef can come in handy here. You should use test ads while developing and testing your app, but once you’re ready to release, it’s critical you set this back to your pubCenter-issued Application ID and Ad Unit ID. However, testing with your own app IDs is important because it ensures your application works with “real” live ads.
Next, to get the best ads, it’s important to provide as much information as possible to the ad system. The AdControl often gets this information automatically, so your application manifest must have the following capabilities:
If it makes sense for your application, it’s also recommended to use the ID_CAP_LOCATION capability. Enabling location-based advertising improves the quality and relevance of the ads that your users will see.
The AdControl.PostalCode and AdControl.Country properties allow an app to update the user's location. In the following example, a Beverly Hills movie star app would generally know that its users are around that location:
// Make sure the AdControl is created
adControl.Country = "US";
adControl.PostalCode = "90210";
Similarly, it might also be useful to handle a few things about the ad experience in your code. If you’ve set the IsAutoRefreshEnabled property to false, then you’ll be able to refresh ads manually. Call the adControl.Refresh method to bring a new ad into view, but make sure not to call it more than every 30 seconds in order to give users enough time to click on ads they might be interested in.
If you’ve set the IsAutoCollapseEnabled property to false, you can listen for an ErrorOccurred event. This event will fire whenever an ad fails to download or display. You can use this event to collapse the AdControl and reclaim that space for use by your application’s other UI elements. When the AdRefreshed event fires, you’ll know a new ad is ready to be seen and you can restore its visibility (see Figure 6).
Figure 6 The AdRefreshed Event
public void AdControl_AdRefreshed(object sender, EventArgs args)
AdControl ad = (AdControl)sender;
ad.Visibility = System.Windows.Visibility.Visible;
"ad control '" + ad.Name + "' got ad, visibility = " + ad.Visibility);
public void AdControl_ErrorOccurred(object sender, AdErrorEventArgs args)
AdControl ad = (AdControl)sender;
ad.Visibility = System.Windows.Visibility.Collapsed;
"error in ad control '" + ad.Name + "': " + args.Error.Message);
Debug.WriteLine("ad control '" + ad.Name + "' visibility = " + ad.Visibility);
catch (Exception e)
Debug.WriteLine("oh no! " + e.Message);
As you’ve seen, using the AdControl in your application is easy. And if you run into trouble, there are generally only a few things that can go wrong and cause ads not to be shown:
Finally, if you’re writing a game and want to use XNA, look at the Microsoft Advertising XNA advertising game component that’s also part of the Microsoft Advertising SDK. Unfortunately, there isn’t enough space to go in-depth on XNA in this article, but there is documentation available online at bit.ly/jhGOVV.
Now go add some advertising and start getting paid for all that hard work you put into your application!
Arthur Bierer is a senior program manager on the Universal Ad Client team at Microsoft in New York. Before working on the Microsoft Advertising SDK for Windows Phone 7, he had 17 years of experience with the Dynamics NAV, .NET Framework, Internet Explorer and Windows products. You can follow him on Twitter at twitter.com/ArthurBiererDev or e-mail him at email@example.com.
Boris Feldman is a group product planning manager for the Mobile Display Advertising business at Microsoft. During his eight years at Microsoft, he has also worked on a number of developer products, including the .NET Framework and Expression Web. You can reach him by e-mail at firstname.lastname@example.org.
Thanks to the following technical experts for reviewing this article:Anzor Balkar, Richard Carr, Sloan Ginn, Ali Heron, Darryl Hudgin, Marty Kauhanen, Mark Masterson, Tim McClelland, Valeriy Ovechkin, Chris Quon, Matt Sullivanand Jason White
I’m using adtomatik for the last two months ago and got very good results, higher fill rates and best ecpm than others ad networks
Hi, Please tell me how to display the default advertisement(own ad) if there is no advertisements and how to add background image to a ad control using styles. Thanks in Advance
More MSDN Magazine Blog entries >
Browse All MSDN Magazines
Subscribe to MSDN Flash newsletter
Receive the MSDN Flash e-mail newsletter every other week, with news and information personalized to your interests and areas of focus.