@traqio/web 0.2.0 → 0.2.2

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/build/index.cjs CHANGED
@@ -362,12 +362,277 @@ var init_empty = __esm({
362
362
  }
363
363
  });
364
364
 
365
+ // ../sdk-core/build/adRevenue.js
366
+ var require_adRevenue = __commonJS({
367
+ "../sdk-core/build/adRevenue.js"(exports) {
368
+ "use strict";
369
+ Object.defineProperty(exports, "__esModule", { value: true });
370
+ exports.CAPACITOR_ADMOB_PAID_EVENTS = exports.MAX_RN_MIN_VERSION = exports.MAX_RN_REVENUE_EVENTS = exports.GMA_MIN_VERSION = exports.GMA_FULLSCREEN_CHANNELS = exports.AdRevenueGate = void 0;
371
+ exports.fromGoogleMobileAdsPaid = fromGoogleMobileAdsPaid;
372
+ exports.fromGoogleMobileAdsChannel = fromGoogleMobileAdsChannel;
373
+ exports.fromGoogleMobileAdsNativeAd = fromGoogleMobileAdsNativeAd;
374
+ exports.fromMaxAdInfo = fromMaxAdInfo;
375
+ exports.fromCapacitorAdMob = fromCapacitorAdMob;
376
+ exports.watchReactNativeAds = watchReactNativeAds;
377
+ function covers(c) {
378
+ return Array.isArray(c) ? c.length > 0 : c === true;
379
+ }
380
+ var AdRevenueGate = class {
381
+ constructor(warn = (m) => console.warn(m)) {
382
+ this.warn = warn;
383
+ this.coverage = {};
384
+ this.warned = /* @__PURE__ */ new Set();
385
+ this.seen = /* @__PURE__ */ new WeakSet();
386
+ }
387
+ arm(coverage) {
388
+ this.coverage = { ...coverage };
389
+ }
390
+ /** Does the automatic capture own this network (any format)? */
391
+ captures(network) {
392
+ return covers(this.coverage[network]);
393
+ }
394
+ /** Does the automatic capture own this format of the network? */
395
+ capturesFormat(network, format) {
396
+ const c = this.coverage[network];
397
+ if (c === true)
398
+ return true;
399
+ return Array.isArray(c) && format !== void 0 && c.includes(format);
400
+ }
401
+ /** A public manual tracker: allowed only for a network nothing captures. */
402
+ manual(network, call) {
403
+ if (!this.captures(network))
404
+ return true;
405
+ this.warnOnce(`manual:${network}`, `[traqio] adRevenue.${network} is on: manual ${call}() ignored \u2014 Traqio already captures this network, remove the call.`);
406
+ return false;
407
+ }
408
+ /** A one-line helper: allowed for the formats the watcher does not capture, once per event. */
409
+ helper(network, format, raw, call) {
410
+ if (this.capturesFormat(network, format)) {
411
+ this.warnOnce(`helper:${network}:${format}`, `[traqio] adRevenue.${network} already captures ${format} ads: ${call} ignored for them, keep it on the formats it is documented for.`);
412
+ return false;
413
+ }
414
+ return this.first(raw);
415
+ }
416
+ /** True the first time this event object is seen (non-objects always pass). */
417
+ first(raw) {
418
+ if (!raw || typeof raw !== "object")
419
+ return true;
420
+ if (this.seen.has(raw))
421
+ return false;
422
+ this.seen.add(raw);
423
+ return true;
424
+ }
425
+ warnOnce(key, message) {
426
+ if (this.warned.has(key))
427
+ return;
428
+ this.warned.add(key);
429
+ try {
430
+ this.warn(message);
431
+ } catch {
432
+ }
433
+ }
434
+ };
435
+ exports.AdRevenueGate = AdRevenueGate;
436
+ var PRECISION_BY_INDEX = ["unknown", "estimated", "publisher_provided", "precise"];
437
+ function precisionOf(raw) {
438
+ var _a;
439
+ return typeof raw === "number" ? (_a = PRECISION_BY_INDEX[raw]) != null ? _a : "unknown" : "unknown";
440
+ }
441
+ function text(raw) {
442
+ return typeof raw === "string" && raw.trim() ? raw : void 0;
443
+ }
444
+ function record(raw) {
445
+ return raw && typeof raw === "object" ? raw : void 0;
446
+ }
447
+ function micros(raw) {
448
+ const n = typeof raw === "string" && /^\d+$/.test(raw.trim()) ? Number(raw) : raw;
449
+ return typeof n === "number" && Number.isSafeInteger(n) && n >= 0 ? n : void 0;
450
+ }
451
+ exports.GMA_FULLSCREEN_CHANNELS = {
452
+ rnapp_google_mobile_ads_app_open_event: "app_open",
453
+ rnapp_google_mobile_ads_interstitial_event: "interstitial",
454
+ rnapp_google_mobile_ads_rewarded_event: "rewarded",
455
+ rnapp_google_mobile_ads_rewarded_interstitial_event: "rewarded_interstitial"
456
+ };
457
+ exports.GMA_MIN_VERSION = "12.4.0";
458
+ function fromGoogleMobileAdsPaid(payload, meta = {}) {
459
+ var _a;
460
+ const p = record(payload);
461
+ if (!p)
462
+ return void 0;
463
+ const currency = text(p.currency);
464
+ if (!currency)
465
+ return void 0;
466
+ let valueMicros = micros(p.valueMicros);
467
+ if (valueMicros === void 0) {
468
+ if (!(typeof p.value === "number" && Number.isFinite(p.value) && p.value >= 0))
469
+ return void 0;
470
+ valueMicros = Math.round(p.value * 1e6);
471
+ }
472
+ const loaded = record((_a = record(p.responseInfo)) == null ? void 0 : _a.loadedAdapterResponse);
473
+ return {
474
+ valueMicros,
475
+ currency,
476
+ precision: precisionOf(p.precision),
477
+ adUnitId: meta.adUnitId,
478
+ adFormat: meta.adFormat,
479
+ adSourceName: text(loaded == null ? void 0 : loaded.adSourceName)
480
+ };
481
+ }
482
+ function fromGoogleMobileAdsChannel(channel, event) {
483
+ const e = record(event);
484
+ const body = record(e == null ? void 0 : e.body);
485
+ if (!e || (body == null ? void 0 : body.type) !== "paid")
486
+ return void 0;
487
+ return fromGoogleMobileAdsPaid(body.data, { adUnitId: text(e.adUnitId), adFormat: exports.GMA_FULLSCREEN_CHANNELS[channel] });
488
+ }
489
+ function fromGoogleMobileAdsNativeAd(event) {
490
+ const e = record(event);
491
+ if ((e == null ? void 0 : e.type) !== "paid")
492
+ return void 0;
493
+ return fromGoogleMobileAdsPaid(e, { adFormat: "native" });
494
+ }
495
+ exports.MAX_RN_REVENUE_EVENTS = [
496
+ "OnBannerAdRevenuePaid",
497
+ "OnMRecAdRevenuePaid",
498
+ "OnInterstitialAdRevenuePaid",
499
+ "OnRewardedAdRevenuePaid",
500
+ "OnAppOpenAdRevenuePaid"
501
+ ];
502
+ exports.MAX_RN_MIN_VERSION = "5.0.0";
503
+ function fromMaxAdInfo(info) {
504
+ const i = record(info);
505
+ if (!i || typeof i.revenue !== "number")
506
+ return void 0;
507
+ return {
508
+ revenue: i.revenue,
509
+ revenuePrecision: text(i.revenuePrecision),
510
+ adUnitId: text(i.adUnitId),
511
+ format: text(i.adFormat),
512
+ networkName: text(i.networkName)
513
+ };
514
+ }
515
+ exports.CAPACITOR_ADMOB_PAID_EVENTS = {
516
+ bannerAdPaid: "banner",
517
+ nativeAdPaid: "native",
518
+ interstitialAdImpression: "interstitial",
519
+ onRewardedVideoAdImpression: "rewarded",
520
+ onRewardedInterstitialAdImpression: "rewarded_interstitial",
521
+ appOpenAdImpression: "app_open"
522
+ };
523
+ function fromCapacitorAdMob(event, adFormat) {
524
+ const e = record(event);
525
+ const valueMicros = micros(e == null ? void 0 : e.valueMicros);
526
+ const currency = text(e == null ? void 0 : e.currencyCode);
527
+ if (!e || valueMicros === void 0 || !currency)
528
+ return void 0;
529
+ return {
530
+ valueMicros,
531
+ currency,
532
+ precision: precisionOf(e.precision),
533
+ adUnitId: text(e.adUnitId),
534
+ adFormat,
535
+ // The mediation ADAPTER class here, not the source's display name — still
536
+ // the only thing telling AdMob's own demand from a mediated network.
537
+ adSourceName: text(e.networkName)
538
+ };
539
+ }
540
+ function nativeModule(rn, name) {
541
+ var _a, _b, _c, _d, _e;
542
+ try {
543
+ return (_d = (_b = (_a = rn.TurboModuleRegistry) == null ? void 0 : _a.get) == null ? void 0 : _b.call(_a, name)) != null ? _d : (_c = rn.NativeModules) == null ? void 0 : _c[name];
544
+ } catch {
545
+ return (_e = rn.NativeModules) == null ? void 0 : _e[name];
546
+ }
547
+ }
548
+ var MAX_RN_FULLSCREEN_FORMATS = ["interstitial", "rewarded", "rewarded_interstitial", "app_open"];
549
+ function watchReactNativeAds(want, sink, rn) {
550
+ var _a, _b, _c, _d;
551
+ if (!rn) {
552
+ try {
553
+ rn = (init_empty(), __toCommonJS(empty_exports));
554
+ } catch {
555
+ return { coverage: {}, stop: () => void 0 };
556
+ }
557
+ }
558
+ const subs = [];
559
+ const coverage = {};
560
+ const guard = (parse, deliver, rawOf = (e) => e) => (e) => {
561
+ try {
562
+ const v = parse(e);
563
+ if (v)
564
+ deliver(v, rawOf(e));
565
+ } catch {
566
+ }
567
+ };
568
+ const paidData = (e) => {
569
+ var _a2, _b2;
570
+ return (_b2 = record((_a2 = record(e)) == null ? void 0 : _a2.body)) == null ? void 0 : _b2.data;
571
+ };
572
+ if (want.admob) {
573
+ coverage.admob = [];
574
+ for (const [channel, format] of Object.entries(exports.GMA_FULLSCREEN_CHANNELS)) {
575
+ const sub = (_a = rn.DeviceEventEmitter) == null ? void 0 : _a.addListener(channel, guard((e) => fromGoogleMobileAdsChannel(channel, e), sink.admob, paidData));
576
+ if (sub) {
577
+ subs.push(sub);
578
+ coverage.admob.push(format);
579
+ }
580
+ }
581
+ const nativeAds = nativeModule(rn, "RNGoogleMobileAdsNativeModule");
582
+ const onNative = guard(fromGoogleMobileAdsNativeAd, sink.admob);
583
+ try {
584
+ if (nativeAds && typeof nativeAds.onAdEvent === "function") {
585
+ subs.push(nativeAds.onAdEvent(onNative));
586
+ coverage.admob.push("native");
587
+ } else if (nativeAds && rn.NativeEventEmitter) {
588
+ const emitter = new rn.NativeEventEmitter(((_b = rn.Platform) == null ? void 0 : _b.OS) === "ios" ? nativeAds : void 0);
589
+ subs.push(emitter.addListener("RNGMANativeAdEvent", onNative));
590
+ coverage.admob.push("native");
591
+ }
592
+ } catch {
593
+ }
594
+ }
595
+ if (want.applovinMax) {
596
+ const max = nativeModule(rn, "AppLovinMAX");
597
+ if (max && rn.NativeEventEmitter) {
598
+ try {
599
+ let names = [...exports.MAX_RN_REVENUE_EVENTS];
600
+ try {
601
+ const c = (_d = (_c = max.getConstants) == null ? void 0 : _c.call(max)) != null ? _d : {};
602
+ const fromModule = Object.keys(c).filter((k) => /^ON_.*_AD_REVENUE_PAID$/.test(k)).map((k) => c[k]).filter((v) => typeof v === "string");
603
+ if (fromModule.length)
604
+ names = [.../* @__PURE__ */ new Set([...fromModule, ...names])];
605
+ } catch {
606
+ }
607
+ const emitter = new rn.NativeEventEmitter(max);
608
+ for (const name of names)
609
+ subs.push(emitter.addListener(name, guard(fromMaxAdInfo, sink.applovinMax)));
610
+ coverage.applovinMax = [...MAX_RN_FULLSCREEN_FORMATS];
611
+ } catch {
612
+ }
613
+ }
614
+ }
615
+ return {
616
+ coverage,
617
+ stop: () => {
618
+ for (const s of subs.splice(0)) {
619
+ try {
620
+ s.remove();
621
+ } catch {
622
+ }
623
+ }
624
+ }
625
+ };
626
+ }
627
+ }
628
+ });
629
+
365
630
  // ../sdk-core/build/index.js
