Thursday, 22 May 2014

[Solved] Monetizing your Windows Phone 8 App with Google Ad Mob


Hi Everybody,

As you know the Google admob was already providing services to monetize smart phone applications through implementation of ads. But it was only limited to Android and iOS 

Now Google admob also allows you to monetize your windows phone application by following few simple steps. But before following step let me tell you one thing that google Admob provides two kind of ads. 

1- Interstitial Ads. (Covers whole page).
2- Banner Ads. (Ads like banner).

Now its upon you that which kind of ads you want to implement in your application but I will show you implementation process for both.  

1 - Signup with google admob
2 - Setting up your payment methods
3 - Click Monetize New App
4 - Click add application manually tab. 
5 - Give the name of application, select platform windows phone 8 and click add app.

6 - Now after creating app select the type of ads either banner or Interstitial. give ad unit name and click save. 

7 - After successful creation of ad unit of application copy the AD Unit ID probably starting from ca-pub- etc. etc. 
8 - Download the SDK for windows phone 8 (Click Here to download)
9 - Extract the downloaded zip foler.
10 - Open extracted folder > lib > windowsphone8, you will find GoogleAds.dll file


Networking Capability is required for both banner and Interstitial. 
Web Browser Compnent Capability is required only for Interstitial. 

11 -  Open WMAppManifest.xml from properties folder in Solution Explorer.

Click Capabilities tab and check ID_CAP_Networking Capability and ID_CAP_WEBBROWSERCOMPONENET

12 -  Right Click on Project in Solution Explorer > Go to  Add Menu > Select Reference... > Click Browse and Select GoogleAds.dll in your extracted folder. 
If you still don't have GoogleAds.dll Click Here to download. 

Note: If you unable to add GoogAds.dll or visual studio is giving error right click on GoogleAds.dll in folder go to its properties and click unblock from its general tab 

13 - Open App.xaml.cs from solution explorer and paste the code below 

 public static InterstitialAd interstitialAd;

        public static void RequestAd()
            interstitialAd = new InterstitialAd("");
            // NOTE: You can edit the event handler to do something custom here. Once the
            // interstitial is received it can be shown whenever you want.
            interstitialAd.ReceivedAd += OnAdReceived;
            interstitialAd.FailedToReceiveAd += OnFailedToReceiveAd;
            interstitialAd.DismissingOverlay += OnDismissingOverlay;
            AdRequest adRequest = new AdRequest();
            adRequest.ForceTesting = false;
        public static void OnAdReceived(object sender, AdEventArgs e)
            Debug.WriteLine("Received interstitial successfully. Click 'Show Interstitial'.");

        public static void OnDismissingOverlay(object sender, AdEventArgs e)
            Debug.WriteLine("Dismissing interstitial.");

        public static void OnFailedToReceiveAd(object sender, AdErrorEventArgs errorCode)
            Debug.WriteLine("Failed to receive interstitial with error " + errorCode.ErrorCode);

Code will give error after pasting it but don't worry just right click InterstitialAd and resolve it.

Your code will look like this 

14- Copy this in constructor of MainPage.xaml.cs


15 - Build the solution and Run it on device or windows phone emulator. When the page is loaded you will see add after some seconds or mili seconds.

Above picture is screenshot taken from deployed application on device. representing Interstitial Ads 

Add two buttons on Main Page  one for loading banner ad and one for interstitial ad. Create Click event for both buttons.

In interstitial button event cut App.RequestAd from constructor and paste in it. 

Paste the following code in App.xaml.cs below the code of Interstitial Ads

  public static void LoadBanner(Grid grid)
            AdView bannerAd = new AdView
                // Replace your AD Unit ID before uploading 
                Format = AdFormats.Banner,
                AdUnitID = ""

            bannerAd.FailedToReceiveAd += OnFailedToReceiveAd;
            /* If you want to set position of your ad uncomment this code 

            bannerAd.HorizontalAlignment = HorizontalAlignment.Left;
            bannerAd.VerticalAlignment = VerticalAlignment.Top;
            // replace 100, 100 with any x and y position
            bannerAd.Margin = new Thickness(100, 100, 0, 0);

            AdRequest adRequest = new AdRequest();
            adRequest.ForceTesting = false;


Call App.LoadBanner(LayoutRoot);  in Banner Button Click Event 

Note: Code for positioning banner ad is commented in above code you can reposition the banner ad by uncommenting it. 

Run application and try both buttons to load both type of ads. below are the screen shots of both ads loaded in application.  


Now both type of ads are loaded successfully. But there are still some issues in it. When interstitial ad is clicked and back button is pressed application crashes. This error is in SDK provided by google admob and they are familier with it but didn't take any action yet, hope this issue will be resolved soon but for the time I have found solution which I will explain below. 

Go to App.xaml.cs and replace event 

private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
            if (Debugger.IsAttached)
                // An unhandled exception has occurred; break into the debugger
with this code 

 private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
            String ex = e.ExceptionObject.ToString().ToLower();
            if(ex.Contains("frameworkdispatcher.update")||ex.Contains("the given key was not present in the dictionary"))
                e.Handled = true;

This will disturb while debugging you will need to press continue button when it will cath exception but it will work in normal conditions. 

There is another thing when request for loading ad is given mutiple times, app crashes. So to avoid this you need to implement a simple logic using Boolean flag 

1- When once app request is done boolean flag should become true and when ad is received or failed then it should be false. So next time ad request can be done. 

Now you have successfully implmeneted both ads for your windows phone 8 application. 


Click here to download sample project

Windows Mobile Professional

1 comment: