Microsoft Advertising for Windows Phone

Monkey Targets Forums/XNA/Microsoft Advertising for Windows Phone

Xaron(Posted 2011) [#1]
Hi all,

as Admob only works with Silverlight at the moment and Monkey uses XNA, I'll post a guide how to setup the Microsoft advertising stuff for Windows Phone. It's quite straight forward.

I don't know how it is compared to Admob yet but will post my results of course...

So how does it work?

(1) First download the advertising SDK here:
http://www.microsoft.com/download/en/details.aspx?id=8729

(2) Register with the pubcenter:
http://pubcenter.microsoft.com/

(3) Build your project with Monkey for XNA and create the Windows Phone 7 copy within Visual Studio.

(4) Add the reference to the Ad SDK:




(5) Code changes:

Now open the program.cs file and look for the following lines:
public class gxtkGame : Game{

	public gxtkApp app;
	
	public GraphicsDeviceManager deviceManager;


And replace it with:
public class gxtkGame : Game{

	public gxtkApp app;
	
	public GraphicsDeviceManager deviceManager;

    static AdGameComponent adGameComponent;
    static DrawableAd bannerAd;


Look for:
	public gxtkGame(){
	
		gxtkApp.game=this;
	
		deviceManager=new GraphicsDeviceManager( this );

		deviceManager.IsFullScreen=MonkeyConfig.WINDOW_FULLSCREEN;
		deviceManager.PreferredBackBufferWidth=MonkeyConfig.WINDOW_WIDTH;
		deviceManager.PreferredBackBufferHeight=MonkeyConfig.WINDOW_HEIGHT;
		Window.AllowUserResizing=MonkeyConfig.WINDOW_RESIZABLE;


And replace it with:
	public gxtkGame(){
	
		gxtkApp.game=this;
	
		deviceManager=new GraphicsDeviceManager( this );

		deviceManager.IsFullScreen=MonkeyConfig.WINDOW_FULLSCREEN;
		deviceManager.PreferredBackBufferWidth=MonkeyConfig.WINDOW_WIDTH;
		deviceManager.PreferredBackBufferHeight=MonkeyConfig.WINDOW_HEIGHT;
		Window.AllowUserResizing=MonkeyConfig.WINDOW_RESIZABLE;

        // Create an adGameComponent for this game
        AdGameComponent.Initialize(this, "test_client");
        adGameComponent = AdGameComponent.Current;
        Components.Add(adGameComponent);


Now look for:

	protected override void LoadContent(){
		try{

			bb_.bbInit();
			bb_.bbMain();
			
			if( app!=null ) app.InvokeOnCreate();


and replace it with:
	protected override void LoadContent(){
		try{

			bb_.bbInit();
			bb_.bbMain();
			
			if( app!=null ) app.InvokeOnCreate();

            // Create a banner ad for the game.
            bannerAd = adGameComponent.CreateAd("Image480_80", new Rectangle(0, 720, GraphicsDevice.Viewport.Bounds.Width, 80));


Now build the phone project. Please note that the XBox project now will fail to build.

That's it! Now you should see a test banner:



To have real ads, replace
AdGameComponent.Initialize(this, "test_client");


with
AdGameComponent.Initialize(this, "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx");


and
bannerAd = adGameComponent.CreateAd("Image480_80", new Rectangle(0, 720, GraphicsDevice.Viewport.Bounds.Width, 80));


with
bannerAd = adGameComponent.CreateAd("xxxx", new Rectangle(0, 720, GraphicsDevice.Viewport.Bounds.Width, 80));



Xaron(Posted 2011) [#2]
If you want to make it even better, add the following two methods to Program.cs:


    /// This is called whenever a new ad is received by the ad client.
    private void bannerAd_AdRefreshed(object sender, EventArgs e)
    {
        bb_std_lang.Print("Ad received successfully");
    }

    /// This is called when an error occurs during the retrieval of an ad.
    private void bannerAd_ErrorOccurred(object sender, Microsoft.Advertising.AdErrorEventArgs e)
    {
        bb_std_lang.Print("Ad error: " + e.Error.Message);
    }


And add the following two lines to the LoadContent method:

            // Add handlers for events (optional).
            bannerAd.ErrorOccurred += new EventHandler<Microsoft.Advertising.AdErrorEventArgs>(bannerAd_ErrorOccurred);
            bannerAd.AdRefreshed += new EventHandler(bannerAd_AdRefreshed);



dave.h(Posted 2012) [#3]
thanks for the tutorial but when change the code and try to deploy to the emulator i get the error

Error 13 The type or namespace name 'AdGameComponent' could not be found (are you missing a using directive or an assembly reference?) C:\Users\dave\Desktop\puzzle\puzzle.build\xna\MonkeyGame\MonkeyGame\Program.cs 254 12 Windows Phone Copy of MonkeyGame

and

Error 14 The type or namespace name 'DrawableAd' could not be found (are you missing a using directive or an assembly reference?) C:\Users\dave\Desktop\puzzle\puzzle.build\xna\MonkeyGame\MonkeyGame\Program.cs 255 12 Windows Phone Copy of MonkeyGame

have you any idea what this means please.


dave.h(Posted 2012) [#4]
i know the error is something to do with

static AdGameComponent adGameComponent;
static DrawableAd bannerAd;

so i was thinking is there something ive not downloaded or installed in the correct directory.Any general pointers would be appreciated


Xaron(Posted 2012) [#5]
Hmm strange. But the reference is there, have you added it?


Raz(Posted 2012) [#6]
Thanks for taking the time to write this. I'm not specifically looking to make a mobile game right now but I definitely appreciate the information being available when/if I do :)


dave.h(Posted 2012) [#7]
@xaron yup i added it like in your example but the strangle thing is when i do this with the windows phone copy i get a few yellow triangles against some of the other references .I will upload a screenshot if someone can tell me how to put images in a post.


Xaron(Posted 2012) [#8]
Do you have the 7.0 SDK or the 7.1 SDK?


dave.h(Posted 2012) [#9]
i may have discovered my error i downloaded WPAdSDK-ENU.msi thinking it was the sdk.The sdk is about 500 meg so once ive installed it and gone through your tutorial again i will post my results back.what a stupid mistake to make.It wasnt till you asked what version i downloaded that i realised my mistake so thanks for that.


Xaron(Posted 2012) [#10]
No problem! :) Fingers crossed! Good luck!


dave.h(Posted 2012) [#11]
ok i got it working but i dont know if ive messed something up setting everything up initially as i have to add this line to get it to work.

using Microsoft.Advertising.Mobile.Xna;

and also most of the microsoft.xna.framework in the reference just gave me the yellow triangle warning and didnt point to any dll file.so i manually set each one to its correct dll and the warning went.Then i ran it in the emulator and it worked just like your screenshot.Why cant i ever do things the easy way.Thanks for the tutorial xaron i would never have figured this out on my own.


dave.h(Posted 2012) [#12]
just in case anyone else gets a problem like mine the problem ive had was that i had more than the windows phone copy of monkeygame open.Also i needed

using Microsoft.Advertising.Mobile.Xna; to be added

when you have more than the windows phone copy open everything messes up and
AdGameComponent and DrawableAd is not recognised.A silly mistake on my part but not the last one i will make.Im already planning on messing up ios when i get a mac.Xaron tutorial works great in either landscape or potrait mode too.


Xaron(Posted 2012) [#13]
Great! You're welcome! :)


dave.h(Posted 2012) [#14]
im just trying to get the real adds on my phone but a little confused with this line i have to change.

bannerAd = adGameComponent.CreateAd("xxxx", new Rectangle(0, 720, GraphicsDevice.Viewport.Bounds.Width, 80));

is the "xxxx" supposed to be my AD UNIT ID or litrally do i type in "xxxx" here as when i deploy this to my phone no ads appear.Or is it because ive not submitted the app to the app hub yet.The test ads work perfectly in the emulator.


Xaron(Posted 2012) [#15]
Yep, that should be the ad unit id.

Don't worry I haven't seen the real ads by myself even on my phone but it works when it's in the market. Strange enough!


dave.h(Posted 2012) [#16]
argghhh it got published then i realised i mistyped the ad unit id.No adverts in my app so now i gotta submit an update.LOL its all a learning curve.


tagoror(Posted 2013) [#17]
Hello,

I need a little of help with Microsoft Ads because I don't know what's happening.

I did all the modifications to the my project and I checked all the steps several times. I'm not getting errors, but the ads are not displayed.

Any idea?

Regards,

Javier


benmc(Posted 2013) [#18]
I just wanted to give this thread a bump because I got ads to work in Monkey v69 with a few modifications. If anyone thinks there's something wrong with this method, let me know, but it appears to be working well for me.

Basically you have to do what's discussed above, but the locations where you put some of the code has changed.

First, make sure you include the following "using" after installing the SDK:

using Microsoft.Advertising.Mobile.Xna;


I defined the static vars in the Program.cs here:

public class BBXnaGame : BBGame{

#if WINDOWS_PHONE
    static AdGameComponent adGameComponent;
    static DrawableAd bannerAd;
#endif

...



Then to create the ad:

public BBXnaGame( Game app ){

       // The below 4 lines should already be in there:
       _app=app;
       _xnaGame=this;
	GraphicsDeviceManager devman=new GraphicsDeviceManager( _app );		
	devman.PreparingDeviceSettings+=new EventHandler<PreparingDeviceSettingsEventArgs>( PreparingDeviceSettings );

       // Then I added the following:

        AdGameComponent.Initialize(_app, "test_client");
        // replace test_client with your applicationID to get live ads

        adGameComponent = AdGameComponent.Current;
        
        _app.Components.Add(adGameComponent);

        bannerAd = adGameComponent.CreateAd("Image480_80", new Rectangle(0, 400, 480, 80));
        // adjust the rectangle size and replace Image480_80 with your adunitID to get live ads

...



Other than what's already talked about above, I was able to get ads running doing this in Monkey v69 on my Windows Phone 7.

One more thing to note is that live ads didn't show up for me in testing for about 24 hours after I created the application ID and ad unit in pubCenter.