@traqio/web 0.2.2 → 0.3.1
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 +236 -46
- package/build/index.cjs.map +3 -3
- package/build/index.d.ts +13 -1
- package/build/index.js +236 -46
- package/build/index.js.map +3 -3
- 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/README.md
CHANGED
|
@@ -7,6 +7,11 @@ arrives from a Traqio tracking link, the landing URL carries the ad click id as
|
|
|
7
7
|
plus a cookie on your own domain) and hands it back when the visitor converts — so the
|
|
8
8
|
conversion is tied to its click **deterministically**, not by guesswork.
|
|
9
9
|
|
|
10
|
+
A visitor who arrives **without** a Traqio link keeps their origin too: the landing's
|
|
11
|
+
`utm_*` parameters, the ad networks' click ids (`gclid`, `fbclid`, `ttclid`) and the
|
|
12
|
+
referring site, kept the same way in a `traq_src` cookie — so a sign-up from an
|
|
13
|
+
Instagram bio or a newsletter is still reported under its source and campaign.
|
|
14
|
+
|
|
10
15
|
It also records web events and visits, with consent handling and in-page erasure. No
|
|
11
16
|
fingerprinting, no third-party cookie, no advertising id. ~7 KB gzipped, no dependency.
|
|
12
17
|
|
|
@@ -55,6 +60,25 @@ const trackId = Traqio.getTrackId(); // null when the visitor did not come from
|
|
|
55
60
|
form.querySelector<HTMLInputElement>('input[name="traq_click"]')!.value = trackId ?? "";
|
|
56
61
|
```
|
|
57
62
|
|
|
63
|
+
### The visit's source, when there was no Traqio link
|
|
64
|
+
|
|
65
|
+
On landing, the SDK also keeps where the visitor came from — `utm_source`,
|
|
66
|
+
`utm_medium`, `utm_campaign`, `utm_content`, `utm_term`, `gclid`, `fbclid`, `ttclid`,
|
|
67
|
+
and the referring site's host (never its path) — in a `traq_src` cookie, a plain query
|
|
68
|
+
string. `@traqio/node` reads it with `sourceFromRequest(req)`; elsewhere, send
|
|
69
|
+
`Traqio.getSource()` with the form. Traqio then reports the sign-up under that source
|
|
70
|
+
and campaign. Rules:
|
|
71
|
+
|
|
72
|
+
- a new `utm_*` or click id replaces the kept source (last campaign wins, like the
|
|
73
|
+
trackID); a referrer alone only fills an empty slot, so the return from a payment
|
|
74
|
+
page never overwrites the campaign;
|
|
75
|
+
- when the visitor also came through a Traqio link, the link click wins;
|
|
76
|
+
- same lifetime (`trackIdDays`), same consent rules and same erasure as the trackID.
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
const source = Traqio.getSource(); // { utmSource: "instagram", utmCampaign: "launch", referrer: "l.instagram.com", … } or null
|
|
80
|
+
```
|
|
81
|
+
|
|
58
82
|
The browser SDK never sends the trackID itself: a key that anyone can read in your page
|
|
59
83
|
must not be able to credit a campaign. Your backend sends the conversion with its
|
|
60
84
|
trackID using a **secret** server key.
|
|
@@ -92,7 +116,7 @@ hidden or the page is left.
|
|
|
92
116
|
```ts
|
|
93
117
|
Traqio.init({ sdkKey: "YOUR_WEB_KEY", waitForConsent: true });
|
|
94
118
|
// … when your banner gets an answer:
|
|
95
|
-
Traqio.setConsent(true); // stores what the page kept in memory, trackID included
|
|
119
|
+
Traqio.setConsent(true); // stores what the page kept in memory, trackID and source included
|
|
96
120
|
Traqio.setConsent(false); // removes everything the SDK stored on the device
|
|
97
121
|
```
|
|
98
122
|
|
|
@@ -110,7 +134,7 @@ networks.
|
|
|
110
134
|
| `userId` | — | Your user id, once known |
|
|
111
135
|
| `consent` | not collected | The visitor's consent, if already known |
|
|
112
136
|
| `waitForConsent` | `false` | Store nothing until `setConsent(true)` |
|
|
113
|
-
| `trackIdDays` | `30` | How long the trackID
|
|
137
|
+
| `trackIdDays` | `30` | How long the trackID and the visit's source are kept |
|
|
114
138
|
| `cookies` | `true` | `false` = localStorage only, no sharing across subdomains |
|
|
115
139
|
| `debug` | detected | Debug traffic, kept out of every report (see below) |
|
|
116
140
|
|
package/build/index.cjs
CHANGED
|
@@ -641,7 +641,7 @@ var require_build = __commonJS({
|
|
|
641
641
|
var adservices_1 = require_adservices();
|
|
642
642
|
var deeplink_1 = require_deeplink();
|
|
643
643
|
var adRevenue_1 = require_adRevenue();
|
|
644
|
-
exports.SDK_VERSION = "0.5.
|
|
644
|
+
exports.SDK_VERSION = "0.5.1";
|
|
645
645
|
var skan_2 = require_skan();
|
|
646
646
|
Object.defineProperty(exports, "Skan", { enumerable: true, get: function() {
|
|
647
647
|
return skan_2.Skan;
|
|
@@ -717,6 +717,7 @@ var require_build = __commonJS({
|
|
|
717
717
|
var FLUSH_DELAY_MS = 3e3;
|
|
718
718
|
var MAX_INSTALL_ATTEMPTS = 3;
|
|
719
719
|
var SESSION_TIMEOUT_MS = 30 * 60 * 1e3;
|
|
720
|
+
var TRACKING_AUTHORIZATIONS = ["not_determined", "restricted", "denied", "authorized"];
|
|
720
721
|
var MAX_PRECISION = {
|
|
721
722
|
exact: "precise",
|
|
722
723
|
publisher_defined: "publisher_provided",
|
|
@@ -787,6 +788,15 @@ var require_build = __commonJS({
|
|
|
787
788
|
}
|
|
788
789
|
return void 0;
|
|
789
790
|
}
|
|
791
|
+
function reactNativeOs() {
|
|
792
|
+
var _a, _b;
|
|
793
|
+
try {
|
|
794
|
+
const os = (_b = (_a = (init_empty(), __toCommonJS(empty_exports))) == null ? void 0 : _a.Platform) == null ? void 0 : _b.OS;
|
|
795
|
+
return os === "ios" || os === "android" ? os : void 0;
|
|
796
|
+
} catch {
|
|
797
|
+
return void 0;
|
|
798
|
+
}
|
|
799
|
+
}
|
|
790
800
|
var Client2 = class {
|
|
791
801
|
constructor() {
|
|
792
802
|
this.queue = [];
|
|
@@ -805,6 +815,7 @@ var require_build = __commonJS({
|
|
|
805
815
|
this.undelivered = [];
|
|
806
816
|
this.deferred = null;
|
|
807
817
|
this.consent = null;
|
|
818
|
+
this.att = null;
|
|
808
819
|
this.debug = false;
|
|
809
820
|
this.maxRevenuePaid = (adInfo) => {
|
|
810
821
|
try {
|
|
@@ -891,9 +902,18 @@ var require_build = __commonJS({
|
|
|
891
902
|
if (props == null ? void 0 : props.value)
|
|
892
903
|
this.skan.addRevenue(props.value);
|
|
893
904
|
}
|
|
905
|
+
/**
|
|
906
|
+
* The store this app runs on: the native bridge's word, else React Native's
|
|
907
|
+
* `Platform.OS`, else UNKNOWN. Never a guess — until 2026-11 a missing bridge meant
|
|
908
|
+
* "android", and every bridgeless iPhone was filed as an Android install whose
|
|
909
|
+
* purchases then went to Meta with the Android package (todo cmwl6okn).
|
|
910
|
+
*/
|
|
911
|
+
get hostPlatform() {
|
|
912
|
+
var _a, _b;
|
|
913
|
+
return (_b = (_a = this.opts.native) == null ? void 0 : _a.platform) != null ? _b : reactNativeOs();
|
|
914
|
+
}
|
|
894
915
|
defaultStore() {
|
|
895
|
-
|
|
896
|
-
const platform = (_a = this.opts.native) == null ? void 0 : _a.platform;
|
|
916
|
+
const platform = this.hostPlatform;
|
|
897
917
|
return platform === "ios" ? "app_store" : platform === "android" ? "play_store" : void 0;
|
|
898
918
|
}
|
|
899
919
|
/**
|
|
@@ -1183,7 +1203,6 @@ var require_build = __commonJS({
|
|
|
1183
1203
|
* (still counted as fresh, since the flag is only written on an answer).
|
|
1184
1204
|
*/
|
|
1185
1205
|
async resolveDeferredOnce(freshInstall) {
|
|
1186
|
-
var _a, _b;
|
|
1187
1206
|
const store = this.opts.storage;
|
|
1188
1207
|
try {
|
|
1189
1208
|
const cached = await (store == null ? void 0 : store.getItem(DEFERRED_KEY));
|
|
@@ -1193,7 +1212,7 @@ var require_build = __commonJS({
|
|
|
1193
1212
|
}
|
|
1194
1213
|
if (!freshInstall && !await (store == null ? void 0 : store.getItem(`${DEFERRED_KEY}_pending`).catch(() => null)))
|
|
1195
1214
|
return null;
|
|
1196
|
-
const platform =
|
|
1215
|
+
const platform = this.hostPlatform;
|
|
1197
1216
|
const referrer = platform === "android" ? await this.readReferrer() : null;
|
|
1198
1217
|
await (store == null ? void 0 : store.setItem(`${DEFERRED_KEY}_pending`, "1").catch(() => void 0));
|
|
1199
1218
|
const answer = await this.askDeferred(platform, referrer == null ? void 0 : referrer.referrer);
|
|
@@ -1230,7 +1249,7 @@ var require_build = __commonJS({
|
|
|
1230
1249
|
* Never when a link is already known, never twice.
|
|
1231
1250
|
*/
|
|
1232
1251
|
async retryDeferredWithConsent() {
|
|
1233
|
-
var _a, _b
|
|
1252
|
+
var _a, _b;
|
|
1234
1253
|
const store = (_a = this.opts) == null ? void 0 : _a.storage;
|
|
1235
1254
|
if (!store || this.consent !== "granted" || this.opts.deepLinking === false || this.isWeb)
|
|
1236
1255
|
return null;
|
|
@@ -1243,10 +1262,10 @@ var require_build = __commonJS({
|
|
|
1243
1262
|
const known = await this.storedDeferred();
|
|
1244
1263
|
if (known)
|
|
1245
1264
|
return known;
|
|
1246
|
-
const link = (0, deeplink_1.toDeepLink)(await this.askDeferred(
|
|
1265
|
+
const link = (0, deeplink_1.toDeepLink)(await this.askDeferred(this.hostPlatform, void 0), true);
|
|
1247
1266
|
if (!link)
|
|
1248
1267
|
return null;
|
|
1249
|
-
await ((
|
|
1268
|
+
await ((_b = this.opts.storage) == null ? void 0 : _b.setItem(DEFERRED_KEY, JSON.stringify(link)).catch(() => void 0));
|
|
1250
1269
|
this.emitDeepLink(link);
|
|
1251
1270
|
return link;
|
|
1252
1271
|
}
|
|
@@ -1510,11 +1529,17 @@ var require_build = __commonJS({
|
|
|
1510
1529
|
}
|
|
1511
1530
|
}
|
|
1512
1531
|
async sendInstallOnce() {
|
|
1513
|
-
var _a, _b;
|
|
1514
1532
|
const store = this.opts.storage;
|
|
1515
1533
|
if (store && await store.getItem(this.installSentKey))
|
|
1516
1534
|
return;
|
|
1517
|
-
const platform =
|
|
1535
|
+
const platform = this.hostPlatform;
|
|
1536
|
+
if (!platform) {
|
|
1537
|
+
console.warn("[traqio] unknown platform \u2014 no native bridge and not React Native: the install is not reported. Pass `native: getNativeBridge()` (React Native / Expo) or your SDK's bridge to init().");
|
|
1538
|
+
return;
|
|
1539
|
+
}
|
|
1540
|
+
if (!this.opts.native) {
|
|
1541
|
+
console.warn(`[traqio] the Traqio native module is missing on ${platform} (Expo Go, or not linked in this build): install reported without its attribution signals. Use a development / EAS build.`);
|
|
1542
|
+
}
|
|
1518
1543
|
const referrer = await this.readReferrer();
|
|
1519
1544
|
if (referrer.retryable && store) {
|
|
1520
1545
|
const attempts = Number(await store.getItem(INSTALL_ATTEMPTS_KEY)) || 0;
|
|
@@ -1638,6 +1663,21 @@ var require_build = __commonJS({
|
|
|
1638
1663
|
keepalive() {
|
|
1639
1664
|
return this.isWeb ? { keepalive: true } : {};
|
|
1640
1665
|
}
|
|
1666
|
+
get asksTrackingAuthorization() {
|
|
1667
|
+
var _a;
|
|
1668
|
+
return ((_a = this.opts.native) == null ? void 0 : _a.platform) === "ios" && typeof this.opts.native.getTrackingAuthorization === "function";
|
|
1669
|
+
}
|
|
1670
|
+
/** iOS only; the last good answer when the bridge cannot give one now. */
|
|
1671
|
+
async readTrackingAuthorization() {
|
|
1672
|
+
var _a, _b;
|
|
1673
|
+
try {
|
|
1674
|
+
const status = await ((_b = (_a = this.opts.native) == null ? void 0 : _a.getTrackingAuthorization) == null ? void 0 : _b.call(_a));
|
|
1675
|
+
if (TRACKING_AUTHORIZATIONS.includes(status))
|
|
1676
|
+
this.att = status != null ? status : null;
|
|
1677
|
+
} catch {
|
|
1678
|
+
}
|
|
1679
|
+
return this.att;
|
|
1680
|
+
}
|
|
1641
1681
|
async post(body, path = "/ingest") {
|
|
1642
1682
|
return await this.send(body, path) === "ok";
|
|
1643
1683
|
}
|
|
@@ -1649,11 +1689,18 @@ var require_build = __commonJS({
|
|
|
1649
1689
|
*/
|
|
1650
1690
|
async send(body, path = "/ingest") {
|
|
1651
1691
|
const consent = this.consent && (path === "/ingest" || path === "/ingest/session") ? { consent: this.consent } : {};
|
|
1692
|
+
const att = path === "/ingest" && this.asksTrackingAuthorization ? await this.readTrackingAuthorization() : null;
|
|
1652
1693
|
try {
|
|
1653
1694
|
const res = await fetch(`${this.opts.endpoint}${path}`, {
|
|
1654
1695
|
method: "POST",
|
|
1655
1696
|
headers: { "Content-Type": "application/json" },
|
|
1656
|
-
body: JSON.stringify({
|
|
1697
|
+
body: JSON.stringify({
|
|
1698
|
+
sdkKey: this.opts.sdkKey,
|
|
1699
|
+
...body,
|
|
1700
|
+
...consent,
|
|
1701
|
+
...att ? { att } : {},
|
|
1702
|
+
...this.debugField()
|
|
1703
|
+
}),
|
|
1657
1704
|
...this.keepalive()
|
|
1658
1705
|
});
|
|
1659
1706
|
if (res.ok)
|
|
@@ -1859,6 +1906,156 @@ var BrowserStore = class {
|
|
|
1859
1906
|
}
|
|
1860
1907
|
};
|
|
1861
1908
|
|
|
1909
|
+
// src/kept.ts
|
|
1910
|
+
var KeptValue = class {
|
|
1911
|
+
constructor(store, key, cookieDomain, cookies, valid) {
|
|
1912
|
+
this.store = store;
|
|
1913
|
+
this.key = key;
|
|
1914
|
+
this.cookieDomain = cookieDomain;
|
|
1915
|
+
this.cookies = cookies;
|
|
1916
|
+
this.valid = valid;
|
|
1917
|
+
}
|
|
1918
|
+
/** The kept value, or null once it has expired or was never there. */
|
|
1919
|
+
get(now = Date.now()) {
|
|
1920
|
+
const raw = this.store.getSync(this.key);
|
|
1921
|
+
if (raw) {
|
|
1922
|
+
try {
|
|
1923
|
+
const parsed = JSON.parse(raw);
|
|
1924
|
+
if (parsed && typeof parsed.id === "string" && parsed.exp > now) return parsed.id;
|
|
1925
|
+
} catch {
|
|
1926
|
+
}
|
|
1927
|
+
}
|
|
1928
|
+
if (this.cookies && this.store.isPersistent) {
|
|
1929
|
+
const fromCookie = readCookie(this.key);
|
|
1930
|
+
if (fromCookie && this.valid(fromCookie)) return fromCookie;
|
|
1931
|
+
}
|
|
1932
|
+
return null;
|
|
1933
|
+
}
|
|
1934
|
+
set(value, maxAgeMs, now = Date.now()) {
|
|
1935
|
+
const record = { id: value, exp: now + maxAgeMs };
|
|
1936
|
+
this.store.setSync(this.key, JSON.stringify(record));
|
|
1937
|
+
if (this.cookies && this.store.isPersistent) writeCookie(this.key, value, maxAgeMs / 1e3, this.cookieDomain());
|
|
1938
|
+
}
|
|
1939
|
+
/** Removes it everywhere — `forgetMe()`. */
|
|
1940
|
+
clear() {
|
|
1941
|
+
this.store.removeSync(this.key);
|
|
1942
|
+
if (this.cookies) deleteCookie(this.key, this.cookieDomain());
|
|
1943
|
+
}
|
|
1944
|
+
/** Writes the cookie half once storage becomes allowed (the localStorage half moves with the store). */
|
|
1945
|
+
persistCookie(now = Date.now()) {
|
|
1946
|
+
const raw = this.store.getSync(this.key);
|
|
1947
|
+
if (!raw || !this.cookies || !this.store.isPersistent) return;
|
|
1948
|
+
try {
|
|
1949
|
+
const parsed = JSON.parse(raw);
|
|
1950
|
+
if (parsed.exp > now) writeCookie(this.key, parsed.id, (parsed.exp - now) / 1e3, this.cookieDomain());
|
|
1951
|
+
} catch {
|
|
1952
|
+
}
|
|
1953
|
+
}
|
|
1954
|
+
};
|
|
1955
|
+
|
|
1956
|
+
// src/source.ts
|
|
1957
|
+
var SOURCE_KEY = "traq_src";
|
|
1958
|
+
var SOURCE_PARAMS = ["utm_source", "utm_medium", "utm_campaign", "utm_content", "utm_term", "gclid", "fbclid", "ttclid"];
|
|
1959
|
+
var MAX_VALUE_LENGTH = 200;
|
|
1960
|
+
var FIELD = {
|
|
1961
|
+
utm_source: "utmSource",
|
|
1962
|
+
utm_medium: "utmMedium",
|
|
1963
|
+
utm_campaign: "utmCampaign",
|
|
1964
|
+
utm_content: "utmContent",
|
|
1965
|
+
utm_term: "utmTerm",
|
|
1966
|
+
gclid: "gclid",
|
|
1967
|
+
fbclid: "fbclid",
|
|
1968
|
+
ttclid: "ttclid",
|
|
1969
|
+
referrer: "referrer"
|
|
1970
|
+
};
|
|
1971
|
+
var clean = (v) => {
|
|
1972
|
+
const t = v == null ? void 0 : v.trim();
|
|
1973
|
+
return t ? t.slice(0, MAX_VALUE_LENGTH) : null;
|
|
1974
|
+
};
|
|
1975
|
+
var bare = (host) => host.toLowerCase().replace(/^www\./, "");
|
|
1976
|
+
function sameSite(host, other) {
|
|
1977
|
+
const a = bare(host);
|
|
1978
|
+
const b = bare(other);
|
|
1979
|
+
return a === b || a.endsWith(`.${b}`) || b.endsWith(`.${a}`);
|
|
1980
|
+
}
|
|
1981
|
+
function referrerHost(referrer, pageHref) {
|
|
1982
|
+
if (!referrer) return null;
|
|
1983
|
+
try {
|
|
1984
|
+
const ref = new URL(referrer);
|
|
1985
|
+
if (ref.protocol !== "http:" && ref.protocol !== "https:") return null;
|
|
1986
|
+
if (sameSite(new URL(pageHref).hostname, ref.hostname)) return null;
|
|
1987
|
+
return clean(bare(ref.hostname));
|
|
1988
|
+
} catch {
|
|
1989
|
+
return null;
|
|
1990
|
+
}
|
|
1991
|
+
}
|
|
1992
|
+
function sourceFromLanding(href, referrer, now) {
|
|
1993
|
+
const out = new URLSearchParams();
|
|
1994
|
+
try {
|
|
1995
|
+
const params = new URL(href).searchParams;
|
|
1996
|
+
for (const key of SOURCE_PARAMS) {
|
|
1997
|
+
const v = clean(params.get(key));
|
|
1998
|
+
if (v) out.set(key, v);
|
|
1999
|
+
}
|
|
2000
|
+
} catch {
|
|
2001
|
+
return null;
|
|
2002
|
+
}
|
|
2003
|
+
const fromUrl = out.toString() !== "";
|
|
2004
|
+
const ref = referrerHost(referrer, href);
|
|
2005
|
+
if (ref) out.set("referrer", ref);
|
|
2006
|
+
if (out.toString() === "") return null;
|
|
2007
|
+
out.set("ts", String(now));
|
|
2008
|
+
return { query: out.toString(), fromUrl };
|
|
2009
|
+
}
|
|
2010
|
+
function parseSource(query) {
|
|
2011
|
+
let params;
|
|
2012
|
+
try {
|
|
2013
|
+
params = new URLSearchParams(query);
|
|
2014
|
+
} catch {
|
|
2015
|
+
return null;
|
|
2016
|
+
}
|
|
2017
|
+
const source = {};
|
|
2018
|
+
for (const [param, field] of Object.entries(FIELD)) {
|
|
2019
|
+
const v = clean(params.get(param));
|
|
2020
|
+
if (v) source[field] = v;
|
|
2021
|
+
}
|
|
2022
|
+
if (!Object.keys(source).length) return null;
|
|
2023
|
+
const ts = Number(params.get("ts"));
|
|
2024
|
+
if (Number.isFinite(ts) && ts > 0) source.landedAt = ts;
|
|
2025
|
+
return source;
|
|
2026
|
+
}
|
|
2027
|
+
var SourceKeeper = class {
|
|
2028
|
+
constructor(store, maxAgeMs, cookieDomain, cookies) {
|
|
2029
|
+
this.maxAgeMs = maxAgeMs;
|
|
2030
|
+
this.kept = new KeptValue(store, SOURCE_KEY, cookieDomain, cookies, (v) => parseSource(v) !== null);
|
|
2031
|
+
}
|
|
2032
|
+
/**
|
|
2033
|
+
* Reads the landing. URL parameters (`utm_*`, a click id) REPLACE what is kept:
|
|
2034
|
+
* the last campaign that brought the visitor is the one that counts, as for the
|
|
2035
|
+
* trackID. A referrer alone only fills an empty slot — the return from a payment
|
|
2036
|
+
* page or a webmail must not overwrite the campaign the visitor came from. A
|
|
2037
|
+
* landing with neither (a typed URL, an internal navigation) changes nothing.
|
|
2038
|
+
*/
|
|
2039
|
+
capture(href, referrer, now = Date.now()) {
|
|
2040
|
+
const landing = sourceFromLanding(href, referrer, now);
|
|
2041
|
+
if (!landing) return null;
|
|
2042
|
+
if (!landing.fromUrl && this.kept.get(now) !== null) return null;
|
|
2043
|
+
this.kept.set(landing.query, this.maxAgeMs, now);
|
|
2044
|
+
return parseSource(landing.query);
|
|
2045
|
+
}
|
|
2046
|
+
/** The kept source, or null (none, or expired). */
|
|
2047
|
+
get(now = Date.now()) {
|
|
2048
|
+
const query = this.kept.get(now);
|
|
2049
|
+
return query === null ? null : parseSource(query);
|
|
2050
|
+
}
|
|
2051
|
+
clear() {
|
|
2052
|
+
this.kept.clear();
|
|
2053
|
+
}
|
|
2054
|
+
persistCookie(now = Date.now()) {
|
|
2055
|
+
this.kept.persistCookie(now);
|
|
2056
|
+
}
|
|
2057
|
+
};
|
|
2058
|
+
|
|
1862
2059
|
// src/trackId.ts
|
|
1863
2060
|
var CLICK_PARAM = "traq_click";
|
|
1864
2061
|
var STORE_KEY = "traq_click";
|
|
@@ -1873,10 +2070,8 @@ function clickIdFromUrl(href) {
|
|
|
1873
2070
|
}
|
|
1874
2071
|
var TrackIdKeeper = class {
|
|
1875
2072
|
constructor(store, maxAgeMs, cookieDomain, cookies) {
|
|
1876
|
-
this.store = store;
|
|
1877
2073
|
this.maxAgeMs = maxAgeMs;
|
|
1878
|
-
this.
|
|
1879
|
-
this.cookies = cookies;
|
|
2074
|
+
this.kept = new KeptValue(store, STORE_KEY, cookieDomain, cookies, (v) => CLICK_ID.test(v));
|
|
1880
2075
|
}
|
|
1881
2076
|
/**
|
|
1882
2077
|
* Reads the landing URL. A new click REPLACES the kept one: the last ad clicked is
|
|
@@ -1885,44 +2080,20 @@ var TrackIdKeeper = class {
|
|
|
1885
2080
|
*/
|
|
1886
2081
|
capture(href, now = Date.now()) {
|
|
1887
2082
|
const id = clickIdFromUrl(href);
|
|
1888
|
-
if (id) this.
|
|
2083
|
+
if (id) this.kept.set(id, this.maxAgeMs, now);
|
|
1889
2084
|
return id;
|
|
1890
2085
|
}
|
|
1891
2086
|
/** The kept click id, or null once it has expired or was never there. */
|
|
1892
2087
|
get(now = Date.now()) {
|
|
1893
|
-
|
|
1894
|
-
if (raw) {
|
|
1895
|
-
try {
|
|
1896
|
-
const parsed = JSON.parse(raw);
|
|
1897
|
-
if (parsed && typeof parsed.id === "string" && parsed.exp > now) return parsed.id;
|
|
1898
|
-
} catch {
|
|
1899
|
-
}
|
|
1900
|
-
}
|
|
1901
|
-
if (this.cookies && this.store.isPersistent) {
|
|
1902
|
-
const fromCookie = readCookie(STORE_KEY);
|
|
1903
|
-
if (fromCookie && CLICK_ID.test(fromCookie)) return fromCookie;
|
|
1904
|
-
}
|
|
1905
|
-
return null;
|
|
2088
|
+
return this.kept.get(now);
|
|
1906
2089
|
}
|
|
1907
2090
|
/** Removes it everywhere — `forgetMe()`. */
|
|
1908
2091
|
clear() {
|
|
1909
|
-
this.
|
|
1910
|
-
if (this.cookies) deleteCookie(STORE_KEY, this.cookieDomain());
|
|
2092
|
+
this.kept.clear();
|
|
1911
2093
|
}
|
|
1912
2094
|
/** Writes the cookie half once storage becomes allowed (the localStorage half moves with the store). */
|
|
1913
2095
|
persistCookie(now = Date.now()) {
|
|
1914
|
-
|
|
1915
|
-
if (!raw || !this.cookies || !this.store.isPersistent) return;
|
|
1916
|
-
try {
|
|
1917
|
-
const parsed = JSON.parse(raw);
|
|
1918
|
-
if (parsed.exp > now) writeCookie(STORE_KEY, parsed.id, (parsed.exp - now) / 1e3, this.cookieDomain());
|
|
1919
|
-
} catch {
|
|
1920
|
-
}
|
|
1921
|
-
}
|
|
1922
|
-
write(id, now) {
|
|
1923
|
-
const record = { id, exp: now + this.maxAgeMs };
|
|
1924
|
-
this.store.setSync(STORE_KEY, JSON.stringify(record));
|
|
1925
|
-
if (this.cookies && this.store.isPersistent) writeCookie(STORE_KEY, id, this.maxAgeMs / 1e3, this.cookieDomain());
|
|
2096
|
+
this.kept.persistCookie(now);
|
|
1926
2097
|
}
|
|
1927
2098
|
};
|
|
1928
2099
|
|
|
@@ -1945,6 +2116,7 @@ var TraqioWeb = class {
|
|
|
1945
2116
|
this.core = new import_core.Client();
|
|
1946
2117
|
this.store = null;
|
|
1947
2118
|
this.trackIds = null;
|
|
2119
|
+
this.sources = null;
|
|
1948
2120
|
this.waitForConsent = false;
|
|
1949
2121
|
this.started = false;
|
|
1950
2122
|
}
|
|
@@ -1965,8 +2137,11 @@ var TraqioWeb = class {
|
|
|
1965
2137
|
const consent = (_a = opts.consent) != null ? _a : this.earlyConsent;
|
|
1966
2138
|
const persistent = !this.waitForConsent || consent === true;
|
|
1967
2139
|
this.store = new BrowserStore(persistent, cookieDomain, cookies);
|
|
1968
|
-
|
|
2140
|
+
const keepMs = ((_b = opts.trackIdDays) != null ? _b : DEFAULT_TRACK_ID_DAYS) * DAY_MS;
|
|
2141
|
+
this.trackIds = new TrackIdKeeper(this.store, keepMs, cookieDomain, cookies);
|
|
1969
2142
|
this.trackIds.capture(location.href);
|
|
2143
|
+
this.sources = new SourceKeeper(this.store, keepMs, cookieDomain, cookies);
|
|
2144
|
+
this.sources.capture(location.href, document.referrer);
|
|
1970
2145
|
if (!this.store.getSync(DEVICE_ID_KEY)) {
|
|
1971
2146
|
const id = randomId();
|
|
1972
2147
|
if (id) this.store.setSync(DEVICE_ID_KEY, id);
|
|
@@ -2004,12 +2179,13 @@ var TraqioWeb = class {
|
|
|
2004
2179
|
* stored on the device.
|
|
2005
2180
|
*/
|
|
2006
2181
|
async setConsent(granted) {
|
|
2007
|
-
var _a;
|
|
2182
|
+
var _a, _b;
|
|
2008
2183
|
if (!this.started) this.earlyConsent = granted;
|
|
2009
2184
|
if (this.store && this.waitForConsent) {
|
|
2010
2185
|
if (granted) {
|
|
2011
2186
|
this.store.enable();
|
|
2012
2187
|
(_a = this.trackIds) == null ? void 0 : _a.persistCookie();
|
|
2188
|
+
(_b = this.sources) == null ? void 0 : _b.persistCookie();
|
|
2013
2189
|
} else {
|
|
2014
2190
|
this.store.disable();
|
|
2015
2191
|
}
|
|
@@ -2025,15 +2201,29 @@ var TraqioWeb = class {
|
|
|
2025
2201
|
var _a, _b;
|
|
2026
2202
|
return (_b = (_a = this.trackIds) == null ? void 0 : _a.get()) != null ? _b : null;
|
|
2027
2203
|
}
|
|
2204
|
+
/**
|
|
2205
|
+
* Where the visitor came from when no Traqio link brought them — the landing's
|
|
2206
|
+
* `utm_*`, an ad network's click id (`gclid`, `fbclid`, `ttclid`), the referring
|
|
2207
|
+
* site — or null. Kept in the `traq_src` cookie your backend reads with the
|
|
2208
|
+
* sign-up (`@traqio/node`: `sourceFromRequest(req)`); pass this to it instead
|
|
2209
|
+
* when your backend is on another domain. A Traqio link click wins over it.
|
|
2210
|
+
*/
|
|
2211
|
+
getSource() {
|
|
2212
|
+
var _a, _b;
|
|
2213
|
+
return (_b = (_a = this.sources) == null ? void 0 : _a.get()) != null ? _b : null;
|
|
2214
|
+
}
|
|
2028
2215
|
/**
|
|
2029
2216
|
* GDPR erasure from the page: the server deletes what it holds about this browser
|
|
2030
2217
|
* and user, and this SDK stops sending and forgets the trackID. Irreversible.
|
|
2031
2218
|
* Resolves false when the request did not reach the server — then nothing changed.
|
|
2032
2219
|
*/
|
|
2033
2220
|
async forgetMe() {
|
|
2034
|
-
var _a;
|
|
2221
|
+
var _a, _b;
|
|
2035
2222
|
const ok = await this.core.forgetMe();
|
|
2036
|
-
if (ok)
|
|
2223
|
+
if (ok) {
|
|
2224
|
+
(_a = this.trackIds) == null ? void 0 : _a.clear();
|
|
2225
|
+
(_b = this.sources) == null ? void 0 : _b.clear();
|
|
2226
|
+
}
|
|
2037
2227
|
return ok;
|
|
2038
2228
|
}
|
|
2039
2229
|
/** Send what is queued now. Already wired to tab hide and page exit. */
|