Sample SDK configuration
Below you will find code samples for setting up the Yandex Games SDK synchronously and asynchronously.
Example specifics:
- Callback functions are not set for the first ad call.
- Every callback function possible is specified for the second call and each subsequent call.
- A
'click'
event handler is assigned to the Show ads button (an ad is called on each button click).
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<title>Page example with SDK configured synchronously</title>
<script src="/sdk.js"></script>
<script>
YaGames.init().then(ysdk => {
ysdk.adv.showFullscreenAdv();
const buttonElem = document.querySelector('#button');
let commonCounter = 0;
buttonElem.addEventListener('click', () => {
let counter = 0;
function getCallback(callbackName) {
return () => {
counter += 1;
commonCounter += 1;
console.log(`showFullscreenAdv; callback ${callbackName}; ${counter} call`);
}
}
ysdk.adv.showFullscreenAdv({
callbacks: {
onClose: getCallback('onClose'),
onOpen: getCallback('onOpen'),
onError: getCallback('onError')
}
});
});
});
</script>
</head>
<body>
<button id="button">Show ads</button>
</body>
</html>
Example specifics:
-
An onClose callback function is set for the first ad call.
-
Every callback function possible is specified for the second call and each subsequent call.
-
The
onClose
callback function includes code that will be run after the ad unit is closed. -
All errors that occur during the SDK operation or during the execution of callback functions are passed to the onError function.
-
A
'click'
event handler is assigned to the Show ads button (an ad is called on each button click).
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<title>Example of page with SDK configured asynchronously</title>
<script>
let ysdk;
function initSDK() {
YaGames
.init()
.then(ysdk_ => {
ysdk = ysdk_;
ysdk.adv.showFullscreenAdv({
callbacks: {
onClose: wasShown => {
console.info('First close')
}
}
});
})
}
document.addEventListener('DOMContentLoaded', () => {
const buttonElem = document.querySelector('#button');
let commonCounter = 0;
buttonElem.addEventListener('click', () => {
let counter = 0;
function getCallback(callbackName) {
return () => {
counter += 1;
commonCounter += 1;
if (commonCounter % 3 === 0) {
throw new Error(`Test error in ${callbackName}, everything okey, it should not abort other code execution`);
}
console.info(`showFullscreenAdv; callback ${callbackName}; ${counter} call`);
}
}
function makeSomethingImportant() {
console.info('It\'s very important \'console.info\'');
}
if (ysdk) {
ysdk.adv.showFullscreenAdv({
callbacks: {
onClose: makeSomethingImportant,
onOpen: getCallback('onOpen'),
onError: function(error) {
console.error(error);
}
}
});
} else {
makeSomethingImportant();
}
});
});
</script>
</head>
<body>
<!-- Yandex Games SDK -->
<script>
(function(d) {
var t = d.getElementsByTagName('script')[0];
var s = d.createElement('script');
s.src = '/sdk.js';
s.async = true;
t.parentNode.insertBefore(s, t);
s.onload = initSDK;
})(document);
</script>
<button id="button">Show ads</button>
</body>
</html>
onClose
: Called when the ad is closed, on error, or if the ad failed to open due to too frequent calls. It's used with thewasShown
argument (boolean
type), the value of which indicates whether the ad was shown or not.onOpen
: Called when the ad is opened successfully.onError
: Called when an error occurrs. The error object is passed to the callback function.
onClose
: Called when the ad is closed, on error, or if the ad failed to open due to too frequent calls. It's used with the wasShown
argument (boolean
type), which indicates whether the ad was shown or not.
onError
: Called when an error occurs. The error object is passed to the callback function.
onOpen
: Called when the ad is opened successfully.