@playcraft/adsdk 1.0.6 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -188,53 +188,174 @@ function ensureProtocol() {
188
188
  }
189
189
 
190
190
  // src/tracking.ts
191
+ function trackURL(url) {
192
+ if (url) {
193
+ const trackingPixel = new Image();
194
+ trackingPixel.src = url;
195
+ }
196
+ }
197
+ function trackBigabidEvent(event) {
198
+ console.log(" trackBigabidEvent", event);
199
+ if (window.BIGABID_BIDTIMEMACROS && window.BIGABID_BIDTIMEMACROS[event]) {
200
+ trackURL(window.BIGABID_BIDTIMEMACROS[event]);
201
+ }
202
+ }
203
+ function trackInMobiEvent(event) {
204
+ console.log(" trackInMobiEvent", event);
205
+ if (window.INMOBI_DSPMACROS && window.INMOBI_DSPMACROS[event]) {
206
+ trackURL(window.INMOBI_DSPMACROS[event]);
207
+ }
208
+ }
209
+ function trackApplovinEvent(event) {
210
+ console.log(" trackApplovinEvent", event);
211
+ if (typeof window.ALPlayableAnalytics !== "undefined") {
212
+ window.ALPlayableAnalytics.trackEvent(event);
213
+ }
214
+ }
215
+ var totalInteractions = 0;
216
+ var progressMilestones = [];
217
+ function setTotalInteractions(total) {
218
+ totalInteractions = total;
219
+ }
220
+ function trackChallengeSuccess() {
221
+ if (AD_NETWORK === "applovin") {
222
+ trackApplovinEvent("CHALLENGE_SOLVED");
223
+ }
224
+ if (AD_NETWORK === "bigabid") {
225
+ trackBigabidComplete();
226
+ }
227
+ if (AD_NETWORK === "inmobi") {
228
+ trackInMobiEvent("Gameplay_Complete");
229
+ }
230
+ }
231
+ function trackChallengeFailed() {
232
+ if (AD_NETWORK === "applovin") {
233
+ trackApplovinEvent("CHALLENGE_FAILED");
234
+ }
235
+ }
236
+ function initProgressMilestones() {
237
+ if (totalInteractions <= 0) return;
238
+ if (AD_NETWORK === "applovin") {
239
+ progressMilestones = [
240
+ { percent: 25, triggered: false, callback: () => trackApplovinEvent("CHALLENGE_PASS_25") },
241
+ { percent: 50, triggered: false, callback: () => trackApplovinEvent("CHALLENGE_PASS_50") },
242
+ { percent: 75, triggered: false, callback: () => trackApplovinEvent("CHALLENGE_PASS_75") },
243
+ { percent: 100, triggered: false, callback: () => trackChallengeSuccess() }
244
+ ];
245
+ } else if (AD_NETWORK === "bigabid") {
246
+ progressMilestones = [
247
+ { percent: 100, triggered: false, callback: () => trackChallengeSuccess() }
248
+ ];
249
+ }
250
+ }
251
+ function checkInteractionProgress(currentInteractions) {
252
+ if (totalInteractions <= 0 || progressMilestones.length === 0) return;
253
+ const currentPercent = currentInteractions / totalInteractions * 100;
254
+ for (const milestone of progressMilestones) {
255
+ if (!milestone.triggered && currentPercent >= milestone.percent) {
256
+ milestone.triggered = true;
257
+ milestone.callback();
258
+ }
259
+ }
260
+ }
261
+ var bigabidCompleteTriggered = false;
262
+ function trackBigabidComplete() {
263
+ if (!bigabidCompleteTriggered) {
264
+ trackBigabidEvent("complete");
265
+ bigabidCompleteTriggered = true;
266
+ }
267
+ }
268
+ var inmobiTimers = [];
269
+ function startInMobiDurationTracking() {
270
+ const durations = [5, 10, 15, 20, 25, 30];
271
+ for (const sec of durations) {
272
+ const timer = setTimeout(() => trackInMobiEvent(`Spent_${sec}_Seconds`), sec * 1e3);
273
+ inmobiTimers.push(timer);
274
+ }
275
+ }
276
+ function clearInMobiDurationTracking() {
277
+ for (const timer of inmobiTimers) {
278
+ clearTimeout(timer);
279
+ }
280
+ inmobiTimers = [];
281
+ }
191
282
  function initTrackingProtocols() {
192
- if (AD_NETWORK === "bigabid" || AD_NETWORK === "inmobi") {
193
- let trackURL2 = function(url) {
194
- if (url) {
195
- const trackingPixel = new Image();
196
- trackingPixel.src = url;
197
- }
198
- };
199
- var trackURL = trackURL2;
283
+ const supportedNetworks = ["bigabid", "inmobi", "applovin"];
284
+ if (!supportedNetworks.includes(AD_NETWORK)) {
285
+ return;
286
+ }
287
+ initProgressMilestones();
288
+ sdk.once("init", () => {
200
289
  if (AD_NETWORK === "bigabid") {
201
- let trackBigabidEvent2 = function(event) {
202
- if (window.BIGABID_BIDTIMEMACROS && window.BIGABID_BIDTIMEMACROS[event])
203
- trackURL2(window.BIGABID_BIDTIMEMACROS[event]);
204
- }, trackComplete2 = function() {
205
- if (!completeTriggered) trackBigabidEvent2("complete");
206
- completeTriggered = true;
207
- };
208
- var trackBigabidEvent = trackBigabidEvent2, trackComplete = trackComplete2;
209
- var completeTriggered = false;
210
- sdk.once("boot", () => trackBigabidEvent2("mraid_viewable"));
211
- sdk.once("start", () => trackBigabidEvent2("game_viewable"));
212
- sdk.once("interaction", () => trackBigabidEvent2("engagement"));
213
- sdk.once("finish", trackComplete2);
214
- sdk.on("interaction", (interactions) => {
215
- if (interactions > 3) trackComplete2();
216
- });
217
- sdk.once("install", () => trackBigabidEvent2("click"));
218
- } else if (AD_NETWORK === "inmobi") {
219
- let trackInMobiEvent2 = function(event) {
220
- if (window.INMOBI_DSPMACROS && window.INMOBI_DSPMACROS[event]) trackURL2(window.INMOBI_DSPMACROS[event]);
221
- };
222
- var trackInMobiEvent = trackInMobiEvent2;
223
- sdk.once("boot", () => trackInMobiEvent2("Ad_Load_Start"));
224
- sdk.once("start", () => trackInMobiEvent2("Ad_Viewable"));
225
- sdk.once("interaction", () => trackInMobiEvent2("First_Engagement"));
226
- sdk.once("finish", () => trackInMobiEvent2("Gameplay_Complete"));
227
- sdk.once("install", () => trackInMobiEvent2("DSP_Click"));
228
- sdk.once("start", () => {
229
- setTimeout(() => trackInMobiEvent2("Spent_5_Seconds"), 5e3);
230
- setTimeout(() => trackInMobiEvent2("Spent_10_Seconds"), 1e4);
231
- setTimeout(() => trackInMobiEvent2("Spent_15_Seconds"), 15e3);
232
- setTimeout(() => trackInMobiEvent2("Spent_20_Seconds"), 2e4);
233
- setTimeout(() => trackInMobiEvent2("Spent_25_Seconds"), 25e3);
234
- setTimeout(() => trackInMobiEvent2("Spent_30_Seconds"), 3e4);
235
- });
290
+ trackBigabidEvent("mraid_viewable");
236
291
  }
237
- }
292
+ if (AD_NETWORK === "inmobi") {
293
+ trackInMobiEvent("Ad_Load_Start");
294
+ }
295
+ if (AD_NETWORK === "applovin") {
296
+ trackApplovinEvent("LOADING");
297
+ }
298
+ });
299
+ sdk.once("start", () => {
300
+ if (AD_NETWORK === "bigabid") {
301
+ trackBigabidEvent("game_viewable");
302
+ }
303
+ if (AD_NETWORK === "inmobi") {
304
+ trackInMobiEvent("Ad_Viewable");
305
+ startInMobiDurationTracking();
306
+ }
307
+ if (AD_NETWORK === "applovin") {
308
+ trackApplovinEvent("LOADED");
309
+ }
310
+ });
311
+ sdk.once("interactive", () => {
312
+ if (AD_NETWORK === "applovin") {
313
+ trackApplovinEvent("DISPLAYED");
314
+ }
315
+ });
316
+ sdk.once("interaction", () => {
317
+ if (AD_NETWORK === "bigabid") {
318
+ trackBigabidEvent("engagement");
319
+ }
320
+ if (AD_NETWORK === "inmobi") {
321
+ trackInMobiEvent("First_Engagement");
322
+ }
323
+ if (AD_NETWORK === "applovin") {
324
+ trackApplovinEvent("CHALLENGE_STARTED");
325
+ }
326
+ });
327
+ sdk.on("success", (successCount) => {
328
+ checkInteractionProgress(successCount);
329
+ });
330
+ sdk.once("finish", () => {
331
+ if (AD_NETWORK === "bigabid") {
332
+ trackBigabidComplete();
333
+ }
334
+ if (AD_NETWORK === "inmobi") {
335
+ trackInMobiEvent("Gameplay_Complete");
336
+ clearInMobiDurationTracking();
337
+ }
338
+ if (AD_NETWORK === "applovin") {
339
+ trackApplovinEvent("ENDCARD_SHOWN");
340
+ }
341
+ });
342
+ sdk.on("install", () => {
343
+ if (AD_NETWORK === "bigabid") {
344
+ trackBigabidEvent("click");
345
+ }
346
+ if (AD_NETWORK === "inmobi") {
347
+ trackInMobiEvent("DSP_Click");
348
+ clearInMobiDurationTracking();
349
+ }
350
+ if (AD_NETWORK === "applovin") {
351
+ trackApplovinEvent("CTA_CLICKED");
352
+ }
353
+ });
354
+ sdk.once("retry", () => {
355
+ if (AD_NETWORK === "applovin") {
356
+ trackApplovinEvent("CHALLENGE_RETRY");
357
+ }
358
+ });
238
359
  }
