Using in App purchase to implement `Block Ads’ Feature in Windows Phone app

In my last blog post I explained how to add in app purchase in Windows Phone app. In this post I will show how IAP can be used to implement `Block Ads’ feature. Suppose you have a free app in the marketplace with in app advertisements. Most likely you wish to keep the app free so that it gets the most number of downloads. Still the advertisements may be annoying to some users and it would be great if you let your users remove those ads by making an in app purchase. Similar feature can also be achieved by implementing trial functionality in your app. I believe IAP is easier to implement and provides much more flexibility than trial apps. You have already seen how to set up IAP in your app so I will don’t discuss it again. Here is how you can implement the `Block Ads’ functionality

Step 1. Follow the following tutorial to set up in app purchase in your app

http://www.windowsapptutorials.com/windows-phone/add-in-app-purchases-in-your-windows-phone-app/

Step 2. Set up in app advertising in your app

If you haven’t added in app ads in your app then you could follow this tutorial to add Google Admob in your app.

http://www.windowsapptutorials.com/windows-phone/how-to-use-google-admob-in-windows-phone-app/

Step 3. Create a public Class AppSettings global to the project

Define a public class AppSettings which is global to the whole project with two public data variables DISPLAYADS and ISADBLOCKERACTIVE. You can define a public class by placing it just below the namespace of the project. In the sample provided I have defined it in App.xaml.cs just below the namespace of my project WAT_Test.

Step 4. Code to assign values to DISPLAYADS and ISADBLOCKERACTIVE

We will update the values of these variables when the app is launched and check whether the user has purchased the IAP to remove ads. If he has purchased it then ISADBLOCKERACTIVE will be set to true and DISPLAYADS will be set to false.

in_app

When I discussed how to use google admob in an app, I showed that it can be done through XAML or using C# code. So here too I will discuss how to block ads when it has been placed using XAML or via C#.

Blocking ads that were placed using XAML

  • Add a reference to your project in App.xaml as shown below in file reference.xaml
  • Add a new application resource ApplicationSettings which will use the AppSettings class in the section of Application.Resources as shown below in file local.xaml

in_app

  • Add a value converter class in App.xaml.cs named BooleanToVisibilityConvertor. This class implements IValueConverter converts a boolean value to an equivalent visibility and vice-versa.
  • Add a new application resource BooleanToVisibilityConverter in Application.Resources as shown in local.xaml file below
  • Now you can modify your AdView code for Admob to place a visibility condition as shown in the code adview.xaml

 


Blocking ads that were placed using C#

This is much simpler and you just need to check whether DISPLAYADS is true or false before calling admob() function.

These screen shots show that earlier ads were being displayed in the app but after IAP they didn’t appear. Be assured it wasn’t coincidence as I have getting 100% fill rate for Google Admob for the past few days.

Download full project WAT_Test.zip

How to use Google AdMob in Windows Phone app

When it comes to monetization using in-app advertisements in Windows Phone apps, there are quite a few option like Microsoft’s Pubcenter, Mobfox, Smaato, AdDuplex, Nokia’s Inneractive and Google’s Admob. In the past two years I have tried all these ad providers and have finally struck to Google’s Admob. There was a period when Microsoft’s Pubcenter gave the best returns but gradually its fill rates started dropping and earnings dropped by around 99.5% for developers outside US. That made me try other ad providers available for WP and frankly none of them are as good as Pubcenter(the old one) but now developers don’t have a choice. In this post I will show you how you can integrate AdMob in your Windows Phone app. You can add AdMob in your app by XAML or via code in C#.

UPDATE:

We have noticed a improved eCPM(0.2 $ to 1.1$) and fill rates(60% to 90%) for pubcenter. So you might consider using pubcenter in your windows phone app.

If you have significant traffic in India then you can give a try to Vserv for Windows Phone 8 which gives a better eCPM than AdMob.

 

Step 1. Getting the SDK

Firstly download Google Admob SDK for Windows Phone from the link below

https://developers.google.com/mobile-ads-sdk/

 

Step 2. Creating an ad unit 

Go to your AdMob account and create an ad unit for your app

https://apps.admob.com/#home

Note: You would need a upgraded Admob account for this purpose and the previous admob account is no longer supported. You can read more about it here.

 

Fill in the details to create an AdUnit as follows

  • Fill in your app name and select platform

  • You can configure analytics for your app and link it to your Google analytics account. Learn how to integrate Google analytics in your app. We will skip it for now.
  • Select the type of AdUnit and fill in the name for the AdUnit. You can customize the look of the ad whenever you want
  • Click on save and an AdUnit id would be generated.

 

 

All done you are ready setting thing up for AdMob.

 

Step 3. Add the GoogleAds reference to your project

Unzip the SDK files and you will find a GoogleAds.dll file in it. Create a new project and add a reference to this dll file in your project. Here’s an article explaining how to integrate a Toolkit or Third party SDK with windows phone app using NuGet Package Manager.

 

Note

If the GoogleAds.dll is showing as incompatible then follow these steps

  • In the file explorer, navigate to the Google Ads.dll
  • Right click the dll file and click ‘Properties’
  • In the ‘General’ tab, at the bottom, there will be an ‘unblock’ button
  • Unblock the dll file and add it again in your project

 

IMPORTANT: You will need to add the following capabilities in your app for ads to appear!

  • ID_CAP_NETWORKING: Access to network services is required when requesting ads.
  • ID_CAP_WEBBROWSERCOMPONENT: Required since the AdView is a web browser.
  • ID_CAP_MEDIALIB_PLAYBACK: Provides access for currently playing media items.
  • ID_CAP_MEDIALIB_AUDIO: Provides read access to audio items in media library.

 

Now you can either add ads using XAML or via C#

How to add ads using XAML

 

Go to Toolbox in your Visual Studio and right click on general category and select Choose Items.

 

 

From the choose items dialog box tick AdView from Google Ads and click OK to include the control in the Toolbox.

 

 

Now you can view AdView control in the Common Controls list of the Toolbox. Select and drag it to place it anywhere in the page

 

 

Add the AdUnit ID you generated earlier to the control. The XAML should now look similar to this.
  If you have added the XAML code directly then a reference will be required for AdView control   xmlns:GoogleAds="clr-namespace:GoogleAds;assembly=GoogleAds"  

How to add ads using C#

  • Add the code below to display ads using C#
  • You can see the function admob(StackPanel stck ) takes a parameter which is the name of the stackpanel in which you wish to place your ad. Instead of a stackpanel you could use a Grid too.
  • Add the StackPanel in which you wish to place your ad in the XAML
  • Call the admob function in the page constructor as follows
    admob(AdGrid);
  • The advantage of this procedure is that you could place multiple ads in different StackPanels or Grid just by changing the name of the StackPanel/Grid in the parameter

 

Here is how the app looks when you run it in the emulator.

 

 

Download full project Google Admob.zip