{"id":750,"date":"2017-12-26T15:42:04","date_gmt":"2017-12-26T15:42:04","guid":{"rendered":"http:\/\/www.netexl.com\/blog\/?p=750"},"modified":"2017-12-27T08:06:47","modified_gmt":"2017-12-27T08:06:47","slug":"monetize-windows-10-store-app-with-ads","status":"publish","type":"post","link":"https:\/\/www.netexl.com\/blog\/monetize-windows-10-store-app-with-ads\/","title":{"rendered":"Monetize Windows 10 Store App with Ads"},"content":{"rendered":"<p>In the <a href=\"https:\/\/www.netexl.com\/blog\/compile-phaser-game-to-windows-10-store-app\/\" target=\"_blank\">previous article<\/a> we converted our Phaser game to a windows 10 store app (UWP app). Now\u00a0we all want to monetize our games. One way of monetizing is by putting ads in our game. Micorsoft has built support for ads in the UWP which offers various ad types and also supports\u00a0mediation with other ad networks.<\/p>\n<p>Refer to following resources to get more detail<\/p>\n<p><a href=\"https:\/\/developer.microsoft.com\/en-us\/store\/monetize\/ads-in-apps\" target=\"_blank\">https:\/\/developer.microsoft.com\/en-us\/store\/monetize\/ads-in-apps<\/a><\/p>\n<p>We will go through steps to quickly add a banner ad to our game.<\/p>\n<p>First step is to add WinJS and Microssoft Advertising SDK to our visual studio project. Open proejct and go to NuGet Package Manager -&gt; Manage NuGet Packages for Solution<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-751\" src=\"https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_1.png\" alt=\"\" width=\"800\" height=\"370\" srcset=\"https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_1.png 800w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_1-300x139.png 300w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_1-768x355.png 768w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_1-280x130.png 280w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>Search for WinJS in Browse section, select the package and click install which will install the package in the current solution. Accept the popup confirmations if you get any.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-752\" src=\"https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_2.png\" alt=\"\" width=\"800\" height=\"370\" srcset=\"https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_2.png 800w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_2-300x139.png 300w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_2-768x355.png 768w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_2-280x130.png 280w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>Next we will add Microssoft Advertising SDK. Simply\u00a0browse\u00a0for Advertising and in the results section, select Micrososft.Advertising.JS and click install.<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-753\" src=\"https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_3.png\" alt=\"\" width=\"800\" height=\"368\" srcset=\"https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_3.png 800w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_3-300x138.png 300w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_3-768x353.png 768w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_3-280x129.png 280w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>Now right click on References in solution explorer and add reference to &#8220;Microsoft Advertising SDK for Javascript&#8221;.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-754\" src=\"https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_4.png\" alt=\"\" width=\"800\" height=\"368\" srcset=\"https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_4.png 800w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_4-300x138.png 300w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_4-768x353.png 768w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_4-280x129.png 280w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>After you have added WinJS and Advertising SDK you should see\u00a0two references and a WinJS folder\u00a0added\u00a0to your project solution (see highlighted below).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-755\" src=\"https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_5.png\" alt=\"\" width=\"800\" height=\"369\" srcset=\"https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_5.png 800w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_5-300x138.png 300w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_5-768x354.png 768w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_5-280x129.png 280w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>Now we have everything to start adding the code to display our ad. For live applications you would need to register to windows dev portal and get Application Id and Ad Unit Id. For testing <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/uwp\/monetize\/set-up-ad-units-in-your-app#test-ad-units\" target=\"_blank\">Microsoft has provided test ids<\/a> which can be used to see how the ads are going to look in our app which we will be using for testing.<\/p>\n<p>Add WinJS references in your html file<\/p>\n<pre class=\"lang:default decode:true\">    &lt;!-- WinJS references --&gt;\r\n    &lt;link href=\"WinJS\/css\/ui-dark.css\" rel=\"stylesheet\" \/&gt;\r\n    &lt;script src=\"WinJS\/js\/base.js\"&gt;&lt;\/script&gt;\r\n    &lt;script src=\"WinJS\/js\/ui.js\"&gt;&lt;\/script&gt;\r\n<\/pre>\n<p>Add Micorsoft Advertising SDK reference in your html file. Make sure this code is loaded after your game code.<\/p>\n<pre class=\"lang:default decode:true\">    &lt;script src=\"\/\/Microsoft.Advertising.JavaScript\/ad.js\"&gt;&lt;\/script&gt;\r\n<\/pre>\n<p>Load following code in your js file.<\/p>\n<pre class=\"lang:default decode:true\">\/\/\/\/ Copyright (c) Microsoft Corporation. All rights reserved\r\n(function () {\r\n    \"use strict\";\r\n    var adCount = 0;\r\n\r\n    var page = WinJS.UI.Pages.define(\"\/index.html\", {\r\n        ready: function (element, options) {\r\n            var adControl = document.getElementById(\"adContainer\").winControl;\r\n            adControl.onErrorOccurred = errorHandler;\r\n            adControl.onAdRefreshed = refreshHandler;\r\n        }\r\n    });\r\n\r\n    \/\/ This is an error event handler for the ad control.\r\n    function errorHandler(adControl, e) {\r\n        WinJS.log &amp;&amp; WinJS.log(\"An error occurred. \" + e.errorCode + \": \" + e.errorMessage, \"samples\", \"error\");\r\n    }\r\n\r\n    \/\/ This is an event handler for the ad control. It is called when the ad is refreshed with a new ad.\r\n    function refreshHandler(adControl) {\r\n        \/\/ We increment the ad count so that the message changes at every refresh.\r\n        adCount++;\r\n        WinJS.log &amp;&amp; WinJS.log(\"Advertisement #\" + adCount, \"samples\", \"status\");\r\n    }\r\n})();\r\n<\/pre>\n<p>Finally add the DIV in which advertisement will be shown.\u00a0Click\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/uwp\/monetize\/supported-ad-sizes-for-banner-ads\" target=\"_blank\">here <\/a>to check for all available ad formats. Select an Ad size and change the following DIV code accordingly for size and place it in your html file. You can play around with style attributes\u00a0to align it on your page per your liking.<\/p>\n<pre class=\"lang:default decode:true\">    &lt;div id=\"adContainer\" style=\"position: static; text-align:center; top: 50px; left: 0px; width: 728px; height: 90px; z-index: 1\"\r\n         data-win-control=\"MicrosoftNSJS.Advertising.AdControl\"\r\n         data-win-options=\"{applicationId: '3f83fe91-d6be-434d-a0ae-7351c5a997f1', adUnitId: 'test'}\"&gt;\r\n    &lt;\/div&gt;\r\n<\/pre>\n<p>Remember we are using test applicationId and adUintId. For live application you will need to replace it with your own ids once you are ready to publish your app in the app store.<\/p>\n<p>Also while debugging make sure you have not selected &#8220;Any CPU&#8221; option. Select specific CPU options from ARM, x86, x64. Also, make sure you have Internet (Client) selected under Capabilities section in package.appxmanifest.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-769\" src=\"https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_7.png\" alt=\"\" width=\"800\" height=\"425\" srcset=\"https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_7.png 800w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_7-300x159.png 300w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_7-768x408.png 768w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_7-280x149.png 280w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>Now hit F5 and you will see nice banner showing up.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-756\" src=\"https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_6.png\" alt=\"\" width=\"800\" height=\"368\" srcset=\"https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_6.png 800w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_6-300x138.png 300w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_6-768x353.png 768w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/12\/WinAd_6-280x129.png 280w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>There are multiple sizes banners are available in and you can choose according to your requirements. There are <a tabindex=\"62\" href=\"https:\/\/docs.microsoft.com\/windows\/uwp\/monetize\/adcontrol-in-xaml-and--net\" target=\"_blank\">Banner ads<\/a>,\u00a0<a tabindex=\"64\" href=\"https:\/\/docs.microsoft.com\/windows\/uwp\/monetize\/interstitial-ads\" target=\"_blank\">Interstitial video and banner ads<\/a>\u00a0and\u00a0<a tabindex=\"66\" href=\"https:\/\/docs.microsoft.com\/windows\/uwp\/monetize\/native-ads\" target=\"_blank\">Native ads<\/a>\u00a0to choose from.<\/p>\n<p>Also, check out the official github repository for samples on how to implement different ad formats in your code.<\/p>\n<p><a href=\"https:\/\/github.com\/Microsoft\/Windows-universal-samples\" target=\"_blank\">https:\/\/github.com\/Microsoft\/Windows-universal-samples<\/a><\/p>\n<p>In\u00a0next article we will be publishing the app to windows app store.<\/p>\n<p>Some troubleshooting steps\u00a0related to advertising libraries<\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/uwp\/monetize\/known-issues-for-the-advertising-libraries\" target=\"_blank\">https:\/\/docs.microsoft.com\/en-us\/windows\/uwp\/monetize\/known-issues-for-the-advertising-libraries<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous article we converted our Phaser game to a windows 10 store app (UWP app). Now\u00a0we all want to monetize our games. One way of monetizing is by putting ads in our game. Micorsoft has built support for ads in the UWP which offers various ad types and also supports\u00a0mediation with other ad networks. Refer to following resources to get more detail https:\/\/developer.microsoft.com\/en-us\/store\/monetize\/ads-in-apps We will go through steps to quickly add a banner ad to our game. First step is to add WinJS and Microssoft Advertising SDK to our visual studio project. Open proejct and go to NuGet Package Manager -&gt; Manage NuGet Packages for Solution Search for WinJS in Browse section, select the package and click install which will install the package in the current solution. Accept the popup confirmations if you get any. Next we will add Microssoft Advertising SDK. Simply\u00a0browse\u00a0for Advertising and in the results[&#8230;]<\/p>\n","protected":false},"author":5,"featured_media":746,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4,21],"tags":[],"class_list":["post-750","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-html5","category-phaser","category-windows-store-app"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/750","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/comments?post=750"}],"version-history":[{"count":4,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/750\/revisions"}],"predecessor-version":[{"id":771,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/750\/revisions\/771"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/media\/746"}],"wp:attachment":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/media?parent=750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/categories?post=750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/tags?post=750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}