예시

다음은 동기식 또는 비동기식 메서드를 사용하여 Yandex Games SDK를 구성하는 방법의 예입니다.

예시 세부 사항:

  1. 첫 번째 광고 호출에는 콜백 함수가 설정되어 있지 않습니다.
  2. 두 번째 호출과 그 이후의 모든 호출에 대해 가능한 모든 콜백이 지정됩니다.
  3. 광고 표시 버튼에는 '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>SDK가 동기식으로 구성된 페이지 예시</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">광고 표시</button>
 </body>
 </html>

예시 세부 사항:

  1. 첫 번째 광고 호출에 대해 onClose 콜백 함수가 설정됩니다.

  2. 두 번째 호출과 이후의 각 호출에 대해 가능한 모든 콜백 함수가 지정됩니다.

  3. 광고 블록이 닫힌 후 실행되는 코드는 onClose 콜백 함수에 추가됩니다.

  4. SDK 또는 콜백 함수로 인해 발생한 모든 오류는 onError 함수에 전달됩니다.

  5. 광고 표시 버튼에는 '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>SDK가 비동기적으로 구성된 페이지 예시</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">광고 표시</button>
 </body>
 </html>
  • onClose: 광고가 닫히거나, 오류가 발생하거나, 너무 자주 호출되어 광고가 열리지 않을 때 호출됩니다. 광고가 표시되었는지 여부를 나타내는 값인 wasShown 인자(부울 유형)와 함께 사용됩니다.
  • onOpen: 광고가 성공적으로 열렸을 때 호출됩니다.
  • onError: 오류가 발생하면 호출됩니다. 오류 객체는 콜백 함수에 전달됩니다.

onClose: 광고가 닫히거나, 오류가 발생하거나, 너무 자주 호출되어 광고가 열리지 않을 때 호출됩니다. 광고가 표시되었는지 여부를 나타내는 값인 wasShown 인자(부울 유형)와 함께 사용됩니다.

onError: 오류가 발생하면 호출됩니다. 오류 객체는 콜백 함수에 전달됩니다.

onOpen: 광고가 성공적으로 열렸을 때 호출됩니다.