@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 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 is kept after the click |
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
@@ -1859,6 +1859,156 @@ var BrowserStore = class {
1859
1859
  }
1860
1860
  };
1861
1861
 
1862
+ // src/kept.ts
1863
+ var KeptValue = class {
1864
+ constructor(store, key, cookieDomain, cookies, valid) {
1865
+ this.store = store;
1866
+ this.key = key;
1867
+ this.cookieDomain = cookieDomain;
1868
+ this.cookies = cookies;
1869
+ this.valid = valid;
1870
+ }
1871
+ /** The kept value, or null once it has expired or was never there. */
1872
+ get(now = Date.now()) {
1873
+ const raw = this.store.getSync(this.key);
1874
+ if (raw) {
1875
+ try {
1876
+ const parsed = JSON.parse(raw);
1877
+ if (parsed && typeof parsed.id === "string" && parsed.exp > now) return parsed.id;
1878
+ } catch {
1879
+ }
1880
+ }
1881
+ if (this.cookies && this.store.isPersistent) {
1882
+ const fromCookie = readCookie(this.key);
1883
+ if (fromCookie && this.valid(fromCookie)) return fromCookie;
1884
+ }
1885
+ return null;
1886
+ }
1887
+ set(value, maxAgeMs, now = Date.now()) {
1888
+ const record = { id: value, exp: now + maxAgeMs };
1889
+ this.store.setSync(this.key, JSON.stringify(record));
1890
+ if (this.cookies && this.store.isPersistent) writeCookie(this.key, value, maxAgeMs / 1e3, this.cookieDomain());
1891
+ }
1892
+ /** Removes it everywhere — `forgetMe()`. */
1893
+ clear() {
1894
+ this.store.removeSync(this.key);
1895
+ if (this.cookies) deleteCookie(this.key, this.cookieDomain());
1896
+ }
1897
+ /** Writes the cookie half once storage becomes allowed (the localStorage half moves with the store). */
1898
+ persistCookie(now = Date.now()) {
1899
+ const raw = this.store.getSync(this.key);
1900
+ if (!raw || !this.cookies || !this.store.isPersistent) return;
1901
+ try {
1902
+ const parsed = JSON.parse(raw);
1903
+ if (parsed.exp > now) writeCookie(this.key, parsed.id, (parsed.exp - now) / 1e3, this.cookieDomain());
1904
+ } catch {
1905
+ }
1906
+ }
1907
+ };
1908
+
1909
+ // src/source.ts
1910
+ var SOURCE_KEY = "traq_src";
1911
+ var SOURCE_PARAMS = ["utm_source", "utm_medium", "utm_campaign", "utm_content", "utm_term", "gclid", "fbclid", "ttclid"];
1912
+ var MAX_VALUE_LENGTH = 200;
1913
+ var FIELD = {
1914
+ utm_source: "utmSource",
1915
+ utm_medium: "utmMedium",
1916
+ utm_campaign: "utmCampaign",
1917
+ utm_content: "utmContent",
1918
+ utm_term: "utmTerm",
1919
+ gclid: "gclid",
1920
+ fbclid: "fbclid",
1921
+ ttclid: "ttclid",
1922
+ referrer: "referrer"
1923
+ };
1924
+ var clean = (v) => {
1925
+ const t = v == null ? void 0 : v.trim();
1926
+ return t ? t.slice(0, MAX_VALUE_LENGTH) : null;
1927
+ };
1928
+ var bare = (host) => host.toLowerCase().replace(/^www\./, "");
1929
+ function sameSite(host, other) {
1930
+ const a = bare(host);
1931
+ const b = bare(other);
1932
+ return a === b || a.endsWith(`.${b}`) || b.endsWith(`.${a}`);
1933
+ }
1934
+ function referrerHost(referrer, pageHref) {
1935
+ if (!referrer) return null;
1936
+ try {
1937
+ const ref = new URL(referrer);
1938
+ if (ref.protocol !== "http:" && ref.protocol !== "https:") return null;
1939
+ if (sameSite(new URL(pageHref).hostname, ref.hostname)) return null;
1940
+ return clean(bare(ref.hostname));
1941
+ } catch {
1942
+ return null;
1943
+ }
1944
+ }
1945
+ function sourceFromLanding(href, referrer, now) {
1946
+ const out = new URLSearchParams();
1947
+ try {
1948
+ const params = new URL(href).searchParams;
1949
+ for (const key of SOURCE_PARAMS) {
1950
+ const v = clean(params.get(key));
1951
+ if (v) out.set(key, v);
1952
+ }
1953
+ } catch {
1954
+ return null;
1955
+ }
1956
+ const fromUrl = out.toString() !== "";
1957
+ const ref = referrerHost(referrer, href);
1958
+ if (ref) out.set("referrer", ref);
1959
+ if (out.toString() === "") return null;
1960
+ out.set("ts", String(now));
1961
+ return { query: out.toString(), fromUrl };
1962
+ }
1963
+ function parseSource(query) {
1964
+ let params;
1965
+ try {
1966
+ params = new URLSearchParams(query);
1967
+ } catch {
1968
+ return null;
1969
+ }
1970
+ const source = {};
1971
+ for (const [param, field] of Object.entries(FIELD)) {
1972
+ const v = clean(params.get(param));
1973
+ if (v) source[field] = v;
1974
+ }
1975
+ if (!Object.keys(source).length) return null;
1976
+ const ts = Number(params.get("ts"));
1977
+ if (Number.isFinite(ts) && ts > 0) source.landedAt = ts;
1978
+ return source;
1979
+ }
1980
+ var SourceKeeper = class {
1981
+ constructor(store, maxAgeMs, cookieDomain, cookies) {
1982
+ this.maxAgeMs = maxAgeMs;
1983
+ this.kept = new KeptValue(store, SOURCE_KEY, cookieDomain, cookies, (v) => parseSource(v) !== null);
1984
+ }
1985
+ /**
1986
+ * Reads the landing. URL parameters (`utm_*`, a click id) REPLACE what is kept:
1987
+ * the last campaign that brought the visitor is the one that counts, as for the
1988
+ * trackID. A referrer alone only fills an empty slot — the return from a payment
1989
+ * page or a webmail must not overwrite the campaign the visitor came from. A
1990
+ * landing with neither (a typed URL, an internal navigation) changes nothing.
1991
+ */
1992
+ capture(href, referrer, now = Date.now()) {
1993
+ const landing = sourceFromLanding(href, referrer, now);
1994
+ if (!landing) return null;
1995
+ if (!landing.fromUrl && this.kept.get(now) !== null) return null;
1996
+ this.kept.set(landing.query, this.maxAgeMs, now);
1997
+ return parseSource(landing.query);
1998
+ }
1999
+ /** The kept source, or null (none, or expired). */
2000
+ get(now = Date.now()) {
2001
+ const query = this.kept.get(now);
2002
+ return query === null ? null : parseSource(query);
2003
+ }
2004
+ clear() {
2005
+ this.kept.clear();
2006
+ }
2007
+ persistCookie(now = Date.now()) {
2008
+ this.kept.persistCookie(now);
2009
+ }
2010
+ };
2011
+
1862
2012
  // src/trackId.ts