239
360
 
240
361
  // src/assets/images.ts
@@ -513,7 +634,7 @@ var initCallback = () => {
513
634
  };
514
635
  function bootAd() {
515
636
  if (sdk.isReady) return;
516
- emitEvent("boot");
637
+ emitEvent("init");
517
638
  document.body.oncontextmenu = function() {
518
639
  return false;
519
640
  };
@@ -830,7 +951,6 @@ function initSDK() {
830
951
  } else {
831
952
  startDefaultProtocol();
832
953
  }
833
- emitEvent("init");
834
954
  }
835
955
  var _sdk = class _sdk {
836
956
  /**
@@ -838,6 +958,12 @@ var _sdk = class _sdk {
838
958
  * This must be called as earlier as possible.
839
959
  *
840
960
  * @param callback Optional function called when ad container is ready
961
+ * @param options Optional configuration options
962
+ * @param options.totalSuccessCount Total number of successful actions expected for progress tracking.
963
+ * When set, the SDK will track success progress as a percentage and trigger
964
+ * channel-specific milestone events (e.g., AppLovin: CHALLENGE_PASS_25/50/75, CHALLENGE_SOLVED).
965
+ * Different channels monitor different percentage thresholds independently.
966
+ * Set to 0 or omit to disable progress tracking.
841
967
  * @example
842
968
  * // Basic initialization
843
969
  * sdk.init();
@@ -847,13 +973,20 @@ var _sdk = class _sdk {
847
973
  * new App(width, height)
848
974
  * });
849
975
  *
850
- * @fires init When initialization starts
851
- * @fires boot When the ad begins booting
852
- * @fires ready When Ad Network is ready and playable ad can be initialized
976
+ * // Initialization with success count progress tracking
977
+ * sdk.init((width, height) => {
978
+ * new App(width, height)
979
+ * }, { totalSuccessCount: 20 });
980
+ *
981
+ * @fires loading When SDK initialization starts
982
+ * @fires loaded When game instance is created and ready to load resources
853
983
  */
854
- static init(callback) {
984
+ static init(callback, options) {
855
985
  if (isSDKInitialized) return;
856
986
  if (callback) initCallback = callback;
987
+ if ((options == null ? void 0 : options.totalSuccessCount) && options.totalSuccessCount > 0) {
988
+ setTotalInteractions(options.totalSuccessCount);
989
+ }
857
990
  initTrackingProtocols();
858
991
  document.readyState === "loading" ? window.addEventListener("DOMContentLoaded", initSDK) : initSDK();
859
992
  isSDKInitialized = true;
@@ -893,6 +1026,22 @@ var _sdk = class _sdk {
893
1026
  _sdk.resize();
894
1027
  }
895
1028
  }
1029
+ /**
1030
+ * Marks the main scene as fully loaded and ready for user interaction.
1031
+ * Should be called at the end of your main game scene's create() method.
1032
+ *
1033
+ * @example
1034
+ * // In Phaser MainScene.create()
1035
+ * create() {
1036
+ * // ... all initialization code ...
1037
+ * sdk.interactive();
1038
+ * }
1039
+ *
1040
+ * @fires interactive When the main scene is ready for user interaction
1041
+ */
1042
+ static interactive() {
1043
+ emitEvent("interactive");
1044
+ }
896
1045
  /**
897
1046
  * Marks the playable ad as finished.
898
1047
  * This triggers network-specific completion handlers.
@@ -1067,6 +1216,60 @@ var _sdk = class _sdk {
1067
1216
  static resize() {
1068
1217
  handleResize(_sdk.maxWidth, _sdk.maxHeight);
1069
1218
  }
1219
+ /**
1220
+ * Records successful game actions (e.g., successful match, level clear).
1221
+ * This should be called by game logic when a meaningful success occurs.
1222
+ * This count is used for progress tracking (e.g., AppLovin milestone events).
1223
+ *
1224
+ * @param count Optional success count to add (default: 1)
1225
+ *
1226
+ * @example
1227
+ * // Record single success when match completes
1228
+ * onMatchSuccess() {
1229
+ * sdk.recordSuccessCount();
1230
+ * }
1231
+ *
1232
+ * // Record multiple successes at once
1233
+ * onComboMatch(comboCount) {
1234
+ * sdk.recordSuccessCount(comboCount);
1235
+ * }
1236
+ *
1237
+ * @fires success With updated success count (triggers progress tracking)
1238
+ */
1239
+ static recordSuccessCount(count = 1) {
1240
+ _sdk.successCount += count;
1241
+ emitEvent("success", _sdk.successCount);
1242
+ }
1243
+ /**
1244
+ * Records a game success/completion event.
1245
+ * Sends platform-specific success tracking events:
1246
+ * - AppLovin: CHALLENGE_SOLVED
1247
+ * - Bigabid: complete
1248
+ * - InMobi: Gameplay_Complete
1249
+ *
1250
+ * @example
1251
+ * // When player completes the level
1252
+ * onLevelComplete() {
1253
+ * sdk.recordSuccess();
1254
+ * }
1255
+ */
1256
+ static recordSuccess() {
1257
+ trackChallengeSuccess();
1258
+ }
1259
+ /**
1260
+ * Records a game failure event.
1261
+ * Sends platform-specific failure tracking events:
1262
+ * - AppLovin: CHALLENGE_FAILED
1263
+ *
1264
+ * @example
1265
+ * // When player fails the level
1266
+ * onLevelFailed() {
1267
+ * sdk.recordFailed();
1268
+ * }
1269
+ */
1270
+ static recordFailed() {
1271
+ trackChallengeFailed();
1272
+ }
1070
1273
  /**
1071
1274
  * Forces the playable ad into a paused state.
1072
1275
  *
@@ -1160,9 +1363,15 @@ var _sdk = class _sdk {
1160
1363
  static getCurChannel() {
1161
1364
  return AD_NETWORK;
1162
1365
  }
1366
+ /**
1367
+ * 判断是否是 Google
1368
+ */
1369
+ static isGoogle() {
1370
+ return _sdk.getCurChannel() === "google";
1371
+ }
1163
1372
  };
1164
1373
  /** Current version of the SDK */
1165
- _sdk.version = "1.0.24";
1374
+ _sdk.version = "1.0.26";
1166
1375
  /** Current maximum width of the playable ad container in pixels */
1167
1376
  _sdk.maxWidth = Math.floor(window.innerWidth);
1168
1377
  /** Current maximum height of the playable ad container in pixels */
@@ -1179,8 +1388,12 @@ _sdk.isPaused = false;
1179
1388
  _sdk.isFinished = false;
1180
1389
  /** Current volume level (0-1) */
1181
1390
  _sdk.volume = actualVolume;
1182
- /** Number of user interactions with the playable ad */
1391
+ /** Number of user interactions with the playable ad (auto-incremented on each touch/click) */
1183
1392
  _sdk.interactions = 0;
1393
+ /** Number of successful game actions (e.g., successful matches, level clears).
1394
+ * Controlled by calling sdk.recordSuccessCount(), NOT auto-incremented.
1395
+ */
1396
+ _sdk.successCount = 0;
1184
1397
  var sdk = _sdk;
1185
1398
  window["console"].log(
1186
1399
  `%c @tencent/playable-sdk %c v${sdk.version} `,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@playcraft/adsdk",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "统一的 Playable SDK,支持 MRAID、Google、Facebook、Vungle、BigoAds 等多广告渠道",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/esm/index.js",