Sample SDK configuration

Below you will find code samples for setting up the Yandex Games SDK synchronously and asynchronously.

Example specifics:

  1. Callback functions are not set for the first ad call.
  2. Every callback function possible is specified for the second call and each subsequent call.
  3. 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'),
                         onOffline: getCallback('onOffline')
                     }
                 });
             });
         });
     </script>
 </head>
 <body>
     <button id="button">Show ads</button>
 </body>
 </html>

Example specifics:

  1. An onClose callback function is set for the first ad call.

  2. Every callback function possible is specified for the second call and each subsequent call.

  3. The onClose callback function includes code that will be run after the ad unit is closed.

  4. All errors that occur during the SDK operation or during the execution of callback functions are passed to the onError function.

  5. 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);
                             },
                             onOffline: getCallback('onOffline')
                         }
                     });
                 } 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>