1863
2013
  var CLICK_PARAM = "traq_click";
1864
2014
  var STORE_KEY = "traq_click";
@@ -1873,10 +2023,8 @@ function clickIdFromUrl(href) {
1873
2023
  }
1874
2024
  var TrackIdKeeper = class {
1875
2025
  constructor(store, maxAgeMs, cookieDomain, cookies) {
1876
- this.store = store;
1877
2026
  this.maxAgeMs = maxAgeMs;
1878
- this.cookieDomain = cookieDomain;
1879
- this.cookies = cookies;
2027
+ this.kept = new KeptValue(store, STORE_KEY, cookieDomain, cookies, (v) => CLICK_ID.test(v));
1880
2028
  }
1881
2029
  /**
1882
2030
  * Reads the landing URL. A new click REPLACES the kept one: the last ad clicked is
@@ -1885,44 +2033,20 @@ var TrackIdKeeper = class {
1885
2033
  */
1886
2034
  capture(href, now = Date.now()) {
1887
2035
  const id = clickIdFromUrl(href);
1888
- if (id) this.write(id, now);
2036
+ if (id) this.kept.set(id, this.maxAgeMs, now);
1889
2037
  return id;
1890
2038
  }
1891
2039
  /** The kept click id, or null once it has expired or was never there. */
1892
2040
  get(now = Date.now()) {
1893
- const raw = this.store.getSync(STORE_KEY);
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;
2041
+ return this.kept.get(now);
1906
2042
  }
1907
2043
  /** Removes it everywhere — `forgetMe()`. */
1908
2044
  clear() {
1909
- this.store.removeSync(STORE_KEY);
1910
- if (this.cookies) deleteCookie(STORE_KEY, this.cookieDomain());
2045
+ this.kept.clear();
1911
2046
  }
1912
2047
  /** Writes the cookie half once storage becomes allowed (the localStorage half moves with the store). */
1913
2048
  persistCookie(now = Date.now()) {
1914
- const raw = this.store.getSync(STORE_KEY);
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());
2049
+ this.kept.persistCookie(now);
1926
2050
  }
