@traqio/web 0.2.2 → 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 +178 -35
- package/build/index.cjs.map +3 -3
- package/build/index.d.ts +13 -1
- package/build/index.js +178 -35
- 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 +1 -1
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
|
@@ -1847,6 +1847,156 @@ var BrowserStore = class {
|
|
|
1847
1847
|
}
|
|
1848
1848
|
};
|
|
1849
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
|
+
|
|
1850
2000
|
// src/trackId.ts
|
|
1851
2001
|
var CLICK_PARAM = "traq_click";
|
|
1852
2002
|
var STORE_KEY = "traq_click";
|
|
@@ -1861,10 +2011,8 @@ function clickIdFromUrl(href) {
|
|
|
1861
2011
|
}
|
|
1862
2012
|
var TrackIdKeeper = class {
|
|
1863
2013
|
constructor(store, maxAgeMs, cookieDomain, cookies) {
|
|
1864
|
-
this.store = store;
|
|
1865
2014
|
this.maxAgeMs = maxAgeMs;
|
|
1866
|
-
this.
|
|
1867
|
-
this.cookies = cookies;
|
|
2015
|
+
this.kept = new KeptValue(store, STORE_KEY, cookieDomain, cookies, (v) => CLICK_ID.test(v));
|
|
1868
2016
|
}
|
|
1869
2017
|
/**
|
|
1870
2018
|
* Reads the landing URL. A new click REPLACES the kept one: the last ad clicked is
|
|
@@ -1873,44 +2021,20 @@ var TrackIdKeeper = class {
|
|
|
1873
2021
|
*/
|
|
1874
2022
|
capture(href, now = Date.now()) {
|
|
1875
2023
|
const id = clickIdFromUrl(href);
|
|
1876
|
-
if (id) this.
|
|
2024
|
+
if (id) this.kept.set(id, this.maxAgeMs, now);
|
|
1877
2025
|
return id;
|
|
1878
2026
|
}
|
|
1879
2027
|
/** The kept click id, or null once it has expired or was never there. */
|
|
1880
2028
|
get(now = Date.now()) {
|
|
1881
|
-
|
|
1882
|
-
if (raw) {
|
|
1883
|
-
try {
|
|
1884
|
-
const parsed = JSON.parse(raw);
|
|
1885
|
-
if (parsed && typeof parsed.id === "string" && parsed.exp > now) return parsed.id;
|
|
1886
|
-
} catch {
|
|
1887
|
-
}
|
|
1888
|
-
}
|
|
1889
|
-
if (this.cookies && this.store.isPersistent) {
|
|
1890
|
-
const fromCookie = readCookie(STORE_KEY);
|
|
1891
|
-
if (fromCookie && CLICK_ID.test(fromCookie)) return fromCookie;
|
|
1892
|
-
}
|
|
1893
|
-
return null;
|
|
2029
|
+
return this.kept.get(now);
|
|
1894
2030
|
}
|
|
1895
2031
|
/** Removes it everywhere — `forgetMe()`. */
|
|
1896
2032
|
clear() {
|
|
1897
|
-
this.
|
|
1898
|
-
if (this.cookies) deleteCookie(STORE_KEY, this.cookieDomain());
|
|
2033
|
+
this.kept.clear();
|
|
1899
2034
|
}
|
|
1900
2035
|
/** Writes the cookie half once storage becomes allowed (the localStorage half moves with the store). */
|
|
1901
2036
|
persistCookie(now = Date.now()) {
|
|
1902
|
-
|
|
1903
|
-
if (!raw || !this.cookies || !this.store.isPersistent) return;
|
|
1904
|
-
try {
|
|
1905
|
-
const parsed = JSON.parse(raw);
|
|
1906
|
-
if (parsed.exp > now) writeCookie(STORE_KEY, parsed.id, (parsed.exp - now) / 1e3, this.cookieDomain());
|
|
1907
|
-
} catch {
|
|
1908
|
-
}
|
|
1909
|
-
}
|
|
1910
|
-
write(id, now) {
|
|
1911
|
-
const record = { id, exp: now + this.maxAgeMs };
|
|
1912
|
-
this.store.setSync(STORE_KEY, JSON.stringify(record));
|
|
1913
|
-
if (this.cookies && this.store.isPersistent) writeCookie(STORE_KEY, id, this.maxAgeMs / 1e3, this.cookieDomain());
|
|
2037
|
+
this.kept.persistCookie(now);
|
|
1914
2038
|
}
|
|
1915
2039
|
};
|
|
1916
2040
|
|
|
@@ -1933,6 +2057,7 @@ var TraqioWeb = class {
|
|
|
1933
2057
|
this.core = new import_core.Client();
|
|
1934
2058
|
this.store = null;
|
|
1935
2059
|
this.trackIds = null;
|
|
2060
|
+
this.sources = null;
|
|
1936
2061
|
this.waitForConsent = false;
|
|
1937
2062
|
this.started = false;
|
|
1938
2063
|
}
|
|
@@ -1953,8 +2078,11 @@ var TraqioWeb = class {
|
|
|
1953
2078
|
const consent = (_a = opts.consent) != null ? _a : this.earlyConsent;
|
|
1954
2079
|
const persistent = !this.waitForConsent || consent === true;
|
|
1955
2080
|
this.store = new BrowserStore(persistent, cookieDomain, cookies);
|
|
1956
|
-
|
|
2081
|
+
const keepMs = ((_b = opts.trackIdDays) != null ? _b : DEFAULT_TRACK_ID_DAYS) * DAY_MS;
|
|
2082
|
+
this.trackIds = new TrackIdKeeper(this.store, keepMs, cookieDomain, cookies);
|
|
1957
2083
|
this.trackIds.capture(location.href);
|
|
2084
|
+
this.sources = new SourceKeeper(this.store, keepMs, cookieDomain, cookies);
|
|
2085
|
+
this.sources.capture(location.href, document.referrer);
|
|
1958
2086
|
if (!this.store.getSync(DEVICE_ID_KEY)) {
|
|
1959
2087
|
const id = randomId();
|
|
1960
2088
|
if (id) this.store.setSync(DEVICE_ID_KEY, id);
|
|
@@ -1992,12 +2120,13 @@ var TraqioWeb = class {
|
|
|
1992
2120
|
* stored on the device.
|
|
1993
2121
|
*/
|
|
1994
2122
|
async setConsent(granted) {
|
|
1995
|
-
var _a;
|
|
2123
|
+
var _a, _b;
|
|
1996
2124
|
if (!this.started) this.earlyConsent = granted;
|
|
1997
2125
|
if (this.store && this.waitForConsent) {
|
|
1998
2126
|
if (granted) {
|
|
1999
2127
|
this.store.enable();
|
|
2000
2128
|
(_a = this.trackIds) == null ? void 0 : _a.persistCookie();
|
|
2129
|
+
(_b = this.sources) == null ? void 0 : _b.persistCookie();
|
|
2001
2130
|
} else {
|
|
2002
2131
|
this.store.disable();
|
|
2003
2132
|
}
|
|
@@ -2013,15 +2142,29 @@ var TraqioWeb = class {
|
|
|
2013
2142
|
var _a, _b;
|
|
2014
2143
|
return (_b = (_a = this.trackIds) == null ? void 0 : _a.get()) != null ? _b : null;
|
|
2015
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
|
+
}
|
|
2016
2156
|
/**
|
|
2017
2157
|
* GDPR erasure from the page: the server deletes what it holds about this browser
|
|
2018
2158
|
* and user, and this SDK stops sending and forgets the trackID. Irreversible.
|
|
2019
2159
|
* Resolves false when the request did not reach the server — then nothing changed.
|
|
2020
2160
|
*/
|
|
2021
2161
|
async forgetMe() {
|
|
2022
|
-
var _a;
|
|
2162
|
+
var _a, _b;
|
|
2023
2163
|
const ok = await this.core.forgetMe();
|
|
2024
|
-
if (ok)
|
|
2164
|
+
if (ok) {
|
|
2165
|
+
(_a = this.trackIds) == null ? void 0 : _a.clear();
|
|
2166
|
+
(_b = this.sources) == null ? void 0 : _b.clear();
|
|
2167
|
+
}
|
|
2025
2168
|
return ok;
|
|
2026
2169
|
}
|
|
2027
2170
|
/** Send what is queued now. Already wired to tab hide and page exit. */
|