ADManager.ts 5.8 KB

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