1927
2051
  };
1928
2052
 
@@ -1945,6 +2069,7 @@ var TraqioWeb = class {
1945
2069
  this.core = new import_core.Client();
1946
2070
  this.store = null;
1947
2071
  this.trackIds = null;
2072
+ this.sources = null;
1948
2073
  this.waitForConsent = false;
1949
2074
  this.started = false;
1950
2075
  }
@@ -1965,8 +2090,11 @@ var TraqioWeb = class {
1965
2090
  const consent = (_a = opts.consent) != null ? _a : this.earlyConsent;
1966
2091
  const persistent = !this.waitForConsent || consent === true;
1967
2092
  this.store = new BrowserStore(persistent, cookieDomain, cookies);
1968
- this.trackIds = new TrackIdKeeper(this.store, ((_b = opts.trackIdDays) != null ? _b : DEFAULT_TRACK_ID_DAYS) * DAY_MS, cookieDomain, cookies);
2093
+ const keepMs = ((_b = opts.trackIdDays) != null ? _b : DEFAULT_TRACK_ID_DAYS) * DAY_MS;
2094
+ this.trackIds = new TrackIdKeeper(this.store, keepMs, cookieDomain, cookies);
1969
2095
  this.trackIds.capture(location.href);
2096
+ this.sources = new SourceKeeper(this.store, keepMs, cookieDomain, cookies);
2097
+ this.sources.capture(location.href, document.referrer);
1970
2098
  if (!this.store.getSync(DEVICE_ID_KEY)) {
1971
2099
  const id = randomId();
1972
2100
  if (id) this.store.setSync(DEVICE_ID_KEY, id);
@@ -2004,12 +2132,13 @@ var TraqioWeb = class {
2004
2132
  * stored on the device.
2005
2133
  */
2006
2134
  async setConsent(granted) {
2007
- var _a;
2135
+ var _a, _b;
2008
2136
  if (!this.started) this.earlyConsent = granted;
2009
2137
  if (this.store && this.waitForConsent) {
2010
2138
  if (granted) {
2011
2139
  this.store.enable();
2012
2140
  (_a = this.trackIds) == null ? void 0 : _a.persistCookie();
2141
+ (_b = this.sources) == null ? void 0 : _b.persistCookie();
2013
2142
  } else {
2014
2143
  this.store.disable();
2015
2144
  }
@@ -2025,15 +2154,29 @@ var TraqioWeb = class {
2025
2154
  var _a, _b;
2026
2155
  return (_b = (_a = this.trackIds) == null ? void 0 : _a.get()) != null ? _b : null;
2027
2156
  }
2157
+ /**
2158
+ * Where the visitor came from when no Traqio link brought them — the landing's
2159
+ * `utm_*`, an ad network's click id (`gclid`, `fbclid`, `ttclid`), the referring
2160
+ * site — or null. Kept in the `traq_src` cookie your backend reads with the
2161
+ * sign-up (`@traqio/node`: `sourceFromRequest(req)`); pass this to it instead
2162
+ * when your backend is on another domain. A Traqio link click wins over it.
2163
+ */
2164
+ getSource() {
2165
+ var _a, _b;
2166
+ return (_b = (_a = this.sources) == null ? void 0 : _a.get()) != null ? _b : null;
2167
+ }
2028
2168
  /**
2029
2169
  * GDPR erasure from the page: the server deletes what it holds about this browser
2030
2170
  * and user, and this SDK stops sending and forgets the trackID. Irreversible.
2031
2171
  * Resolves false when the request did not reach the server — then nothing changed.
2032
2172
  */
2033
2173
  async forgetMe() {
2034
- var _a;
2174
+ var _a, _b;
2035
2175
  const ok = await this.core.forgetMe();
2036
- if (ok) (_a = this.trackIds) == null ? void 0 : _a.clear();
2176
+ if (ok) {
2177
+ (_a = this.trackIds) == null ? void 0 : _a.clear();
2178
+ (_b = this.sources) == null ? void 0 : _b.clear();
2179
+ }
2037
2180
  return ok;
2038
2181
  }
2039
2182
  /** Send what is queued now. Already wired to tab hide and page exit. */