Implementing advert in Windows Phone app using Nokia NAX with C#

One of the new way of advertising in Windows Phone application is using Nokia NAX. Nokia Ad Exchange (NAX) is a mobile in-app advertising exchange offering access to the top ad networks in the world. With one API and one partner, you’ll get access to over 120 ad agencies and networks worldwide. One of the key concepts for NAX are:

  • Optimization across 120+ ad networks
  • Payment enabled in over 200 countries
  • Manage your own ad campaigns to promote your app
  • Powerful ad performance dashboard
  • NAX is free for developers

Dashboard looks like:

image

Nokia NAX is powered by Inneractive.

So, if you live in country where you can’t use Microsft Ad Exchange, you can easily use NAX to earn some money from your Windows Phone app. You only need PayPal account to get your money.

I’ll show you how to implement location based NAX ad (Banner) in your Windows Phone 8 application.

Step 1 – Register and download SDK

First what you need to do is to register on https://nax.nokia.com.

After that you need to download SDK from SDKs page for Windows Phone 8. Current version is 1.1.3 from 02/14/2013. I propose you  to look at Ad Placement Strategy.html document in folder Documentation with suggestions where to put your advert. From location depends how much will you earn from ad.

Step 2 – Put ad files into Windows Phone project

From InneractiveAdSDK folder of extracted SDK copy files to the root of your Visual Studio project (root because of simplicity, you can put these files in separate folder):

  • Inneractive.Ad.dll
  • InneractiveAdLocation.cs (use this file only if you want to use location based ad in your WP app)

image

and then in Visual Studio for your Project do Add/Existing Item and choose both of the files.

Step 3 – Register NAX add dll file

You need to register Inneractive.Ad.dll file. Use References/Add Reference, click Browse button and find dll file from your Windows Phone solution.

After successful adding of dll file you need to get this reference:

image

Step 4 – Include capabilities

In order to NAX work in your app you need to activate some of the capabilities. From Properties/WMAppManifest.xaml file activate next check boxes in Capabilities section:

  • ID_CAP_LOCATION
  • ID_CAP_NETWORKING
  • ID_CAP_WEBBROWSERCOMPONENT
  • ID_CAP_PHONEDIALER
  • ID_CAP_IDENTITY_DEVICE

Step 5 – Display NAX ad in XAML

First, you need to add control in XAML where your ad will be positioned. We will use nax_control StackPanel using Grid:

<Grid x:Name=”ContentPanel” Grid.Row=”0″ Margin=”12,0,12,0″>
  <Grid.RowDefinitions>
    <RowDefinition Height=”*”/>
    <RowDefinition Height=”53″/>
</Grid.RowDefinitions>

<ListBox Grid.Row=”0″>
</ListBox>
  <StackPanel Height=”53″ Name=”nax_control” Grid.Row=”1″>
  </StackPanel>
</Grid
>

List of Supported ad sizes:

  • 300 x 50
  • 320 x 53
  • 300 x 250 (Rectangle)
  • 320 x 480 (Full Screen)

We will use 320×53 pixel size on the bottom of the page using Grid.

The final result is:

NAXOnPage

Step 6 – C# code for NAX

After adding control in XAML code we need to write some code using C#. First we need to add two namespace in page where we want to put our ad control, in this case MainPage.xaml.cs with:

using Inneractive.Nokia.Ad;
using InneractiveAdLocation;

I would suggest to include namespace:

using Microsoft.Phone.Net.NetworkInformation;

NetworkInformation is to check if internet connection is available with method: DeviceNetworkInformation.IsNetworkAvailable.

Then define optionalParams before main constructor in the class. All logic are in two methods:

  • MainPage_Loaded
  • iaLocation_Done
public partial class MainPage : PhoneApplicationPage
{
  Dictionary<InneractiveAd.IaOptionalParams, string> optionalParams;

  // Constructor
  public MainPage()
  {
    InitializeComponent();

    // Sample code to localize the ApplicationBar
    this.Loaded += new RoutedEventHandler(MainPage_Loaded);
  }

  private void MainPage_Loaded(object sender, RoutedEventArgs e)
  {
    if (DeviceNetworkInformation.IsNetworkAvailable)
    {
      // Watch location
      IaLocationClass iaLocation = new IaLocationClass();
      iaLocation.Done += new System.EventHandler(iaLocation_Done);
      iaLocation.StartWatchLocation();

      optionalParams = new Dictionary<InneractiveAd.IaOptionalParams, string>();
      optionalParams.Add(InneractiveAd.IaOptionalParams.Key_OptionalAdWidth, "320"); //ad width
      optionalParams.Add(InneractiveAd.IaOptionalParams.Key_OptionalAdHeight, "53"); //add height
    }

    //Show Add Banner. Remarks: pay attention to use Application Id from NAX
    if (optionalParams != null && nax_control.Children.Count()== 0)
    {
      InneractiveAd iaBanner = new InneractiveAd("ApplicationId_NAX", InneractiveAd.IaAdType.IaAdType_Banner, 30, optionalParams);
      nax_control.Children.Add(iaBanner);
    }
  }

  void iaLocation_Done(object sender, IaLocationEventArgs e)
  {
    try
    {
      // Add location, if received
      if (e != null && e.location != null)
      optionalParams.Add(InneractiveAd.IaOptionalParams.Key_Gps_Coordinates, e.location);
    }
    catch (Exception ex)
    {
      System.Diagnostics.Debug.WriteLine("Error: " + ex.ToString());
    }
  }
}

Remarks: ApplicationId_NAX is generated from nax.nokia.com from Add App section.

image

You need to enter some of the information such as:

  • Mobile platform
  • Application name
  • Category
  • Does your app use location

After this you get generated Application Id for you application. This is crucial info to track your ad banner in your Windows Phone app in Nokia NAX dashboard.

Generated Application Id (AppID) that you need to use in your app (we call it ApplicationId_NAX in our app):

image

I hope that you can earn some money using Nokia NAX ad.

You can find this article as featured article on developer.nokia.com as Wiki page with example code here.

About these ads

About Spaso Lazarevic

Spaso Lazarevic is Senior Software Developer working with Microsoft technologies. Leader of .NET User Group Bijeljina, speaker at Microsoft events, writter and blogger. Microsoft MVP for Visual C#.
This entry was posted in Programming, Windows Phone and tagged , . Bookmark the permalink.

9 Responses to Implementing advert in Windows Phone app using Nokia NAX with C#

  1. Hi Spaspo

    Very cool! This blog post would be really useful and get more visibility as an entry on Nokia Developer Wiki – it is a topic that hasn’t been covered and which is interesting to a lot of readers.

    Would it be possible to duplicate it there? If you copy paste the content into the wiki I’m happy to format it for you.

    Regards
    Hamish

  2. The size of the ads are ridiculous. If they would make an ad that actually fit the phone (this pubcenter ads) I would go all in. But the ads just look weird when in an app.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s