Handle ad errors
Warning
As of June 1, 2020, the Microsoft Ad Monetization platform for Windows UWP apps will be shut down. Learn more
The AdControl, InterstitialAd, and NativeAdsManagerV2 classes each have an ErrorOccurred event that is raised if an ad-related error occurs. Your app code can handle this event and examine the ErrorCode and ErrorMessage properties of the event args object to help determine the cause of the error.
XAML apps
To handle ad-related errors in a XAML app:
Assign the ErrorOccurred event of your AdControl, InterstitialAd, or NativeAdsManagerV2 object to the name of an event handler delegate.
Code the error event handling delegate so that it takes two parameters: an Object for the sender and an AdErrorEventArgs object.
Here is an example that assigns a delegate named OnAdError to the ErrorOccurred event of an AdControl object named myBannerAdControl.
myBannerAdControl.ErrorOccurred = OnAdError;
Here is an example definition of the OnAdError delegate that writes error information to the output window in Visual Studio.
private void OnAdError(object sender, AdErrorEventArgs e)
{
System.Diagnostics.Debug.WriteLine("AdControl error (" + ((AdControl)sender).Name + "): " + e.Error +
" ErrorCode: " + e.ErrorCode.ToString());
}
See Error handling in XAML/C# walkthrough for a walkthrough that demonstrates AdControl error handling in XAML and C#.
JavaScript/HTML apps
To handle ErrorOccur errors in a JavaScript app:
Assign the onErrorOccurred event to an event handler.
Code the event handler.
Here is an example that assigns an event handler named errorLogger to the ErrorOccurred event of an AdControl object.
<div id="myAd" style="position: absolute; top: 53px; left: 0px; width: 250px; height: 250px; z-index: 1"
data-win-control="MicrosoftNSJS.Advertising.AdControl"
data-win-options="{applicationId: '3f83fe91-d6be-434d-a0ae-7351c5a997f1', adUnitId: 'test', onErrorOccurred: errorLogger}">
</div>
The error handling function is declarative and must be enclosed in the markSupportedForProcessing function.
The error handler catches the JavaScript error object when an error occurs. The error object provides two arguments to the error handler.
Here is an example of an error handling function named errorLogger that handles the onErrorOccurred event.
WinJS.Utilities.markSupportedForProcessing(
window.errorLogger = function (sender, evt) {
console.log(new Date()).toLocaleTimeString() + ": " + sender.element.id + " error: " + evt.errorMessage +
" error code: " + evt.errorCode + \n");
});
See Error Handling in JavaScript walkthrough for a walkthrough that demonstrates AdControl error handling in JavaScript.