@traqio/web 0.2.1 → 0.3.0
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/README.md +26 -2
- package/build/index.cjs +304 -67
- package/build/index.cjs.map +4 -4
- package/build/index.d.ts +13 -1
- package/build/index.js +304 -67
- package/build/index.js.map +4 -4
- package/build/kept.d.ts +29 -0
- package/build/source.d.ts +70 -0
- package/build/test/fakeBrowser.d.ts +1 -0
- package/build/trackId.d.ts +1 -4
- package/build/traqio.min.js +1 -1
- package/build/traqio.min.js.map +4 -4
- package/package.json +2 -2
package/build/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { type WebSource } from "./source";
|
|
2
|
+
export type { WebSource } from "./source";
|
|
1
3
|
export declare const DEFAULT_ENDPOINT = "https://api.traqio.app";
|
|
2
4
|
export interface WebInitOptions {
|
|
3
5
|
/** The site's WEB key (`tq_web_…`), from the console. Public by design: it is held to the site's origins. */
|
|
@@ -18,7 +20,8 @@ export interface WebInitOptions {
|
|
|
18
20
|
*/
|
|
19
21
|
waitForConsent?: boolean;
|
|
20
22
|
/**
|
|
21
|
-
* How long the trackID
|
|
23
|
+
* How long the trackID — and the visit's source (`getSource()`) — is kept after
|
|
24
|
+
* the landing, in days. Default 30. Keeping it
|
|
22
25
|
* longer than the site's attribution window is harmless: the server refuses a
|
|
23
26
|
* click outside the window.
|
|
24
27
|
*/
|
|
@@ -48,6 +51,7 @@ export declare class TraqioWeb {
|
|
|
48
51
|
private readonly core;
|
|
49
52
|
private store;
|
|
50
53
|
private trackIds;
|
|
54
|
+
private sources;
|
|
51
55
|
private waitForConsent;
|
|
52
56
|
private started;
|
|
53
57
|
/** A `setConsent()` made before `init()` — it decides whether init may store. */
|
|
@@ -78,6 +82,14 @@ export declare class TraqioWeb {
|
|
|
78
82
|
* your backend cannot read the `traq_click` cookie itself.
|
|
79
83
|
*/
|
|
80
84
|
getTrackId(): string | null;
|
|
85
|
+
/**
|
|
86
|
+
* Where the visitor came from when no Traqio link brought them — the landing's
|
|
87
|
+
* `utm_*`, an ad network's click id (`gclid`, `fbclid`, `ttclid`), the referring
|
|
88
|
+
* site — or null. Kept in the `traq_src` cookie your backend reads with the
|
|
89
|
+
* sign-up (`@traqio/node`: `sourceFromRequest(req)`); pass this to it instead
|
|
90
|
+
* when your backend is on another domain. A Traqio link click wins over it.
|
|
91
|
+
*/
|
|
92
|
+
getSource(): WebSource | null;
|
|
81
93
|
/**
|
|
82
94
|
* GDPR erasure from the page: the server deletes what it holds about this browser
|
|
83
95
|
* and user, and this SDK stops sending and forgets the trackID. Irreversible.
|
package/build/index.js
CHANGED
|
@@ -362,13 +362,72 @@ var require_adRevenue = __commonJS({
|
|
|
362
362
|
"../sdk-core/build/adRevenue.js"(exports) {
|
|
363
363
|
"use strict";
|
|
364
364
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
365
|
-
exports.CAPACITOR_ADMOB_PAID_EVENTS = exports.MAX_RN_MIN_VERSION = exports.MAX_RN_REVENUE_EVENTS = exports.GMA_MIN_VERSION = exports.GMA_FULLSCREEN_CHANNELS = void 0;
|
|
365
|
+
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;
|
|
366
366
|
exports.fromGoogleMobileAdsPaid = fromGoogleMobileAdsPaid;
|
|
367
367
|
exports.fromGoogleMobileAdsChannel = fromGoogleMobileAdsChannel;
|
|
368
368
|
exports.fromGoogleMobileAdsNativeAd = fromGoogleMobileAdsNativeAd;
|
|
369
369
|
exports.fromMaxAdInfo = fromMaxAdInfo;
|
|
370
370
|
exports.fromCapacitorAdMob = fromCapacitorAdMob;
|
|
371
371
|
exports.watchReactNativeAds = watchReactNativeAds;
|
|
372
|
+
function covers(c) {
|
|
373
|
+
return Array.isArray(c) ? c.length > 0 : c === true;
|
|
374
|
+
}
|
|
375
|
+
var AdRevenueGate = class {
|
|
376
|
+
constructor(warn = (m) => console.warn(m)) {
|
|
377
|
+
this.warn = warn;
|
|
378
|
+
this.coverage = {};
|
|
379
|
+
this.warned = /* @__PURE__ */ new Set();
|
|
380
|
+
this.seen = /* @__PURE__ */ new WeakSet();
|
|
381
|
+
}
|
|
382
|
+
arm(coverage) {
|
|
383
|
+
this.coverage = { ...coverage };
|
|
384
|
+
}
|
|
385
|
+
/** Does the automatic capture own this network (any format)? */
|
|
386
|
+
captures(network) {
|
|
387
|
+
return covers(this.coverage[network]);
|
|
388
|
+
}
|
|
389
|
+
/** Does the automatic capture own this format of the network? */
|
|
390
|
+
capturesFormat(network, format) {
|
|
391
|
+
const c = this.coverage[network];
|
|
392
|
+
if (c === true)
|
|
393
|
+
return true;
|
|
394
|
+
return Array.isArray(c) && format !== void 0 && c.includes(format);
|
|
395
|
+
}
|
|
396
|
+
/** A public manual tracker: allowed only for a network nothing captures. */
|
|
397
|
+
manual(network, call) {
|
|
398
|
+
if (!this.captures(network))
|
|
399
|
+
return true;
|
|
400
|
+
this.warnOnce(`manual:${network}`, `[traqio] adRevenue.${network} is on: manual ${call}() ignored \u2014 Traqio already captures this network, remove the call.`);
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
403
|
+
/** A one-line helper: allowed for the formats the watcher does not capture, once per event. */
|
|
404
|
+
helper(network, format, raw, call) {
|
|
405
|
+
if (this.capturesFormat(network, format)) {
|
|
406
|
+
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.`);
|
|
407
|
+
return false;
|
|
408
|
+
}
|
|
409
|
+
return this.first(raw);
|
|
410
|
+
}
|
|
411
|
+
/** True the first time this event object is seen (non-objects always pass). */
|
|
412
|
+
first(raw) {
|
|
413
|
+
if (!raw || typeof raw !== "object")
|
|
414
|
+
return true;
|
|
415
|
+
if (this.seen.has(raw))
|
|
416
|
+
return false;
|
|
417
|
+
this.seen.add(raw);
|
|
418
|
+
return true;
|
|
419
|
+
}
|
|
420
|
+
warnOnce(key, message) {
|
|
421
|
+
if (this.warned.has(key))
|
|
422
|
+
return;
|
|
423
|
+
this.warned.add(key);
|
|
424
|
+
try {
|
|
425
|
+
this.warn(message);
|
|
426
|
+
} catch {
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
exports.AdRevenueGate = AdRevenueGate;
|
|
372
431
|
var PRECISION_BY_INDEX = ["unknown", "estimated", "publisher_provided", "precise"];
|
|
373
432
|
function precisionOf(raw) {
|
|
374
433
|
var _a;
|
|
@@ -481,38 +540,49 @@ var require_adRevenue = __commonJS({
|
|
|
481
540
|
return (_e = rn.NativeModules) == null ? void 0 : _e[name];
|
|
482
541
|
}
|
|
483
542
|
}
|
|
543
|
+
var MAX_RN_FULLSCREEN_FORMATS = ["interstitial", "rewarded", "rewarded_interstitial", "app_open"];
|
|
484
544
|
function watchReactNativeAds(want, sink, rn) {
|
|
485
545
|
var _a, _b, _c, _d;
|
|
486
546
|
if (!rn) {
|
|
487
547
|
try {
|
|
488
548
|
rn = (init_empty(), __toCommonJS(empty_exports));
|
|
489
549
|
} catch {
|
|
490
|
-
return () => void 0;
|
|
550
|
+
return { coverage: {}, stop: () => void 0 };
|
|
491
551
|
}
|
|
492
552
|
}
|
|
493
553
|
const subs = [];
|
|
494
|
-
const
|
|
554
|
+
const coverage = {};
|
|
555
|
+
const guard = (parse, deliver, rawOf = (e) => e) => (e) => {
|
|
495
556
|
try {
|
|
496
557
|
const v = parse(e);
|
|
497
558
|
if (v)
|
|
498
|
-
deliver(v);
|
|
559
|
+
deliver(v, rawOf(e));
|
|
499
560
|
} catch {
|
|
500
561
|
}
|
|
501
562
|
};
|
|
563
|
+
const paidData = (e) => {
|
|
564
|
+
var _a2, _b2;
|
|
565
|
+
return (_b2 = record((_a2 = record(e)) == null ? void 0 : _a2.body)) == null ? void 0 : _b2.data;
|
|
566
|
+
};
|
|
502
567
|
if (want.admob) {
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
568
|
+
coverage.admob = [];
|
|
569
|
+
for (const [channel, format] of Object.entries(exports.GMA_FULLSCREEN_CHANNELS)) {
|
|
570
|
+
const sub = (_a = rn.DeviceEventEmitter) == null ? void 0 : _a.addListener(channel, guard((e) => fromGoogleMobileAdsChannel(channel, e), sink.admob, paidData));
|
|
571
|
+
if (sub) {
|
|
506
572
|
subs.push(sub);
|
|
573
|
+
coverage.admob.push(format);
|
|
574
|
+
}
|
|
507
575
|
}
|
|
508
576
|
const nativeAds = nativeModule(rn, "RNGoogleMobileAdsNativeModule");
|
|
509
577
|
const onNative = guard(fromGoogleMobileAdsNativeAd, sink.admob);
|
|
510
578
|
try {
|
|
511
579
|
if (nativeAds && typeof nativeAds.onAdEvent === "function") {
|
|
512
580
|
subs.push(nativeAds.onAdEvent(onNative));
|
|
581
|
+
coverage.admob.push("native");
|
|
513
582
|
} else if (nativeAds && rn.NativeEventEmitter) {
|
|
514
583
|
const emitter = new rn.NativeEventEmitter(((_b = rn.Platform) == null ? void 0 : _b.OS) === "ios" ? nativeAds : void 0);
|
|
515
584
|
subs.push(emitter.addListener("RNGMANativeAdEvent", onNative));
|
|
585
|
+
coverage.admob.push("native");
|
|
516
586
|
}
|
|
517
587
|
} catch {
|
|
518
588
|
}
|
|
@@ -532,15 +602,19 @@ var require_adRevenue = __commonJS({
|
|
|
532
602
|
const emitter = new rn.NativeEventEmitter(max);
|
|
533
603
|
for (const name of names)
|
|
534
604
|
subs.push(emitter.addListener(name, guard(fromMaxAdInfo, sink.applovinMax)));
|
|
605
|
+
coverage.applovinMax = [...MAX_RN_FULLSCREEN_FORMATS];
|
|
535
606
|
} catch {
|
|
536
607
|
}
|
|
537
608
|
}
|
|
538
609
|
}
|
|
539
|
-
return
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
610
|
+
return {
|
|
611
|
+
coverage,
|
|
612
|
+
stop: () => {
|
|
613
|
+
for (const s of subs.splice(0)) {
|
|
614
|
+
try {
|
|
615
|
+
s.remove();
|
|
616
|
+
} catch {
|
|
617
|
+
}
|
|
544
618
|
}
|
|
545
619
|
}
|
|
546
620
|
};
|
|
@@ -553,7 +627,7 @@ var require_build = __commonJS({
|
|
|
553
627
|
"../sdk-core/build/index.js"(exports) {
|
|
554
628
|
"use strict";
|
|
555
629
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
556
|
-
exports.Traqio = exports.Client = 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;
|
|
630
|
+
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;
|
|
557
631
|
exports.normalizeAdvertisingId = normalizeAdvertisingId;
|
|
558
632
|
exports.fromMaxAd = fromMaxAd;
|
|
559
633
|
exports.detectDebug = detectDebug;
|
|
@@ -562,7 +636,7 @@ var require_build = __commonJS({
|
|
|
562
636
|
var adservices_1 = require_adservices();
|
|
563
637
|
var deeplink_1 = require_deeplink();
|
|
564
638
|
var adRevenue_1 = require_adRevenue();
|
|
565
|
-
exports.SDK_VERSION = "0.
|
|
639
|
+
exports.SDK_VERSION = "0.5.0";
|
|
566
640
|
var skan_2 = require_skan();
|
|
567
641
|
Object.defineProperty(exports, "Skan", { enumerable: true, get: function() {
|
|
568
642
|
return skan_2.Skan;
|
|
@@ -612,6 +686,9 @@ var require_build = __commonJS({
|
|
|
612
686
|
Object.defineProperty(exports, "watchReactNativeAds", { enumerable: true, get: function() {
|
|
613
687
|
return adRevenue_2.watchReactNativeAds;
|
|
614
688
|
} });
|
|
689
|
+
Object.defineProperty(exports, "AdRevenueGate", { enumerable: true, get: function() {
|
|
690
|
+
return adRevenue_2.AdRevenueGate;
|
|
691
|
+
} });
|
|
615
692
|
var INSTALL_SENT_KEY = "traq_install_sent";
|
|
616
693
|
var INSTALL_SENT_DEBUG_KEY = "traq_install_sent_debug";
|
|
617
694
|
var INSTALL_ATTEMPTS_KEY = "traq_install_attempts";
|
|
@@ -709,6 +786,7 @@ var require_build = __commonJS({
|
|
|
709
786
|
constructor() {
|
|
710
787
|
this.queue = [];
|
|
711
788
|
this.adQueue = [];
|
|
789
|
+
this.adGate = new adRevenue_1.AdRevenueGate();
|
|
712
790
|
this.adBatchId = null;
|
|
713
791
|
this.context = { sdkVersion: exports.SDK_VERSION };
|
|
714
792
|
this.referrer = null;
|
|
@@ -725,9 +803,10 @@ var require_build = __commonJS({
|
|
|
725
803
|
this.debug = false;
|
|
726
804
|
this.maxRevenuePaid = (adInfo) => {
|
|
727
805
|
try {
|
|
728
|
-
const
|
|
729
|
-
|
|
730
|
-
|
|
806
|
+
const max = (0, adRevenue_1.fromMaxAdInfo)(adInfo);
|
|
807
|
+
const ad = max && fromMaxAd(max);
|
|
808
|
+
if (ad && this.adGate.helper("applovinMax", ad.adFormat, adInfo, "Traqio.maxRevenuePaid"))
|
|
809
|
+
this.queueAdRevenue(ad, "applovin_max", "auto");
|
|
731
810
|
} catch {
|
|
732
811
|
}
|
|
733
812
|
};
|
|
@@ -770,19 +849,24 @@ var require_build = __commonJS({
|
|
|
770
849
|
}
|
|
771
850
|
// `InitOptions.adRevenue`: hand the ad SDKs' own paid events to the trackers.
|
|
772
851
|
wireAdRevenue() {
|
|
773
|
-
var _a;
|
|
852
|
+
var _a, _b;
|
|
774
853
|
const want = this.opts.adRevenue;
|
|
775
854
|
if (!want || this.isWeb || !(want.admob || want.applovinMax))
|
|
776
855
|
return;
|
|
777
856
|
const sink = {
|
|
778
|
-
admob: (ad) =>
|
|
779
|
-
|
|
857
|
+
admob: (ad, raw) => {
|
|
858
|
+
if (this.adGate.first(raw))
|
|
859
|
+
this.queueAdRevenue(ad, "admob", "auto");
|
|
860
|
+
},
|
|
861
|
+
applovinMax: (ad, raw) => {
|
|
862
|
+
const converted = fromMaxAd(ad);
|
|
863
|
+
if (converted && this.adGate.first(raw))
|
|
864
|
+
this.queueAdRevenue(converted, "applovin_max", "auto");
|
|
865
|
+
}
|
|
780
866
|
};
|
|
781
867
|
try {
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
else
|
|
785
|
-
(0, adRevenue_1.watchReactNativeAds)(want, sink);
|
|
868
|
+
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;
|
|
869
|
+
this.adGate.arm(coverage);
|
|
786
870
|
} catch {
|
|
787
871
|
}
|
|
788
872
|
}
|
|
@@ -817,9 +901,14 @@ var require_build = __commonJS({
|
|
|
817
901
|
*
|
|
818
902
|
* Impressions are batched and sent to /ingest/ad-revenue, where they are matched
|
|
819
903
|
* to this install and become the ad half of ROAS.
|
|
904
|
+
*
|
|
905
|
+
* Ignored (one warning) once `adRevenue: { admob: true }` captures AdMob: the
|
|
906
|
+
* impression is already counted.
|
|
820
907
|
*/
|
|
821
908
|
trackAdRevenue(ad) {
|
|
822
|
-
this.
|
|
909
|
+
if (!this.adGate.manual("admob", "trackAdRevenue"))
|
|
910
|
+
return;
|
|
911
|
+
this.queueAdRevenue(ad, "admob", "manual");
|
|
823
912
|
}
|
|
824
913
|
/**
|
|
825
914
|
* Report one AppLovin MAX impression's revenue. Call it from MAX's callback:
|
|
@@ -831,12 +920,15 @@ var require_build = __commonJS({
|
|
|
831
920
|
* }); }
|
|
832
921
|
*
|
|
833
922
|
* Same batching, same endpoint and same ROAS numerator as AdMob, filed under
|
|
834
|
-
* the `applovin_max` source.
|
|
923
|
+
* the `applovin_max` source. Ignored (one warning) once
|
|
924
|
+
* `adRevenue: { applovinMax: true }` captures MAX.
|
|
835
925
|
*/
|
|
836
926
|
trackMaxAdRevenue(ad) {
|
|
927
|
+
if (!this.adGate.manual("applovinMax", "trackMaxAdRevenue"))
|
|
928
|
+
return;
|
|
837
929
|
const converted = fromMaxAd(ad);
|
|
838
930
|
if (converted)
|
|
839
|
-
this.queueAdRevenue(converted, "applovin_max");
|
|
931
|
+
this.queueAdRevenue(converted, "applovin_max", "manual");
|
|
840
932
|
}
|
|
841
933
|
/**
|
|
842
934
|
* The one line for an ad that reports through its own callback — a
|
|
@@ -847,25 +939,27 @@ var require_build = __commonJS({
|
|
|
847
939
|
* ad.addAdEventListener(AdEventType.PAID, Traqio.admobPaid(AD_UNIT_ID, "rewarded"))
|
|
848
940
|
*
|
|
849
941
|
* Reads the library's PAID payload (`value` in currency units, or v17's exact
|
|
850
|
-
* `valueMicros`) and reports it as AdMob revenue.
|
|
942
|
+
* `valueMicros`) and reports it as AdMob revenue. With `adRevenue: { admob: true }`
|
|
943
|
+
* it only counts the formats the capture does not reach (banners): on a
|
|
944
|
+
* full-screen or native ad it is ignored, so the impression is never doubled.
|
|
851
945
|
*/
|
|
852
946
|
admobPaid(adUnitId, adFormat = "banner") {
|
|
853
947
|
return (event) => {
|
|
854
948
|
try {
|
|
855
949
|
const ad = (0, adRevenue_1.fromGoogleMobileAdsPaid)(event, { adUnitId, adFormat });
|
|
856
|
-
if (ad)
|
|
857
|
-
this.
|
|
950
|
+
if (ad && this.adGate.helper("admob", adFormat, event, "Traqio.admobPaid()"))
|
|
951
|
+
this.queueAdRevenue(ad, "admob", "auto");
|
|
858
952
|
} catch {
|
|
859
953
|
}
|
|
860
954
|
};
|
|
861
955
|
}
|
|
862
|
-
queueAdRevenue(ad, source) {
|
|
956
|
+
queueAdRevenue(ad, source, origin) {
|
|
863
957
|
var _a;
|
|
864
958
|
if (!this.started || this.erased)
|
|
865
959
|
return;
|
|
866
960
|
if (!(ad.valueMicros >= 0) || !ad.currency)
|
|
867
961
|
return;
|
|
868
|
-
this.adQueue.push({ ...ad, source, impressionId: uuid(), ts: (_a = ad.ts) != null ? _a : Math.floor(Date.now() / 1e3) });
|
|
962
|
+
this.adQueue.push({ ...ad, source, origin, impressionId: uuid(), ts: (_a = ad.ts) != null ? _a : Math.floor(Date.now() / 1e3) });
|
|
869
963
|
this.skan.addRevenue(ad.valueMicros / 1e6);
|
|
870
964
|
if (this.adQueue.length > MAX_AD_QUEUE)
|
|
871
965
|
this.adQueue.splice(0, this.adQueue.length - MAX_AD_QUEUE);
|
|
@@ -1753,6 +1847,156 @@ var BrowserStore = class {
|
|
|
1753
1847
|
}
|
|
1754
1848
|
};
|
|
1755
1849
|
|
|
1850
|
+
// src/kept.ts
|
|
1851
|
+
var KeptValue = class {
|
|
1852
|
+
constructor(store, key, cookieDomain, cookies, valid) {
|
|
1853
|
+
this.store = store;
|
|
1854
|
+
this.key = key;
|
|
1855
|
+
this.cookieDomain = cookieDomain;
|
|
1856
|
+
this.cookies = cookies;
|
|
1857
|
+
this.valid = valid;
|
|
1858
|
+
}
|
|
1859
|
+
/** The kept value, or null once it has expired or was never there. */
|
|
1860
|
+
get(now = Date.now()) {
|
|
1861
|
+
const raw = this.store.getSync(this.key);
|
|
1862
|
+
if (raw) {
|
|
1863
|
+
try {
|
|
1864
|
+
const parsed = JSON.parse(raw);
|
|
1865
|
+
if (parsed && typeof parsed.id === "string" && parsed.exp > now) return parsed.id;
|
|
1866
|
+
} catch {
|
|
1867
|
+
}
|
|
1868
|
+
}
|
|
1869
|
+
if (this.cookies && this.store.isPersistent) {
|
|
1870
|
+
const fromCookie = readCookie(this.key);
|
|
1871
|
+
if (fromCookie && this.valid(fromCookie)) return fromCookie;
|
|
1872
|
+
}
|
|
1873
|
+
return null;
|
|
1874
|
+
}
|
|
1875
|
+
set(value, maxAgeMs, now = Date.now()) {
|
|
1876
|
+
const record = { id: value, exp: now + maxAgeMs };
|
|
1877
|
+
this.store.setSync(this.key, JSON.stringify(record));
|
|
1878
|
+
if (this.cookies && this.store.isPersistent) writeCookie(this.key, value, maxAgeMs / 1e3, this.cookieDomain());
|
|
1879
|
+
}
|
|
1880
|
+
/** Removes it everywhere — `forgetMe()`. */
|
|
1881
|
+
clear() {
|
|
1882
|
+
this.store.removeSync(this.key);
|
|
1883
|
+
if (this.cookies) deleteCookie(this.key, this.cookieDomain());
|
|
1884
|
+
}
|
|
1885
|
+
/** Writes the cookie half once storage becomes allowed (the localStorage half moves with the store). */
|
|
1886
|
+
persistCookie(now = Date.now()) {
|
|
1887
|
+
const raw = this.store.getSync(this.key);
|
|
1888
|
+
if (!raw || !this.cookies || !this.store.isPersistent) return;
|
|
1889
|
+
try {
|
|
1890
|
+
const parsed = JSON.parse(raw);
|
|
1891
|
+
if (parsed.exp > now) writeCookie(this.key, parsed.id, (parsed.exp - now) / 1e3, this.cookieDomain());
|
|
1892
|
+
} catch {
|
|
1893
|
+
}
|
|
1894
|
+
}
|
|
1895
|
+
};
|
|
1896
|
+
|
|
1897
|
+
// src/source.ts
|
|
1898
|
+
var SOURCE_KEY = "traq_src";
|
|
1899
|
+
var SOURCE_PARAMS = ["utm_source", "utm_medium", "utm_campaign", "utm_content", "utm_term", "gclid", "fbclid", "ttclid"];
|
|
1900
|
+
var MAX_VALUE_LENGTH = 200;
|
|
1901
|
+
var FIELD = {
|
|
1902
|
+
utm_source: "utmSource",
|
|
1903
|
+
utm_medium: "utmMedium",
|
|
1904
|
+
utm_campaign: "utmCampaign",
|
|
1905
|
+
utm_content: "utmContent",
|
|
1906
|
+
utm_term: "utmTerm",
|
|
1907
|
+
gclid: "gclid",
|
|
1908
|
+
fbclid: "fbclid",
|
|
1909
|
+
ttclid: "ttclid",
|
|
1910
|
+
referrer: "referrer"
|
|
1911
|
+
};
|
|
1912
|
+
var clean = (v) => {
|
|
1913
|
+
const t = v == null ? void 0 : v.trim();
|
|
1914
|
+
return t ? t.slice(0, MAX_VALUE_LENGTH) : null;
|
|
1915
|
+
};
|
|
1916
|
+
var bare = (host) => host.toLowerCase().replace(/^www\./, "");
|
|
1917
|
+
function sameSite(host, other) {
|
|
1918
|
+
const a = bare(host);
|
|
1919
|
+
const b = bare(other);
|
|
1920
|
+
return a === b || a.endsWith(`.${b}`) || b.endsWith(`.${a}`);
|
|
1921
|
+
}
|
|
1922
|
+
function referrerHost(referrer, pageHref) {
|
|
1923
|
+
if (!referrer) return null;
|
|
1924
|
+
try {
|
|
1925
|
+
const ref = new URL(referrer);
|
|
1926
|
+
if (ref.protocol !== "http:" && ref.protocol !== "https:") return null;
|
|
1927
|
+
if (sameSite(new URL(pageHref).hostname, ref.hostname)) return null;
|
|
1928
|
+
return clean(bare(ref.hostname));
|
|
1929
|
+
} catch {
|
|
1930
|
+
return null;
|
|
1931
|
+
}
|
|
1932
|
+
}
|
|
1933
|
+
function sourceFromLanding(href, referrer, now) {
|
|
1934
|
+
const out = new URLSearchParams();
|
|
1935
|
+
try {
|
|
1936
|
+
const params = new URL(href).searchParams;
|
|
1937
|
+
for (const key of SOURCE_PARAMS) {
|
|
1938
|
+
const v = clean(params.get(key));
|
|
1939
|
+
if (v) out.set(key, v);
|
|
1940
|
+
}
|
|
1941
|
+
} catch {
|
|
1942
|
+
return null;
|
|
1943
|
+
}
|
|
1944
|
+
const fromUrl = out.toString() !== "";
|
|
1945
|
+
const ref = referrerHost(referrer, href);
|
|
1946
|
+
if (ref) out.set("referrer", ref);
|
|
1947
|
+
if (out.toString() === "") return null;
|
|
1948
|
+
out.set("ts", String(now));
|
|
1949
|
+
return { query: out.toString(), fromUrl };
|
|
1950
|
+
}
|
|
1951
|
+
function parseSource(query) {
|
|
1952
|
+
let params;
|
|
1953
|
+
try {
|
|
1954
|
+
params = new URLSearchParams(query);
|
|
1955
|
+
} catch {
|
|
1956
|
+
return null;
|
|
1957
|
+
}
|
|
1958
|
+
const source = {};
|
|
1959
|
+
for (const [param, field] of Object.entries(FIELD)) {
|
|
1960
|
+
const v = clean(params.get(param));
|
|
1961
|
+
if (v) source[field] = v;
|
|
1962
|
+
}
|
|
1963
|
+
if (!Object.keys(source).length) return null;
|
|
1964
|
+
const ts = Number(params.get("ts"));
|
|
1965
|
+
if (Number.isFinite(ts) && ts > 0) source.landedAt = ts;
|
|
1966
|
+
return source;
|
|
1967
|
+
}
|
|
1968
|
+
var SourceKeeper = class {
|
|
1969
|
+
constructor(store, maxAgeMs, cookieDomain, cookies) {
|
|
1970
|
+
this.maxAgeMs = maxAgeMs;
|
|
1971
|
+
this.kept = new KeptValue(store, SOURCE_KEY, cookieDomain, cookies, (v) => parseSource(v) !== null);
|
|
1972
|
+
}
|
|
1973
|
+
/**
|
|
1974
|
+
* Reads the landing. URL parameters (`utm_*`, a click id) REPLACE what is kept:
|
|
1975
|
+
* the last campaign that brought the visitor is the one that counts, as for the
|
|
1976
|
+
* trackID. A referrer alone only fills an empty slot — the return from a payment
|
|
1977
|
+
* page or a webmail must not overwrite the campaign the visitor came from. A
|
|
1978
|
+
* landing with neither (a typed URL, an internal navigation) changes nothing.
|
|
1979
|
+
*/
|
|
1980
|
+
capture(href, referrer, now = Date.now()) {
|
|
1981
|
+
const landing = sourceFromLanding(href, referrer, now);
|
|
1982
|
+
if (!landing) return null;
|
|
1983
|
+
if (!landing.fromUrl && this.kept.get(now) !== null) return null;
|
|
1984
|
+
this.kept.set(landing.query, this.maxAgeMs, now);
|
|
1985
|
+
return parseSource(landing.query);
|
|
1986
|
+
}
|
|
1987
|
+
/** The kept source, or null (none, or expired). */
|
|
1988
|
+
get(now = Date.now()) {
|
|
1989
|
+
const query = this.kept.get(now);
|
|
1990
|
+
return query === null ? null : parseSource(query);
|
|
1991
|
+
}
|
|
1992
|
+
clear() {
|
|
1993
|
+
this.kept.clear();
|
|
1994
|
+
}
|
|
1995
|
+
persistCookie(now = Date.now()) {
|
|
1996
|
+
this.kept.persistCookie(now);
|
|
1997
|
+
}
|
|
1998
|
+
};
|
|
1999
|
+
|
|
1756
2000
|
// src/trackId.ts
|
|
1757
2001
|
var CLICK_PARAM = "traq_click";
|
|
1758
2002
|
var STORE_KEY = "traq_click";
|
|
@@ -1767,10 +2011,8 @@ function clickIdFromUrl(href) {
|
|
|
1767
2011
|
}
|
|
1768
2012
|
var TrackIdKeeper = class {
|
|
1769
2013
|
constructor(store, maxAgeMs, cookieDomain, cookies) {
|
|
1770
|
-
this.store = store;
|
|
1771
2014
|
this.maxAgeMs = maxAgeMs;
|
|
1772
|
-
this.
|
|
1773
|
-
this.cookies = cookies;
|
|
2015
|
+
this.kept = new KeptValue(store, STORE_KEY, cookieDomain, cookies, (v) => CLICK_ID.test(v));
|
|
1774
2016
|
}
|
|
1775
2017
|
/**
|
|
1776
2018
|
* Reads the landing URL. A new click REPLACES the kept one: the last ad clicked is
|
|
@@ -1779,44 +2021,20 @@ var TrackIdKeeper = class {
|
|
|
1779
2021
|
*/
|
|
1780
2022
|
capture(href, now = Date.now()) {
|
|
1781
2023
|
const id = clickIdFromUrl(href);
|
|
1782
|
-
if (id) this.
|
|
2024
|
+
if (id) this.kept.set(id, this.maxAgeMs, now);
|
|
1783
2025
|
return id;
|
|
1784
2026
|
}
|
|
1785
2027
|
/** The kept click id, or null once it has expired or was never there. */
|
|
1786
2028
|
get(now = Date.now()) {
|
|
1787
|
-
|
|
1788
|
-
if (raw) {
|
|
1789
|
-
try {
|
|
1790
|
-
const parsed = JSON.parse(raw);
|
|
1791
|
-
if (parsed && typeof parsed.id === "string" && parsed.exp > now) return parsed.id;
|
|
1792
|
-
} catch {
|
|
1793
|
-
}
|
|
1794
|
-
}
|
|
1795
|
-
if (this.cookies && this.store.isPersistent) {
|
|
1796
|
-
const fromCookie = readCookie(STORE_KEY);
|
|
1797
|
-
if (fromCookie && CLICK_ID.test(fromCookie)) return fromCookie;
|
|
1798
|
-
}
|
|
1799
|
-
return null;
|
|
2029
|
+
return this.kept.get(now);
|
|
1800
2030
|
}
|
|
1801
2031
|
/** Removes it everywhere — `forgetMe()`. */
|
|
1802
2032
|
clear() {
|
|
1803
|
-
this.
|
|
1804
|
-
if (this.cookies) deleteCookie(STORE_KEY, this.cookieDomain());
|
|
2033
|
+
this.kept.clear();
|
|
1805
2034
|
}
|
|
1806
2035
|
/** Writes the cookie half once storage becomes allowed (the localStorage half moves with the store). */
|
|
1807
2036
|
persistCookie(now = Date.now()) {
|
|
1808
|
-
|
|
1809
|
-
if (!raw || !this.cookies || !this.store.isPersistent) return;
|
|
1810
|
-
try {
|
|
1811
|
-
const parsed = JSON.parse(raw);
|
|
1812
|
-
if (parsed.exp > now) writeCookie(STORE_KEY, parsed.id, (parsed.exp - now) / 1e3, this.cookieDomain());
|
|
1813
|
-
} catch {
|
|
1814
|
-
}
|
|
1815
|
-
}
|
|
1816
|
-
write(id, now) {
|
|
1817
|
-
const record = { id, exp: now + this.maxAgeMs };
|
|
1818
|
-
this.store.setSync(STORE_KEY, JSON.stringify(record));
|
|
1819
|
-
if (this.cookies && this.store.isPersistent) writeCookie(STORE_KEY, id, this.maxAgeMs / 1e3, this.cookieDomain());
|
|
2037
|
+
this.kept.persistCookie(now);
|
|
1820
2038
|
}
|
|
1821
2039
|
};
|
|
1822
2040
|
|
|
@@ -1839,6 +2057,7 @@ var TraqioWeb = class {
|
|
|
1839
2057
|
this.core = new import_core.Client();
|
|
1840
2058
|
this.store = null;
|
|
1841
2059
|
this.trackIds = null;
|
|
2060
|
+
this.sources = null;
|
|
1842
2061
|
this.waitForConsent = false;
|
|
1843
2062
|
this.started = false;
|
|
1844
2063
|
}
|
|
@@ -1859,8 +2078,11 @@ var TraqioWeb = class {
|
|
|
1859
2078
|
const consent = (_a = opts.consent) != null ? _a : this.earlyConsent;
|
|
1860
2079
|
const persistent = !this.waitForConsent || consent === true;
|
|
1861
2080
|
this.store = new BrowserStore(persistent, cookieDomain, cookies);
|
|
1862
|
-
|
|
2081
|
+
const keepMs = ((_b = opts.trackIdDays) != null ? _b : DEFAULT_TRACK_ID_DAYS) * DAY_MS;
|
|
2082
|
+
this.trackIds = new TrackIdKeeper(this.store, keepMs, cookieDomain, cookies);
|
|
1863
2083
|
this.trackIds.capture(location.href);
|
|
2084
|
+
this.sources = new SourceKeeper(this.store, keepMs, cookieDomain, cookies);
|
|
2085
|
+
this.sources.capture(location.href, document.referrer);
|
|
1864
2086
|
if (!this.store.getSync(DEVICE_ID_KEY)) {
|
|
1865
2087
|
const id = randomId();
|
|
1866
2088
|
if (id) this.store.setSync(DEVICE_ID_KEY, id);
|
|
@@ -1898,12 +2120,13 @@ var TraqioWeb = class {
|
|
|
1898
2120
|
* stored on the device.
|
|
1899
2121
|
*/
|
|
1900
2122
|
async setConsent(granted) {
|
|
1901
|
-
var _a;
|
|
2123
|
+
var _a, _b;
|
|
1902
2124
|
if (!this.started) this.earlyConsent = granted;
|
|
1903
2125
|
if (this.store && this.waitForConsent) {
|
|
1904
2126
|
if (granted) {
|
|
1905
2127
|
this.store.enable();
|
|
1906
2128
|
(_a = this.trackIds) == null ? void 0 : _a.persistCookie();
|
|
2129
|
+
(_b = this.sources) == null ? void 0 : _b.persistCookie();
|
|
1907
2130
|
} else {
|
|
1908
2131
|
this.store.disable();
|
|
1909
2132
|
}
|
|
@@ -1919,15 +2142,29 @@ var TraqioWeb = class {
|
|
|
1919
2142
|
var _a, _b;
|
|
1920
2143
|
return (_b = (_a = this.trackIds) == null ? void 0 : _a.get()) != null ? _b : null;
|
|
1921
2144
|
}
|
|
2145
|
+
/**
|
|
2146
|
+
* Where the visitor came from when no Traqio link brought them — the landing's
|
|
2147
|
+
* `utm_*`, an ad network's click id (`gclid`, `fbclid`, `ttclid`), the referring
|
|
2148
|
+
* site — or null. Kept in the `traq_src` cookie your backend reads with the
|
|
2149
|
+
* sign-up (`@traqio/node`: `sourceFromRequest(req)`); pass this to it instead
|
|
2150
|
+
* when your backend is on another domain. A Traqio link click wins over it.
|
|
2151
|
+
*/
|
|
2152
|
+
getSource() {
|
|
2153
|
+
var _a, _b;
|
|
2154
|
+
return (_b = (_a = this.sources) == null ? void 0 : _a.get()) != null ? _b : null;
|
|
2155
|
+
}
|
|
1922
2156
|
/**
|
|
1923
2157
|
* GDPR erasure from the page: the server deletes what it holds about this browser
|
|
1924
2158
|
* and user, and this SDK stops sending and forgets the trackID. Irreversible.
|
|
1925
2159
|
* Resolves false when the request did not reach the server — then nothing changed.
|
|
1926
2160
|
*/
|
|
1927
2161
|
async forgetMe() {
|
|
1928
|
-
var _a;
|
|
2162
|
+
var _a, _b;
|
|
1929
2163
|
const ok = await this.core.forgetMe();
|
|
1930
|
-
if (ok)
|
|
2164
|
+
if (ok) {
|
|
2165
|
+
(_a = this.trackIds) == null ? void 0 : _a.clear();
|
|
2166
|
+
(_b = this.sources) == null ? void 0 : _b.clear();
|
|
2167
|
+
}
|
|
1931
2168
|
return ok;
|
|
1932
2169
|
}
|
|
1933
2170
|
/** Send what is queued now. Already wired to tab hide and page exit. */
|