366
631
  var require_build = __commonJS({
367
632
  "../sdk-core/build/index.js"(exports) {
368
633
  "use strict";
369
634
  Object.defineProperty(exports, "__esModule", { value: true });
370
- exports.Traqio = exports.Client = exports.isTrackingUrl = exports.isOpenUrl = exports.reportAdServicesToken = exports.Skan = exports.SDK_VERSION = void 0;
635
+ exports.Traqio = exports.Client = exports.AdRevenueGate = exports.watchReactNativeAds = exports.fromMaxAdInfo = exports.fromGoogleMobileAdsPaid = exports.fromGoogleMobileAdsNativeAd = exports.fromGoogleMobileAdsChannel = exports.fromCapacitorAdMob = exports.MAX_RN_REVENUE_EVENTS = exports.MAX_RN_MIN_VERSION = exports.GMA_MIN_VERSION = exports.GMA_FULLSCREEN_CHANNELS = exports.CAPACITOR_ADMOB_PAID_EVENTS = exports.isTrackingUrl = exports.isOpenUrl = exports.reportAdServicesToken = exports.Skan = exports.SDK_VERSION = void 0;
371
636
  exports.normalizeAdvertisingId = normalizeAdvertisingId;
372
637
  exports.fromMaxAd = fromMaxAd;
373
638
  exports.detectDebug = detectDebug;
@@ -375,7 +640,8 @@ var require_build = __commonJS({
375
640
  var skan_1 = require_skan();
376
641
  var adservices_1 = require_adservices();
377
642
  var deeplink_1 = require_deeplink();
378
- exports.SDK_VERSION = "0.3.0";
643
+ var adRevenue_1 = require_adRevenue();
644
+ exports.SDK_VERSION = "0.5.0";
379
645
  var skan_2 = require_skan();
380
646
  Object.defineProperty(exports, "Skan", { enumerable: true, get: function() {
381
647
  return skan_2.Skan;
@@ -391,6 +657,43 @@ var require_build = __commonJS({
391
657
  Object.defineProperty(exports, "isTrackingUrl", { enumerable: true, get: function() {
392
658
  return deeplink_2.isTrackingUrl;
393
659
  } });
660
+ var adRevenue_2 = require_adRevenue();
661
+ Object.defineProperty(exports, "CAPACITOR_ADMOB_PAID_EVENTS", { enumerable: true, get: function() {
662
+ return adRevenue_2.CAPACITOR_ADMOB_PAID_EVENTS;
663
+ } });
664
+ Object.defineProperty(exports, "GMA_FULLSCREEN_CHANNELS", { enumerable: true, get: function() {
665
+ return adRevenue_2.GMA_FULLSCREEN_CHANNELS;
666
+ } });
667
+ Object.defineProperty(exports, "GMA_MIN_VERSION", { enumerable: true, get: function() {
668
+ return adRevenue_2.GMA_MIN_VERSION;
669
+ } });
670
+ Object.defineProperty(exports, "MAX_RN_MIN_VERSION", { enumerable: true, get: function() {
671
+ return adRevenue_2.MAX_RN_MIN_VERSION;
672
+ } });
673
+ Object.defineProperty(exports, "MAX_RN_REVENUE_EVENTS", { enumerable: true, get: function() {
674
+ return adRevenue_2.MAX_RN_REVENUE_EVENTS;
675
+ } });
676
+ Object.defineProperty(exports, "fromCapacitorAdMob", { enumerable: true, get: function() {
677
+ return adRevenue_2.fromCapacitorAdMob;
678
+ } });
679
+ Object.defineProperty(exports, "fromGoogleMobileAdsChannel", { enumerable: true, get: function() {
680
+ return adRevenue_2.fromGoogleMobileAdsChannel;
681
+ } });
682
+ Object.defineProperty(exports, "fromGoogleMobileAdsNativeAd", { enumerable: true, get: function() {
683
+ return adRevenue_2.fromGoogleMobileAdsNativeAd;
684
+ } });
685
+ Object.defineProperty(exports, "fromGoogleMobileAdsPaid", { enumerable: true, get: function() {
686
+ return adRevenue_2.fromGoogleMobileAdsPaid;
687
+ } });
688
+ Object.defineProperty(exports, "fromMaxAdInfo", { enumerable: true, get: function() {
689
+ return adRevenue_2.fromMaxAdInfo;
690
+ } });
691
+ Object.defineProperty(exports, "watchReactNativeAds", { enumerable: true, get: function() {
692
+ return adRevenue_2.watchReactNativeAds;
693
+ } });
694
+ Object.defineProperty(exports, "AdRevenueGate", { enumerable: true, get: function() {
695
+ return adRevenue_2.AdRevenueGate;
696
+ } });
394
697
  var INSTALL_SENT_KEY = "traq_install_sent";
395
698
  var INSTALL_SENT_DEBUG_KEY = "traq_install_sent_debug";
396
699
  var INSTALL_ATTEMPTS_KEY = "traq_install_attempts";
@@ -488,6 +791,7 @@ var require_build = __commonJS({
488
791
  constructor() {
489
792
  this.queue = [];
490
793
  this.adQueue = [];
794
+ this.adGate = new adRevenue_1.AdRevenueGate();
491
795
  this.adBatchId = null;
492
796
  this.context = { sdkVersion: exports.SDK_VERSION };
493
797
  this.referrer = null;
@@ -502,6 +806,15 @@ var require_build = __commonJS({
502
806
  this.deferred = null;
503
807
  this.consent = null;
504
808
  this.debug = false;
809
+ this.maxRevenuePaid = (adInfo) => {
810
+ try {
811
+ const max = (0, adRevenue_1.fromMaxAdInfo)(adInfo);
812
+ const ad = max && fromMaxAd(max);
813
+ if (ad && this.adGate.helper("applovinMax", ad.adFormat, adInfo, "Traqio.maxRevenuePaid"))
814
+ this.queueAdRevenue(ad, "applovin_max", "auto");
815
+ } catch {
816
+ }
817
+ };
505
818
  }
506
819
  /** Whether this run is in debug mode — see `InitOptions.debug`. Settled by `init()`. */
507
820
  get isDebug() {
@@ -535,9 +848,33 @@ var require_build = __commonJS({
535
848
  await this.initSkan();
536
849
  await this.initAppleAds();
537
850
  this.wireLifecycle();
851
+ this.wireAdRevenue();
538
852
  if (this.queue.length || this.adQueue.length)
539
853
  this.scheduleFlush(0);
540
854
  }
855
+ // `InitOptions.adRevenue`: hand the ad SDKs' own paid events to the trackers.
856
+ wireAdRevenue() {
857
+ var _a, _b;
858
+ const want = this.opts.adRevenue;
859
+ if (!want || this.isWeb || !(want.admob || want.applovinMax))
860
+ return;
861
+ const sink = {
862
+ admob: (ad, raw) => {
863
+ if (this.adGate.first(raw))
864
+ this.queueAdRevenue(ad, "admob", "auto");
865
+ },
866
+ applovinMax: (ad, raw) => {
867
+ const converted = fromMaxAd(ad);
868
+ if (converted && this.adGate.first(raw))
869
+ this.queueAdRevenue(converted, "applovin_max", "auto");
870
+ }
871
+ };
872
+ try {
873
+ const coverage = ((_a = this.opts.native) == null ? void 0 : _a.watchAdRevenue) ? (_b = this.opts.native.watchAdRevenue(want, sink)) != null ? _b : want : (0, adRevenue_1.watchReactNativeAds)(want, sink).coverage;
874
+ this.adGate.arm(coverage);
875
+ } catch {
876
+ }
877
+ }
541
878
  track(name, props) {
542
879
  var _a, _b;
543
880
  if (!this.started || this.erased)
@@ -569,9 +906,14 @@ var require_build = __commonJS({
569
906
  *
570
907
  * Impressions are batched and sent to /ingest/ad-revenue, where they are matched
571
908
  * to this install and become the ad half of ROAS.
909
+ *
910
+ * Ignored (one warning) once `adRevenue: { admob: true }` captures AdMob: the
911
+ * impression is already counted.
572
912
  */
573
913
  trackAdRevenue(ad) {
574
- this.queueAdRevenue(ad, "admob");
914
+ if (!this.adGate.manual("admob", "trackAdRevenue"))
915
+ return;
916
+ this.queueAdRevenue(ad, "admob", "manual");
575
917
  }
576
918
  /**
577
919
  * Report one AppLovin MAX impression's revenue. Call it from MAX's callback:
@@ -583,20 +925,46 @@ var require_build = __commonJS({
583
925
  * }); }
584
926
  *
585
927
  * Same batching, same endpoint and same ROAS numerator as AdMob, filed under
586
- * the `applovin_max` source.
928
+ * the `applovin_max` source. Ignored (one warning) once
929
+ * `adRevenue: { applovinMax: true }` captures MAX.
587
930
  */
588
931
  trackMaxAdRevenue(ad) {
932
+ if (!this.adGate.manual("applovinMax", "trackMaxAdRevenue"))
933
+ return;
589
934
  const converted = fromMaxAd(ad);
590
935
  if (converted)
591
- this.queueAdRevenue(converted, "applovin_max");
936
+ this.queueAdRevenue(converted, "applovin_max", "manual");
937
+ }
938
+ /**
939
+ * The one line for an ad that reports through its own callback — a
940
+ * react-native-google-mobile-ads `<BannerAd>` (the full-screen formats are
941
+ * captured by `adRevenue: { admob: true }`):
942
+ *
943
+ * <BannerAd unitId={AD_UNIT_ID} size={…} onPaid={Traqio.admobPaid(AD_UNIT_ID)} />
944
+ * ad.addAdEventListener(AdEventType.PAID, Traqio.admobPaid(AD_UNIT_ID, "rewarded"))
945
+ *
946
+ * Reads the library's PAID payload (`value` in currency units, or v17's exact
947
+ * `valueMicros`) and reports it as AdMob revenue. With `adRevenue: { admob: true }`
948
+ * it only counts the formats the capture does not reach (banners): on a
949
+ * full-screen or native ad it is ignored, so the impression is never doubled.
950
+ */
951
+ admobPaid(adUnitId, adFormat = "banner") {
952
+ return (event) => {
953
+ try {
954
+ const ad = (0, adRevenue_1.fromGoogleMobileAdsPaid)(event, { adUnitId, adFormat });
955
+ if (ad && this.adGate.helper("admob", adFormat, event, "Traqio.admobPaid()"))
956
+ this.queueAdRevenue(ad, "admob", "auto");
957
+ } catch {
958
+ }
959
+ };
592
960
  }
593
- queueAdRevenue(ad, source) {
961
+ queueAdRevenue(ad, source, origin) {
594
962
  var _a;
595
963
  if (!this.started || this.erased)
596
964
  return;
597
965
  if (!(ad.valueMicros >= 0) || !ad.currency)
598
966
  return;
599
- this.adQueue.push({ ...ad, source, impressionId: uuid(), ts: (_a = ad.ts) != null ? _a : Math.floor(Date.now() / 1e3) });
967
+ this.adQueue.push({ ...ad, source, origin, impressionId: uuid(), ts: (_a = ad.ts) != null ? _a : Math.floor(Date.now() / 1e3) });
600
968
  this.skan.addRevenue(ad.valueMicros / 1e6);
601
969
  if (this.adQueue.length > MAX_AD_QUEUE)
602
970
  this.adQueue.splice(0, this.adQueue.length - MAX_AD_QUEUE);