ADManager.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. export default class ADManager {
  2. private static _ins: ADManager = null;
  3. public static get ins(): ADManager {
  4. return ADManager._ins ??= new ADManager();
  5. }
  6. /* ... */
  7. // view AD steps is [1,2,3,5,6,7], click AD steps is [1,2,3,6,7,5,4]
  8. callbackFunc = {
  9. // Indicates load ad resource from OpenAD platform, false will be returned if there is no resource to be loaded for the publisher slot/zone
  10. onAdResourceLoad: (e) => {
  11. // 'step1', e = true / false
  12. },
  13. // Indicates the interactive ad is opening
  14. onAdOpening: (e) => {
  15. // 'step2', e = true / false
  16. console.log("onAdOpening:", e);
  17. },
  18. // Indicates the interactive ad is opened,
  19. onAdOpened: (e) => {
  20. // 'step3', e = true / false
  21. console.log("onAdOpened:", e);
  22. },
  23. // indicates the interactive ad task is finished, the task is defined by publisher
  24. onAdTaskFinished: (e) => {
  25. // 'step5', e = true / false
  26. console.log("onAdTaskFinished:", e);
  27. },
  28. // Indicates the interactive ad is closing
  29. onAdClosing: (e) => {
  30. // 'step6',e = true / false
  31. console.log("onAdClosing:", e);
  32. },
  33. // Indicates the interactive ad is closed
  34. onAdClosed: (e) => {
  35. // 'step7', e = view / click / close
  36. // close: user manually closed ads. client side can not get any rewards.
  37. if (e === "close") {
  38. // do sth ...
  39. }
  40. // view: viewed Ad completed, not clicked, not manually closed ads; client side needs to get rewards level 1.
  41. if (e === "view") {
  42. // do sth ...
  43. }
  44. // click: clicked Ad completed, include viewed Ad, not manually closed ads; client side needs to get rewards level 2.
  45. // If your app is 'WEB3' or 'WEB', can add codes below; 'LMA' or 'LWA' can not write codes here.
  46. if (e === "click") {
  47. // do sth ...
  48. }
  49. console.log("onAdClosed:", e);
  50. },
  51. // Indicates clicked and jumps
  52. onAdClick: (e) => {
  53. // 'step4', e = true / false
  54. console.log("onAdClick:", e);
  55. },
  56. };
  57. clickReward = async () => {
  58. // maybe this is a ajax, maybe a ws, or somthing. async mode.
  59. // let res = await getRewardsLevel2Method;
  60. console.log("点击广告的clickReward");
  61. if (this.getSuccess) {
  62. this.getSuccess.call(null);
  63. }
  64. };
  65. getSuccess: Function;
  66. // 展示Google广告
  67. public showGoogleAD(callBack: Function) {
  68. this.getSuccess = callBack;
  69. console.log("showGoogleAD:", callBack);
  70. //@ts-ignore
  71. if (!window.googletag) {
  72. console.log("googletag not init");
  73. return;
  74. }
  75. //@ts-ignore
  76. const googletag = window.googletag || { cmd: [] };
  77. var that = this;
  78. googletag.cmd.push(() => {
  79. console.log(
  80. "googletag.enums.OutOfPageFormat.REWARDED:",
  81. googletag.enums.OutOfPageFormat.REWARDED
  82. );
  83. const rewardedSlot = googletag
  84. .defineOutOfPageSlot(
  85. "/23296944437/rewarded",
  86. googletag.enums.OutOfPageFormat.REWARDED
  87. )
  88. .addService(googletag.pubads());
  89. console.log("rewardedSlot:", rewardedSlot);
  90. googletag.enableServices();
  91. googletag.pubads().addEventListener("rewardedSlotReady", function (evt) {
  92. // if (confirm("want to see a rewarded ad?")) {
  93. var result = evt.makeRewardedVisible();
  94. console.log("makeRewardedVisible return:", result);
  95. });
  96. googletag
  97. .pubads()
  98. .addEventListener("rewardedSlotGranted", function (evt) {
  99. console.error("granted123! " + JSON.stringify(evt.payload));
  100. // {type:"",amount:0}
  101. if (that.getSuccess) {
  102. that.getSuccess.call(evt.payload);
  103. }
  104. });
  105. googletag.pubads().addEventListener("rewardedSlotClosed", function (evt) {
  106. console.error("Closed by the user!");
  107. googletag.destroySlots([rewardedSlot]);
  108. });
  109. googletag.display(rewardedSlot);
  110. // googletag.display("div-gpt-ad-1747208248493-0");
  111. });
  112. }
  113. // 展示Line广告
  114. //@ts-ignore
  115. public showInterLineAD(callBack: Function) {
  116. this.getSuccess = callBack;
  117. console.log("showInterLineAD:", callBack);
  118. /* ... */
  119. const LineAD = {
  120. adInfo: {
  121. zoneId: 430, // int, This is an example zoneId, please get your own code parameters
  122. publisherId: 15, // int, This is an example publisherId, please get your own code parameters
  123. },
  124. adParams: {
  125. line: {
  126. type: "LMA", // LMA, LWA
  127. liffId: "2007001592-ozOgMXpb", // when type = LMA / LWA, liffId is required
  128. //@ts-ignore
  129. prototype: window.liff, // when type = LMA / LWA, prototype is required. line.prototype must contain two methods: getProfile and openWindow, whether you are using liff SDK js or npm installation package;
  130. },
  131. wallet: {
  132. // If you have a web3 wallet components, Optional
  133. type: "eth", // eth: eth wallet, kaia: line wallet, ton: ton wallet;
  134. provider: null, // here is a provider object after wallet initialization.
  135. components: "", // web3 wallet components name
  136. },
  137. },
  138. };
  139. /* ... */
  140. //@ts-ignore
  141. window.OpenADLineJsSDK.interactive.init({ ...LineAD }).then((res) => {
  142. console.log("window.OpenADLineJsSDK.interactive:", res);
  143. if (res.code === 0) {
  144. // you can callback 'getRender' function, user can load an interactive ad;
  145. //@ts-ignore
  146. window.OpenADLineJsSDK.interactive.getRender({
  147. adInfo: LineAD.adInfo,
  148. cb: this.callbackFunc,
  149. clickReward: this.clickReward,
  150. });
  151. } else {
  152. console.error("no ad!");
  153. // you can't callback 'getRender' function, user can't load an interactive ad;
  154. }
  155. });
  156. }
  157. /* ... */
  158. }