@temps-sdk/analytics-core 0.0.2 → 0.0.6
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/dist/Analytics.d.ts.map +1 -1
- package/dist/Analytics.js +22 -8
- package/dist/EngagementTracker.d.ts +3 -0
- package/dist/EngagementTracker.d.ts.map +1 -1
- package/dist/EngagementTracker.js +7 -2
- package/dist/SessionRecorder.d.ts +79 -3
- package/dist/SessionRecorder.d.ts.map +1 -1
- package/dist/SessionRecorder.js +380 -114
- package/dist/SpeedTracker.d.ts +36 -4
- package/dist/SpeedTracker.d.ts.map +1 -1
- package/dist/SpeedTracker.js +114 -37
- package/dist/constants.d.ts +13 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +15 -0
- package/dist/identity.d.ts +15 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +79 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/types.d.ts +67 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -0
- package/dist/utils.d.ts +30 -2
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +69 -13
- package/package.json +1 -1
package/dist/SpeedTracker.d.ts
CHANGED
|
@@ -1,18 +1,50 @@
|
|
|
1
1
|
export interface SpeedTrackerOptions {
|
|
2
2
|
basePath: string;
|
|
3
|
+
/** Analytics ingest key (`pa_…`). See `AnalyticsClientOptions.ingestKey`. */
|
|
4
|
+
ingestKey?: string;
|
|
3
5
|
}
|
|
4
6
|
/**
|
|
5
7
|
* Subscribes to Web Vitals and forwards metrics to the Temps analytics endpoint.
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
+
*
|
|
9
|
+
* Load metrics (TTFB, FCP, LCP) go out in one "speed" request as soon as all
|
|
10
|
+
* three are known, or when the page is hidden, whichever comes first; a
|
|
11
|
+
* value that is only reported after that hide-flush is sent on its own, so
|
|
12
|
+
* a visitor who switches tabs early still gets an LCP. They never wait for
|
|
13
|
+
* an interaction: FID only exists for visitors who click or
|
|
14
|
+
* type, so gating on it dropped every visitor who just read the page and
|
|
15
|
+
* left. FID rides along when it already arrived, and is otherwise sent on its
|
|
16
|
+
* own. Late metrics (CLS, INP) are sent individually as they stabilize.
|
|
17
|
+
*
|
|
18
|
+
* Every beacon carries the path and query captured when tracking started, not
|
|
19
|
+
* the ones current at send time. Web Vitals here are page-lifetime metrics of
|
|
20
|
+
* the hard navigation that loaded the page, so a CLS or INP value reported
|
|
21
|
+
* after a client-side route change still belongs to the landing page — the
|
|
22
|
+
* same attribution CrUX uses.
|
|
8
23
|
*/
|
|
9
24
|
export declare class SpeedTracker {
|
|
10
25
|
private readonly basePath;
|
|
11
|
-
private
|
|
26
|
+
private readonly ingestKey?;
|
|
27
|
+
private readonly pathname;
|
|
28
|
+
private readonly query;
|
|
29
|
+
private loadMetrics;
|
|
30
|
+
private loadSent;
|
|
31
|
+
/** Load metrics already delivered, so none is ever sent twice. */
|
|
32
|
+
private readonly sentLoadMetrics;
|
|
33
|
+
private stopped;
|
|
34
|
+
private readonly handleVisibilityChange;
|
|
35
|
+
private readonly handlePageHide;
|
|
12
36
|
constructor(options: SpeedTrackerOptions);
|
|
13
37
|
private start;
|
|
14
|
-
private
|
|
38
|
+
private recordLoad;
|
|
39
|
+
private recordFid;
|
|
40
|
+
/** Send the load beacon once, with whatever load metrics are known. */
|
|
41
|
+
private flushLoad;
|
|
15
42
|
private sendLate;
|
|
43
|
+
private send;
|
|
44
|
+
/**
|
|
45
|
+
* Stop sending. web-vitals has no unsubscribe, so its callbacks keep
|
|
46
|
+
* firing; they are ignored from here on.
|
|
47
|
+
*/
|
|
16
48
|
destroy(): void;
|
|
17
49
|
}
|
|
18
50
|
//# sourceMappingURL=SpeedTracker.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SpeedTracker.d.ts","sourceRoot":"","sources":["../src/SpeedTracker.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"SpeedTracker.d.ts","sourceRoot":"","sources":["../src/SpeedTracker.ts"],"names":[],"mappings":"AAOA,MAAM,WAAW,mBAAmB;IAClC,QAAQ,EAAE,MAAM,CAAC;IACjB,6EAA6E;IAC7E,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAID;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAS;IACpC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAc;IACvC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAc;IACpC,OAAO,CAAC,WAAW,CAA+C;IAClE,OAAO,CAAC,QAAQ,CAAS;IACzB,kEAAkE;IAClE,OAAO,CAAC,QAAQ,CAAC,eAAe,CAA6B;IAC7D,OAAO,CAAC,OAAO,CAAS;IAExB,OAAO,CAAC,QAAQ,CAAC,sBAAsB,CAErC;IAEF,OAAO,CAAC,QAAQ,CAAC,cAAc,CAE7B;gBAEU,OAAO,EAAE,mBAAmB;IASxC,OAAO,CAAC,KAAK;IAeb,OAAO,CAAC,UAAU;IAiBlB,OAAO,CAAC,SAAS;IAUjB,uEAAuE;IACvE,OAAO,CAAC,SAAS;IAkBjB,OAAO,CAAC,QAAQ;IAKhB,OAAO,CAAC,IAAI;IAgBZ;;;OAGG;IACI,OAAO,IAAI,IAAI;CAOvB"}
|
package/dist/SpeedTracker.js
CHANGED
|
@@ -1,61 +1,138 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024-2026 Temps Contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
1
3
|
import { onCLS, onFID, onLCP, onTTFB, onFCP, onINP } from "web-vitals";
|
|
2
|
-
import { sendAnalytics } from "./utils";
|
|
4
|
+
import { sendAnalytics, sendAnalyticsReliable } from "./utils";
|
|
3
5
|
/**
|
|
4
6
|
* Subscribes to Web Vitals and forwards metrics to the Temps analytics endpoint.
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
+
*
|
|
8
|
+
* Load metrics (TTFB, FCP, LCP) go out in one "speed" request as soon as all
|
|
9
|
+
* three are known, or when the page is hidden, whichever comes first; a
|
|
10
|
+
* value that is only reported after that hide-flush is sent on its own, so
|
|
11
|
+
* a visitor who switches tabs early still gets an LCP. They never wait for
|
|
12
|
+
* an interaction: FID only exists for visitors who click or
|
|
13
|
+
* type, so gating on it dropped every visitor who just read the page and
|
|
14
|
+
* left. FID rides along when it already arrived, and is otherwise sent on its
|
|
15
|
+
* own. Late metrics (CLS, INP) are sent individually as they stabilize.
|
|
16
|
+
*
|
|
17
|
+
* Every beacon carries the path and query captured when tracking started, not
|
|
18
|
+
* the ones current at send time. Web Vitals here are page-lifetime metrics of
|
|
19
|
+
* the hard navigation that loaded the page, so a CLS or INP value reported
|
|
20
|
+
* after a client-side route change still belongs to the landing page — the
|
|
21
|
+
* same attribution CrUX uses.
|
|
7
22
|
*/
|
|
8
23
|
export class SpeedTracker {
|
|
9
24
|
constructor(options) {
|
|
10
|
-
this.
|
|
25
|
+
this.pathname = "";
|
|
26
|
+
this.query = "";
|
|
27
|
+
this.loadMetrics = {};
|
|
28
|
+
this.loadSent = false;
|
|
29
|
+
/** Load metrics already delivered, so none is ever sent twice. */
|
|
30
|
+
this.sentLoadMetrics = new Set();
|
|
31
|
+
this.stopped = false;
|
|
32
|
+
this.handleVisibilityChange = () => {
|
|
33
|
+
if (document.visibilityState === "hidden")
|
|
34
|
+
this.flushLoad();
|
|
35
|
+
};
|
|
36
|
+
this.handlePageHide = () => {
|
|
37
|
+
this.flushLoad();
|
|
38
|
+
};
|
|
11
39
|
this.basePath = options.basePath;
|
|
40
|
+
this.ingestKey = options.ingestKey;
|
|
12
41
|
if (typeof window === "undefined")
|
|
13
42
|
return;
|
|
43
|
+
this.pathname = window.location.pathname;
|
|
44
|
+
this.query = window.location.search;
|
|
14
45
|
this.start();
|
|
15
46
|
}
|
|
16
47
|
start() {
|
|
17
|
-
onTTFB((m) =>
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
onLCP((m) => {
|
|
22
|
-
this.initialMetrics.LCP = { value: m.value, rating: m.rating };
|
|
23
|
-
this.sendInitial();
|
|
24
|
-
});
|
|
25
|
-
onFID((m) => {
|
|
26
|
-
this.initialMetrics.FID = { value: m.value, rating: m.rating };
|
|
27
|
-
this.sendInitial();
|
|
28
|
-
});
|
|
29
|
-
onFCP((m) => {
|
|
30
|
-
this.initialMetrics.FCP = { value: m.value, rating: m.rating };
|
|
31
|
-
this.sendInitial();
|
|
32
|
-
});
|
|
48
|
+
onTTFB((m) => this.recordLoad("ttfb", m.value));
|
|
49
|
+
onFCP((m) => this.recordLoad("fcp", m.value));
|
|
50
|
+
onLCP((m) => this.recordLoad("lcp", m.value));
|
|
51
|
+
onFID((m) => this.recordFid(m.value));
|
|
33
52
|
onCLS((m) => this.sendLate("cls", m.value));
|
|
34
53
|
onINP((m) => this.sendLate("inp", m.value));
|
|
54
|
+
// LCP is only final once the visitor interacts or leaves. A visitor who
|
|
55
|
+
// leaves before it is reported still has a TTFB and FCP worth keeping.
|
|
56
|
+
document.addEventListener("visibilitychange", this.handleVisibilityChange, true);
|
|
57
|
+
window.addEventListener("pagehide", this.handlePageHide, true);
|
|
35
58
|
}
|
|
36
|
-
|
|
37
|
-
if (
|
|
59
|
+
recordLoad(name, value) {
|
|
60
|
+
if (this.stopped || this.sentLoadMetrics.has(name))
|
|
38
61
|
return;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
62
|
+
if (this.loadSent) {
|
|
63
|
+
// The load beacon already went out without this value (the page was
|
|
64
|
+
// hidden before it was reported, e.g. LCP after the visitor came back).
|
|
65
|
+
// Send just the missing value rather than dropping it.
|
|
66
|
+
this.sentLoadMetrics.add(name);
|
|
67
|
+
this.send({ [name]: value });
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
this.loadMetrics[name] = value;
|
|
71
|
+
const { ttfb, fcp, lcp } = this.loadMetrics;
|
|
72
|
+
if (ttfb !== undefined && fcp !== undefined && lcp !== undefined) {
|
|
73
|
+
this.flushLoad();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
recordFid(value) {
|
|
77
|
+
if (this.stopped || this.sentLoadMetrics.has("fid"))
|
|
78
|
+
return;
|
|
79
|
+
if (this.loadSent) {
|
|
80
|
+
this.sentLoadMetrics.add("fid");
|
|
81
|
+
this.send({ fid: value });
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
this.loadMetrics.fid = value;
|
|
85
|
+
}
|
|
86
|
+
/** Send the load beacon once, with whatever load metrics are known. */
|
|
87
|
+
flushLoad() {
|
|
88
|
+
if (this.stopped || this.loadSent)
|
|
89
|
+
return;
|
|
90
|
+
const { ttfb, fcp, lcp, fid } = this.loadMetrics;
|
|
91
|
+
if (ttfb === undefined && fcp === undefined && lcp === undefined && fid === undefined) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
this.loadSent = true;
|
|
95
|
+
for (const [name, value] of Object.entries(this.loadMetrics)) {
|
|
96
|
+
if (value !== undefined)
|
|
97
|
+
this.sentLoadMetrics.add(name);
|
|
98
|
+
}
|
|
99
|
+
this.send({
|
|
100
|
+
ttfb: ttfb ?? null,
|
|
101
|
+
lcp: lcp ?? null,
|
|
102
|
+
fid: fid ?? null,
|
|
103
|
+
fcp: fcp ?? null,
|
|
104
|
+
});
|
|
48
105
|
}
|
|
49
106
|
sendLate(name, value) {
|
|
107
|
+
if (this.stopped)
|
|
108
|
+
return;
|
|
109
|
+
this.send({ [name]: value });
|
|
110
|
+
}
|
|
111
|
+
send(metrics) {
|
|
112
|
+
// The ingest field is `pathname`; a `path` key is ignored by the server
|
|
113
|
+
// and leaves the page NULL in storage.
|
|
50
114
|
const payload = {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
query:
|
|
115
|
+
...metrics,
|
|
116
|
+
pathname: this.pathname,
|
|
117
|
+
query: this.query,
|
|
54
118
|
};
|
|
55
|
-
|
|
119
|
+
// A page that is going away can cancel a plain fetch; a beacon survives.
|
|
120
|
+
if (typeof document !== "undefined" && document.visibilityState === "hidden") {
|
|
121
|
+
sendAnalyticsReliable("speed", payload, this.basePath, this.ingestKey);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
void sendAnalytics("speed", payload, "POST", this.basePath, this.ingestKey);
|
|
56
125
|
}
|
|
57
|
-
|
|
126
|
+
/**
|
|
127
|
+
* Stop sending. web-vitals has no unsubscribe, so its callbacks keep
|
|
128
|
+
* firing; they are ignored from here on.
|
|
129
|
+
*/
|
|
58
130
|
destroy() {
|
|
59
|
-
this.
|
|
131
|
+
this.stopped = true;
|
|
132
|
+
this.loadMetrics = {};
|
|
133
|
+
if (typeof window === "undefined")
|
|
134
|
+
return;
|
|
135
|
+
document.removeEventListener("visibilitychange", this.handleVisibilityChange, true);
|
|
136
|
+
window.removeEventListener("pagehide", this.handlePageHide, true);
|
|
60
137
|
}
|
|
61
138
|
}
|
package/dist/constants.d.ts
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
export declare const DEFAULT_BASE_PATH = "/api/_temps";
|
|
2
2
|
export declare const SESSION_RECORDER_ENDPOINT = "session-replay";
|
|
3
|
+
/**
|
|
4
|
+
* Header carrying the `pa_`-prefixed analytics ingest key (ADR-040). Preferred
|
|
5
|
+
* over the query param: it keeps the credential out of URLs, and therefore out
|
|
6
|
+
* of `proxy_logs.request_query`, referrer headers and browser history.
|
|
7
|
+
*/
|
|
8
|
+
export declare const INGEST_KEY_HEADER = "X-Temps-Analytics-Key";
|
|
9
|
+
/**
|
|
10
|
+
* Query-param fallback for the analytics ingest key. `navigator.sendBeacon`
|
|
11
|
+
* cannot set request headers, so unload-path deliveries — page_leave and the
|
|
12
|
+
* final session-replay flush, exactly the events that matter most — have to
|
|
13
|
+
* carry the key here instead.
|
|
14
|
+
*/
|
|
15
|
+
export declare const INGEST_KEY_QUERY_PARAM = "temps_key";
|
|
3
16
|
/**
|
|
4
17
|
* Built-in paths excluded from session replay by default, covering common
|
|
5
18
|
* authentication and payment flows so integrators don't accidentally record
|
package/dist/constants.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,iBAAiB,gBAAgB,CAAC;AAC/C,eAAO,MAAM,yBAAyB,mBAAmB,CAAC;AAE1D;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,0BAA0B,CAAC;AAEzD;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,cAAc,CAAC;AAElD;;;;;;;;GAQG;AACH,eAAO,MAAM,sBAAsB,EAAE,MAAM,EAkB1C,CAAC"}
|
package/dist/constants.js
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024-2026 Temps Contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
1
3
|
export const DEFAULT_BASE_PATH = "/api/_temps";
|
|
2
4
|
export const SESSION_RECORDER_ENDPOINT = "session-replay";
|
|
5
|
+
/**
|
|
6
|
+
* Header carrying the `pa_`-prefixed analytics ingest key (ADR-040). Preferred
|
|
7
|
+
* over the query param: it keeps the credential out of URLs, and therefore out
|
|
8
|
+
* of `proxy_logs.request_query`, referrer headers and browser history.
|
|
9
|
+
*/
|
|
10
|
+
export const INGEST_KEY_HEADER = "X-Temps-Analytics-Key";
|
|
11
|
+
/**
|
|
12
|
+
* Query-param fallback for the analytics ingest key. `navigator.sendBeacon`
|
|
13
|
+
* cannot set request headers, so unload-path deliveries — page_leave and the
|
|
14
|
+
* final session-replay flush, exactly the events that matter most — have to
|
|
15
|
+
* carry the key here instead.
|
|
16
|
+
*/
|
|
17
|
+
export const INGEST_KEY_QUERY_PARAM = "temps_key";
|
|
3
18
|
/**
|
|
4
19
|
* Built-in paths excluded from session replay by default, covering common
|
|
5
20
|
* authentication and payment flows so integrators don't accidentally record
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a stable visitor id, generating and persisting one on first call.
|
|
3
|
+
* `undefined` when `localStorage` is missing or unusable: outside a browser,
|
|
4
|
+
* blocked by the user's cookie settings or a sandboxed iframe (merely reading
|
|
5
|
+
* `localStorage` throws), or full. An id that cannot be persisted would be a
|
|
6
|
+
* new visitor on every event, so the server's own identity is used instead.
|
|
7
|
+
*/
|
|
8
|
+
export declare function getOrCreateVisitorId(): string | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Returns the current session id, rolling over to a new one once the
|
|
11
|
+
* inactivity window has elapsed since the last call. Call this once per
|
|
12
|
+
* outbound request so `lastSeen` tracks real activity.
|
|
13
|
+
*/
|
|
14
|
+
export declare function getOrCreateSessionId(): string | undefined;
|
|
15
|
+
//# sourceMappingURL=identity.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../src/identity.ts"],"names":[],"mappings":"AAsCA;;;;;;GAMG;AACH,wBAAgB,oBAAoB,IAAI,MAAM,GAAG,SAAS,CAYzD;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,IAAI,MAAM,GAAG,SAAS,CAgBzD"}
|
package/dist/identity.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024-2026 Temps Contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
3
|
+
/**
|
|
4
|
+
* First-party, client-generated visitor/session identity.
|
|
5
|
+
*
|
|
6
|
+
* The server prefers its own encrypted `_temps_visitor_id`/`_temps_sid`
|
|
7
|
+
* cookies (tamper-evident, issued by the Temps proxy when it serves the
|
|
8
|
+
* page). Those cookies only exist when Temps itself serves the app's HTML —
|
|
9
|
+
* when Temps is used purely as an analytics/session-replay backend for an
|
|
10
|
+
* app it doesn't deploy or proxy, no such cookie is ever issued, and a
|
|
11
|
+
* server `Set-Cookie` from a cross-origin ingest call would be a
|
|
12
|
+
* third-party cookie that Safari/Chrome/Firefox privacy modes block anyway
|
|
13
|
+
* (see gotempsh/temps#848). This module generates and persists identity
|
|
14
|
+
* entirely client-side instead: the script always executes in the page's
|
|
15
|
+
* own origin regardless of where it was loaded from, so a first-party
|
|
16
|
+
* `localStorage` entry works everywhere a Temps-issued cookie can't.
|
|
17
|
+
*/
|
|
18
|
+
const VISITOR_ID_KEY = "temps_visitor_id";
|
|
19
|
+
const SESSION_ID_KEY = "temps_session_id";
|
|
20
|
+
const SESSION_LAST_SEEN_KEY = "temps_session_last_seen";
|
|
21
|
+
/**
|
|
22
|
+
* Mirrors the proxy's default session inactivity window
|
|
23
|
+
* (`session_max_age_minutes` in `crates/temps-proxy/src/traits.rs`) so
|
|
24
|
+
* client-generated session grouping stays consistent with the
|
|
25
|
+
* cookie-issued case.
|
|
26
|
+
*/
|
|
27
|
+
const SESSION_MAX_AGE_MS = 30 * 60 * 1000;
|
|
28
|
+
function randomId(prefix) {
|
|
29
|
+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
30
|
+
return crypto.randomUUID();
|
|
31
|
+
}
|
|
32
|
+
return `${prefix}_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Returns a stable visitor id, generating and persisting one on first call.
|
|
36
|
+
* `undefined` when `localStorage` is missing or unusable: outside a browser,
|
|
37
|
+
* blocked by the user's cookie settings or a sandboxed iframe (merely reading
|
|
38
|
+
* `localStorage` throws), or full. An id that cannot be persisted would be a
|
|
39
|
+
* new visitor on every event, so the server's own identity is used instead.
|
|
40
|
+
*/
|
|
41
|
+
export function getOrCreateVisitorId() {
|
|
42
|
+
try {
|
|
43
|
+
if (typeof localStorage === "undefined")
|
|
44
|
+
return undefined;
|
|
45
|
+
let visitorId = localStorage.getItem(VISITOR_ID_KEY);
|
|
46
|
+
if (!visitorId) {
|
|
47
|
+
visitorId = randomId("visitor");
|
|
48
|
+
localStorage.setItem(VISITOR_ID_KEY, visitorId);
|
|
49
|
+
}
|
|
50
|
+
return visitorId;
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Returns the current session id, rolling over to a new one once the
|
|
58
|
+
* inactivity window has elapsed since the last call. Call this once per
|
|
59
|
+
* outbound request so `lastSeen` tracks real activity.
|
|
60
|
+
*/
|
|
61
|
+
export function getOrCreateSessionId() {
|
|
62
|
+
try {
|
|
63
|
+
if (typeof localStorage === "undefined")
|
|
64
|
+
return undefined;
|
|
65
|
+
const now = Date.now();
|
|
66
|
+
const lastSeen = Number(localStorage.getItem(SESSION_LAST_SEEN_KEY));
|
|
67
|
+
let sessionId = localStorage.getItem(SESSION_ID_KEY);
|
|
68
|
+
if (!sessionId || !Number.isFinite(lastSeen) || now - lastSeen > SESSION_MAX_AGE_MS) {
|
|
69
|
+
sessionId = randomId("session");
|
|
70
|
+
localStorage.setItem(SESSION_ID_KEY, sessionId);
|
|
71
|
+
}
|
|
72
|
+
localStorage.setItem(SESSION_LAST_SEEN_KEY, String(now));
|
|
73
|
+
return sessionId;
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
// Same reasoning as getOrCreateVisitorId.
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
}
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,OAAO,EAAE,iBAAiB,EAAE,KAAK,wBAAwB,EAAE,KAAK,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAC5G,OAAO,EAAE,YAAY,EAAE,KAAK,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE,KAAK,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AACjF,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/types.d.ts
CHANGED
|
@@ -36,12 +36,49 @@ export interface SessionRecordingConfig {
|
|
|
36
36
|
maskTextClass?: string;
|
|
37
37
|
/** Record canvas elements. Defaults to false. */
|
|
38
38
|
recordCanvas?: boolean;
|
|
39
|
-
/**
|
|
39
|
+
/**
|
|
40
|
+
* Capture fonts loaded through the FontFace API so replays render with the
|
|
41
|
+
* page's own fonts. Defaults to true.
|
|
42
|
+
*/
|
|
40
43
|
collectFonts?: boolean;
|
|
41
44
|
/** Number of events to batch before sending. Defaults to 100. */
|
|
42
45
|
batchSize?: number;
|
|
43
|
-
/** Interval in ms to flush events. Defaults to
|
|
46
|
+
/** Interval in ms to flush events. Defaults to 10000. */
|
|
44
47
|
flushInterval?: number;
|
|
48
|
+
/**
|
|
49
|
+
* Milliseconds of no user interaction after which recording pauses. A paused
|
|
50
|
+
* recorder detaches rrweb entirely, so a passive tab stops producing events
|
|
51
|
+
* instead of recording page-driven DOM churn forever. Interaction resumes it
|
|
52
|
+
* with a fresh full snapshot, on the same session. Set to `0` to never pause.
|
|
53
|
+
* Defaults to 60000 (1 minute).
|
|
54
|
+
*/
|
|
55
|
+
idleTimeout?: number;
|
|
56
|
+
/**
|
|
57
|
+
* Pause recording while the document is hidden (background tab). Defaults to
|
|
58
|
+
* true. Independent of `idleTimeout` — a hidden tab pauses immediately.
|
|
59
|
+
*/
|
|
60
|
+
pauseOnHidden?: boolean;
|
|
61
|
+
/**
|
|
62
|
+
* Force a full DOM snapshot every N milliseconds. Snapshots make replay
|
|
63
|
+
* seeking cheap but are by far the largest events, so raising this trades
|
|
64
|
+
* seek granularity for ingest volume. Defaults to 30000.
|
|
65
|
+
*/
|
|
66
|
+
checkoutEveryNms?: number;
|
|
67
|
+
/** Force a full DOM snapshot every N events. Defaults to 200. */
|
|
68
|
+
checkoutEveryNth?: number;
|
|
69
|
+
/**
|
|
70
|
+
* Upper bound on events buffered while the server is unreachable. Past this
|
|
71
|
+
* the oldest events are dropped, so a persistent ingest outage costs bounded
|
|
72
|
+
* memory rather than growing until the tab dies. Defaults to 5000.
|
|
73
|
+
*/
|
|
74
|
+
maxBufferedEvents?: number;
|
|
75
|
+
/**
|
|
76
|
+
* Log every failed session-replay request, plus diagnostics: session start,
|
|
77
|
+
* why a page is not recorded (excluded path, sampled out), pauses and
|
|
78
|
+
* resumes. Without it, each kind of failure (init, upload) is reported once
|
|
79
|
+
* per page as a console warning. Defaults to false.
|
|
80
|
+
*/
|
|
81
|
+
debug?: boolean;
|
|
45
82
|
}
|
|
46
83
|
export interface AnalyticsClientOptions {
|
|
47
84
|
/** Base endpoint path. Defaults to `/api/_temps`. */
|
|
@@ -52,6 +89,26 @@ export interface AnalyticsClientOptions {
|
|
|
52
89
|
ignoreLocalhost?: boolean;
|
|
53
90
|
/** Custom domain to use for analytics. Defaults to window.location.hostname. */
|
|
54
91
|
domain?: string;
|
|
92
|
+
/**
|
|
93
|
+
* Analytics ingest key (`pa_…`), minted per project in the Temps Console
|
|
94
|
+
* (Project → Analytics → Setup) or with
|
|
95
|
+
* `bunx @temps-sdk/cli analytics keys create`.
|
|
96
|
+
*
|
|
97
|
+
* Only needed when Temps neither serves nor proxies the app. Without a
|
|
98
|
+
* Temps-managed deployment there is no `Host` entry in the proxy route
|
|
99
|
+
* table, so the server cannot tell which project an event belongs to and
|
|
100
|
+
* rejects ingest outright. Presenting the key resolves project and
|
|
101
|
+
* environment scope directly and the `Host` header stops being consulted
|
|
102
|
+
* for attribution. Leave it unset for apps deployed by Temps — that path is
|
|
103
|
+
* unchanged and remains the default.
|
|
104
|
+
*
|
|
105
|
+
* The key is **public by design**: it ships in your browser bundle and
|
|
106
|
+
* appears in request URLs on the `sendBeacon` path. It grants analytics
|
|
107
|
+
* ingest and nothing else. Never put a `tk_` API key, a `dt_` deployment
|
|
108
|
+
* token or an `si_` service ingest token here — those are secrets and do
|
|
109
|
+
* not work on this endpoint.
|
|
110
|
+
*/
|
|
111
|
+
ingestKey?: string;
|
|
55
112
|
}
|
|
56
113
|
export interface AnalyticsOptions extends AnalyticsClientOptions {
|
|
57
114
|
/** Auto track pageviews on route changes. Defaults to true. */
|
|
@@ -78,6 +135,14 @@ export interface AnalyticsOptions extends AnalyticsClientOptions {
|
|
|
78
135
|
export interface AnalyticsApi {
|
|
79
136
|
/** Whether analytics are currently enabled. */
|
|
80
137
|
readonly enabled: boolean;
|
|
138
|
+
/**
|
|
139
|
+
* The ingest key this instance was configured with, if any. Framework
|
|
140
|
+
* hooks that send their own beacons outside the plugin instance (e.g.
|
|
141
|
+
* `usePageLeave`'s unload handler) read this as their default so a
|
|
142
|
+
* cross-origin setup only has to configure the key once, on the plugin —
|
|
143
|
+
* not again on every hook that bypasses it.
|
|
144
|
+
*/
|
|
145
|
+
readonly ingestKey?: string;
|
|
81
146
|
/** Send a custom event. */
|
|
82
147
|
trackEvent(eventName: string, data?: Record<string, JsonValue>): Promise<void>;
|
|
83
148
|
/** Manually trigger a pageview. */
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,aAAa,GAAG,MAAM,GA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M,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/E,mCAAmC;IACnC,aAAa,IAAI,IAAI,CAAC;IACtB,kDAAkD;IAClD,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IACnF,2CAA2C;IAC3C,sBAAsB,IAAI,IAAI,CAAC;IAC/B,4CAA4C;IAC5C,uBAAuB,IAAI,IAAI,CAAC;IAChC,sDAAsD;IACtD,OAAO,IAAI,IAAI,CAAC;CACjB;AAED,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,GAAG,mBAAmB,GAAG,MAAM,CAAC;CAC/C;AAED,MAAM,WAAW,WAAW;IAC1B,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACvB"}
|
package/dist/types.js
CHANGED
package/dist/utils.d.ts
CHANGED
|
@@ -2,6 +2,34 @@ import type { JsonValue } from "./types";
|
|
|
2
2
|
export declare function getRequestId(): string | undefined;
|
|
3
3
|
export declare function isLocalhostLike(): boolean;
|
|
4
4
|
export declare function isTestEnvironment(): boolean;
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Request headers for a `fetch`-based ingest call. Returns an empty object
|
|
7
|
+
* when no ingest key is configured, so a Temps-hosted app sends exactly the
|
|
8
|
+
* headers it always has and keeps resolving by `Host`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function ingestKeyHeaders(ingestKey?: string): Record<string, string>;
|
|
11
|
+
/**
|
|
12
|
+
* Appends the ingest key to a URL as `?temps_key=…` for transports that cannot
|
|
13
|
+
* set headers (`navigator.sendBeacon`). Returns the URL untouched when no key
|
|
14
|
+
* is configured.
|
|
15
|
+
*/
|
|
16
|
+
export declare function withIngestKey(url: string, ingestKey?: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* Wraps a JSON payload for `navigator.sendBeacon`.
|
|
19
|
+
*
|
|
20
|
+
* Beacons always use credentials mode `include`. A beacon whose Blob type is
|
|
21
|
+
* not CORS-safelisted (`application/json` is not) is preflighted, and a
|
|
22
|
+
* cross-origin preflight for a credentialed request fails unless the server
|
|
23
|
+
* answers with a credentialed grant — so on keyed, cross-origin ingest the
|
|
24
|
+
* unload events (`page_leave`, late Web Vitals, the final replay batch) were
|
|
25
|
+
* silently dropped. `text/plain` is safelisted: the beacon goes out with no
|
|
26
|
+
* preflight, and the server parses its body as JSON exactly as before.
|
|
27
|
+
*
|
|
28
|
+
* Only keyed requests are sent as `text/plain`. The server accepts that type
|
|
29
|
+
* on the keyed path alone, and an app without a key is served by Temps on its
|
|
30
|
+
* own origin, where `application/json` was never a problem.
|
|
31
|
+
*/
|
|
32
|
+
export declare function beaconBlob(payload: string, ingestKey?: string): Blob;
|
|
33
|
+
export declare function sendAnalytics(endpoint: string, data: Record<string, JsonValue>, method: "POST" | "PUT" | "PATCH" | undefined, basePath: string, ingestKey?: string): Promise<void>;
|
|
34
|
+
export declare function sendAnalyticsReliable(endpoint: string, data: Record<string, JsonValue>, basePath: string, ingestKey?: string): boolean;
|
|
7
35
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAEzC,wBAAgB,YAAY,IAAI,MAAM,GAAG,SAAS,CAcjD;AAED,wBAAgB,eAAe,IAAI,OAAO,CASzC;AAED,wBAAgB,iBAAiB,IAAI,OAAO,CAS3C;AAoBD;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAE3E;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAIrE;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAEpE;AAED,wBAAsB,aAAa,CACjC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAC/B,MAAM,EAAE,MAAM,GAAG,KAAK,GAAG,OAAO,YAAS,EACzC,QAAQ,EAAE,MAAM,EAChB,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC,IAAI,CAAC,CAWf;AAED,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAC/B,QAAQ,EAAE,MAAM,EAChB,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CA2CT"}
|