@temps-sdk/analytics-core 0.0.5 → 0.0.7-beta.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/dist/Analytics.d.ts.map +1 -1
- package/dist/Analytics.js +156 -8
- package/dist/AutoTracking.d.ts +127 -0
- package/dist/AutoTracking.d.ts.map +1 -0
- package/dist/AutoTracking.js +207 -0
- package/dist/FeatureFlags.d.ts +130 -0
- package/dist/FeatureFlags.d.ts.map +1 -0
- package/dist/FeatureFlags.js +271 -0
- package/dist/Identify.d.ts +51 -0
- package/dist/Identify.d.ts.map +1 -0
- package/dist/Identify.js +154 -0
- package/dist/SessionRecorder.d.ts +33 -1
- package/dist/SessionRecorder.d.ts.map +1 -1
- package/dist/SessionRecorder.js +64 -4
- package/dist/SpeedTracker.d.ts +5 -1
- package/dist/SpeedTracker.d.ts.map +1 -1
- package/dist/SpeedTracker.js +7 -1
- package/dist/SurveyWidget.d.ts +18 -0
- package/dist/SurveyWidget.d.ts.map +1 -0
- package/dist/SurveyWidget.js +195 -0
- package/dist/Surveys.d.ts +138 -0
- package/dist/Surveys.d.ts.map +1 -0
- package/dist/Surveys.js +351 -0
- package/dist/identity.d.ts +49 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +126 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/request.d.ts +10 -0
- package/dist/request.d.ts.map +1 -0
- package/dist/request.js +40 -0
- package/dist/types.d.ts +89 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +16 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +19 -2
- package/package.json +7 -2
package/dist/types.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import type { AutoTrackOptions } from "./AutoTracking";
|
|
2
|
+
import type { FeatureFlags, FeatureFlagsOptions } from "./FeatureFlags";
|
|
3
|
+
import type { Surveys, SurveysOptions } from "./Surveys";
|
|
1
4
|
export type JsonPrimitive = string | number | boolean | null;
|
|
2
5
|
export type JsonValue = JsonPrimitive | JsonValue[] | {
|
|
3
6
|
[key: string]: JsonValue;
|
|
@@ -109,8 +112,36 @@ export interface AnalyticsClientOptions {
|
|
|
109
112
|
* not work on this endpoint.
|
|
110
113
|
*/
|
|
111
114
|
ingestKey?: string;
|
|
115
|
+
/**
|
|
116
|
+
* Store nothing on the visitor's device (ADR 109 D3). No visitor or session
|
|
117
|
+
* id is written to `localStorage`, ids an earlier visit stored are deleted,
|
|
118
|
+
* and session recording does not start. Turn it on together with the
|
|
119
|
+
* project's "Cookieless" identity mode in the Temps Console, which makes
|
|
120
|
+
* the server count visitors by a hash that changes every day: a returning
|
|
121
|
+
* visitor counts as new the next day. Defaults to false.
|
|
122
|
+
*/
|
|
123
|
+
cookieless?: boolean;
|
|
112
124
|
}
|
|
113
|
-
export interface AnalyticsOptions extends AnalyticsClientOptions {
|
|
125
|
+
export interface AnalyticsOptions extends AnalyticsClientOptions, AutoTrackOptions {
|
|
126
|
+
/**
|
|
127
|
+
* Feature-flag context and server-rendered bootstrap values. Flags are read
|
|
128
|
+
* lazily from `{basePath}/flags`; see `AnalyticsApi.flags`.
|
|
129
|
+
*/
|
|
130
|
+
flags?: Pick<FeatureFlagsOptions, "key" | "attributes" | "bootstrap" | "onError">;
|
|
131
|
+
/**
|
|
132
|
+
* In-app surveys defined in the Temps console (ADR 110 D1). Off unless
|
|
133
|
+
* `enabled` is true, so sites without surveys make no extra request. Needs
|
|
134
|
+
* analytics to be on: answers are recorded as analytics events.
|
|
135
|
+
*/
|
|
136
|
+
surveys?: Pick<SurveysOptions, "key" | "attributes" | "onError"> & {
|
|
137
|
+
enabled?: boolean;
|
|
138
|
+
/**
|
|
139
|
+
* Render the built-in widget when a survey's conditions are met: on load,
|
|
140
|
+
* on client-side navigation and on a triggering `trackEvent`. Defaults to
|
|
141
|
+
* true. Set false to draw your own UI with `analytics.surveys`.
|
|
142
|
+
*/
|
|
143
|
+
autoShow?: boolean;
|
|
144
|
+
};
|
|
114
145
|
/** Auto track pageviews on route changes. Defaults to true. */
|
|
115
146
|
autoTrackPageviews?: boolean;
|
|
116
147
|
/** Auto track page leave events. Defaults to true. */
|
|
@@ -133,6 +164,18 @@ export interface AnalyticsOptions extends AnalyticsClientOptions {
|
|
|
133
164
|
sessionRecordingConfig?: SessionRecordingConfig;
|
|
134
165
|
}
|
|
135
166
|
export interface AnalyticsApi {
|
|
167
|
+
/**
|
|
168
|
+
* Feature flags marked client-visible, evaluated by Temps for this visitor.
|
|
169
|
+
* `flags.get("checkout.v2", false)` is synchronous and returns the fallback
|
|
170
|
+
* until the flags arrive; nothing is fetched until the first read.
|
|
171
|
+
*/
|
|
172
|
+
readonly flags: FeatureFlags;
|
|
173
|
+
/**
|
|
174
|
+
* Surveys this visitor may see. Disabled (empty) unless
|
|
175
|
+
* `surveys.enabled` was set; with `autoShow` off, render them yourself
|
|
176
|
+
* from `eligible()` and record answers with `submit()`.
|
|
177
|
+
*/
|
|
178
|
+
readonly surveys: Surveys;
|
|
136
179
|
/** Whether analytics are currently enabled. */
|
|
137
180
|
readonly enabled: boolean;
|
|
138
181
|
/**
|
|
@@ -147,8 +190,37 @@ export interface AnalyticsApi {
|
|
|
147
190
|
trackEvent(eventName: string, data?: Record<string, JsonValue>): Promise<void>;
|
|
148
191
|
/** Manually trigger a pageview. */
|
|
149
192
|
trackPageview(): void;
|
|
150
|
-
/**
|
|
151
|
-
|
|
193
|
+
/**
|
|
194
|
+
* Report the current page as missing: emits a `not_found` event whose
|
|
195
|
+
* `path` property is the current path. For apps that answer `200` for an
|
|
196
|
+
* unknown route, call it from the "not found" view, or put the
|
|
197
|
+
* `data-temps-404` attribute on that view and let the tracker do it.
|
|
198
|
+
*/
|
|
199
|
+
trackNotFound(data?: Record<string, JsonValue>): Promise<void>;
|
|
200
|
+
/**
|
|
201
|
+
* Attach your app's user id (and optional traits) to this visitor, e.g.
|
|
202
|
+
* right after signup or sign-in. The server labels the visitor this
|
|
203
|
+
* browser already is; it cannot name another one. Repeating the same call
|
|
204
|
+
* sends nothing. In cookieless mode it sends nothing and resolves to
|
|
205
|
+
* `cookieless_project`.
|
|
206
|
+
*/
|
|
207
|
+
identify(userId: string, traits?: Record<string, JsonValue>): Promise<IdentifyResult>;
|
|
208
|
+
/**
|
|
209
|
+
* Forget the identity on sign-out: the next event belongs to a new,
|
|
210
|
+
* anonymous visitor. Rejects with `ResetFailedError` when the server did
|
|
211
|
+
* not confirm it (its tracking cookie still names the old visitor), so
|
|
212
|
+
* retry; the ids stored in the browser are cleared either way.
|
|
213
|
+
*/
|
|
214
|
+
reset(): Promise<void>;
|
|
215
|
+
/**
|
|
216
|
+
* The visitor id this tracker stored. `undefined` in cookieless mode,
|
|
217
|
+
* before the first event, or when storage is blocked. A backend on Temps
|
|
218
|
+
* does not need it: the Node SDK's `analytics.identify(request, …)` reads
|
|
219
|
+
* the proxy's `_temps_visitor_id` cookie from the request. It is for a
|
|
220
|
+
* backend calling with a personal API key (`visitorId` option), since
|
|
221
|
+
* deployment tokens only accept that sealed cookie value.
|
|
222
|
+
*/
|
|
223
|
+
getVisitorId(): string | undefined;
|
|
152
224
|
/** Enable session recording at runtime. */
|
|
153
225
|
enableSessionRecording(): void;
|
|
154
226
|
/** Disable session recording at runtime. */
|
|
@@ -170,4 +242,18 @@ export interface SpeedMetric {
|
|
|
170
242
|
path?: string | null;
|
|
171
243
|
query?: string | null;
|
|
172
244
|
}
|
|
245
|
+
/** What `identify()` did. */
|
|
246
|
+
export interface IdentifyResult {
|
|
247
|
+
/** Whether the visitor now carries this identity. */
|
|
248
|
+
applied: boolean;
|
|
249
|
+
/**
|
|
250
|
+
* The server's outcome (`updated`, `unchanged`, `visitor_not_found`,
|
|
251
|
+
* `cookieless_project`, `browser_identify_refused`,
|
|
252
|
+
* `server_identity_kept`), or a local one when nothing was sent:
|
|
253
|
+
* `already_sent`, `invalid_user_id`, `network_error`, `http_<status>`,
|
|
254
|
+
* or `reset` when `reset()` ran while it waited for the page view.
|
|
255
|
+
*/
|
|
256
|
+
outcome: string;
|
|
257
|
+
message: string;
|
|
258
|
+
}
|
|
173
259
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAC7D,MAAM,MAAM,SAAS,GAAG,aAAa,GAAG,SAAS,EAAE,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAEnF,MAAM,WAAW,kBAAkB;IACjC,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;CACxC;AAED,MAAM,WAAW,sBAAsB;IACrC;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB;;;;;OAKG;IACH,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC,wEAAwE;IACxE,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,yCAAyC;IACzC,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,gEAAgE;IAChE,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,iEAAiE;IACjE,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,mEAAmE;IACnE,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,qDAAqD;IACrD,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,iDAAiD;IACjD,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,iEAAiE;IACj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AAC;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;AAED,6BAA6B;AAC7B,MAAM,WAAW,cAAc;IAC7B,qDAAqD;IACrD,OAAO,EAAE,OAAO,CAAC;IACjB;;;;;;OAMG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;CACjB"}
|
package/dist/utils.d.ts
CHANGED
|
@@ -14,6 +14,22 @@ export declare function ingestKeyHeaders(ingestKey?: string): Record<string, str
|
|
|
14
14
|
* is configured.
|
|
15
15
|
*/
|
|
16
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;
|
|
17
33
|
export declare function sendAnalytics(endpoint: string, data: Record<string, JsonValue>, method: "POST" | "PUT" | "PATCH" | undefined, basePath: string, ingestKey?: string): Promise<void>;
|
|
18
34
|
export declare function sendAnalyticsReliable(endpoint: string, data: Record<string, JsonValue>, basePath: string, ingestKey?: string): boolean;
|
|
19
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":"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,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,
|
|
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"}
|
package/dist/utils.js
CHANGED
|
@@ -77,6 +77,24 @@ export function withIngestKey(url, ingestKey) {
|
|
|
77
77
|
const separator = url.includes("?") ? "&" : "?";
|
|
78
78
|
return `${url}${separator}${INGEST_KEY_QUERY_PARAM}=${encodeURIComponent(ingestKey)}`;
|
|
79
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Wraps a JSON payload for `navigator.sendBeacon`.
|
|
82
|
+
*
|
|
83
|
+
* Beacons always use credentials mode `include`. A beacon whose Blob type is
|
|
84
|
+
* not CORS-safelisted (`application/json` is not) is preflighted, and a
|
|
85
|
+
* cross-origin preflight for a credentialed request fails unless the server
|
|
86
|
+
* answers with a credentialed grant — so on keyed, cross-origin ingest the
|
|
87
|
+
* unload events (`page_leave`, late Web Vitals, the final replay batch) were
|
|
88
|
+
* silently dropped. `text/plain` is safelisted: the beacon goes out with no
|
|
89
|
+
* preflight, and the server parses its body as JSON exactly as before.
|
|
90
|
+
*
|
|
91
|
+
* Only keyed requests are sent as `text/plain`. The server accepts that type
|
|
92
|
+
* on the keyed path alone, and an app without a key is served by Temps on its
|
|
93
|
+
* own origin, where `application/json` was never a problem.
|
|
94
|
+
*/
|
|
95
|
+
export function beaconBlob(payload, ingestKey) {
|
|
96
|
+
return new Blob([payload], { type: ingestKey ? "text/plain" : "application/json" });
|
|
97
|
+
}
|
|
80
98
|
export async function sendAnalytics(endpoint, data, method = "POST", basePath, ingestKey) {
|
|
81
99
|
try {
|
|
82
100
|
await fetch(`${basePath}/${endpoint}`, {
|
|
@@ -107,8 +125,7 @@ export function sendAnalyticsReliable(endpoint, data, basePath, ingestKey) {
|
|
|
107
125
|
navigator.sendBeacon &&
|
|
108
126
|
typeof navigator.sendBeacon === "function") {
|
|
109
127
|
try {
|
|
110
|
-
|
|
111
|
-
if (navigator.sendBeacon(url, blob))
|
|
128
|
+
if (navigator.sendBeacon(url, beaconBlob(payload, ingestKey)))
|
|
112
129
|
return true;
|
|
113
130
|
}
|
|
114
131
|
catch {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@temps-sdk/analytics-core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7-beta.1",
|
|
4
4
|
"description": "Framework-agnostic core for Temps analytics, engagement, Web Vitals, and session replay.",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|
|
@@ -40,5 +40,10 @@
|
|
|
40
40
|
"typescript": "^5.5.4",
|
|
41
41
|
"vitest": "^4.1.0"
|
|
42
42
|
},
|
|
43
|
-
"sideEffects": false
|
|
43
|
+
"sideEffects": false,
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "git+https://github.com/gotempsh/temps.git",
|
|
47
|
+
"directory": "sdks/node/packages/analytics-core"
|
|
48
|
+
}
|
|
44
49
|
}
|