@plasius/analytics 1.3.1 → 1.3.3
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 +10 -1
- package/dist/index.cjs +422 -246
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +110 -10
- package/dist/index.js.map +1 -1
- package/dist/metrics.cjs +121 -7
- package/dist/metrics.cjs.map +1 -1
- package/dist/metrics.js +92 -4
- package/dist/metrics.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-5BHE32SK.cjs +0 -98
- package/dist/chunk-5BHE32SK.cjs.map +0 -1
- package/dist/chunk-OEVYIQET.js +0 -98
- package/dist/chunk-OEVYIQET.js.map +0 -1
package/dist/index.d.cts
CHANGED
|
@@ -103,6 +103,8 @@ interface AnalyticsTransportRequest {
|
|
|
103
103
|
body: string;
|
|
104
104
|
headers: Record<string, string>;
|
|
105
105
|
keepalive: boolean;
|
|
106
|
+
/** Aborted when the host discards this client after consent withdrawal. */
|
|
107
|
+
signal?: AbortSignal;
|
|
106
108
|
}
|
|
107
109
|
type AnalyticsTransport = (request: AnalyticsTransportRequest) => Promise<void>;
|
|
108
110
|
interface LocalSpaceAnalyticsConfig {
|
|
@@ -152,7 +154,10 @@ interface LocalSpaceAnalyticsClient {
|
|
|
152
154
|
flush: () => Promise<void>;
|
|
153
155
|
updateConfig: (config: Partial<LocalSpaceAnalyticsConfig>) => void;
|
|
154
156
|
getConfig: () => Readonly<ResolvedLocalSpaceAnalyticsConfig>;
|
|
155
|
-
|
|
157
|
+
/** Stop the client; discard also cancels transport and clears retained data. */
|
|
158
|
+
destroy: (options?: {
|
|
159
|
+
discard?: boolean;
|
|
160
|
+
}) => void;
|
|
156
161
|
}
|
|
157
162
|
|
|
158
163
|
declare function createLocalSpaceAnalyticsClient(config: LocalSpaceAnalyticsConfig): LocalSpaceAnalyticsClient;
|
package/dist/index.d.ts
CHANGED
|
@@ -103,6 +103,8 @@ interface AnalyticsTransportRequest {
|
|
|
103
103
|
body: string;
|
|
104
104
|
headers: Record<string, string>;
|
|
105
105
|
keepalive: boolean;
|
|
106
|
+
/** Aborted when the host discards this client after consent withdrawal. */
|
|
107
|
+
signal?: AbortSignal;
|
|
106
108
|
}
|
|
107
109
|
type AnalyticsTransport = (request: AnalyticsTransportRequest) => Promise<void>;
|
|
108
110
|
interface LocalSpaceAnalyticsConfig {
|
|
@@ -152,7 +154,10 @@ interface LocalSpaceAnalyticsClient {
|
|
|
152
154
|
flush: () => Promise<void>;
|
|
153
155
|
updateConfig: (config: Partial<LocalSpaceAnalyticsConfig>) => void;
|
|
154
156
|
getConfig: () => Readonly<ResolvedLocalSpaceAnalyticsConfig>;
|
|
155
|
-
|
|
157
|
+
/** Stop the client; discard also cancels transport and clears retained data. */
|
|
158
|
+
destroy: (options?: {
|
|
159
|
+
discard?: boolean;
|
|
160
|
+
}) => void;
|
|
156
161
|
}
|
|
157
162
|
|
|
158
163
|
declare function createLocalSpaceAnalyticsClient(config: LocalSpaceAnalyticsConfig): LocalSpaceAnalyticsClient;
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
USEFUL_METRIC_EVENT_DEFINITIONS,
|
|
3
|
-
projectUsefulMetric
|
|
4
|
-
} from "./chunk-OEVYIQET.js";
|
|
5
|
-
|
|
6
1
|
// src/core/client.ts
|
|
7
2
|
import { createSchema, field } from "@plasius/schema";
|
|
8
3
|
var DEFAULT_FLUSH_INTERVAL_MS = 5e3;
|
|
@@ -220,7 +215,8 @@ async function defaultTransport({
|
|
|
220
215
|
endpoint,
|
|
221
216
|
body,
|
|
222
217
|
headers,
|
|
223
|
-
keepalive
|
|
218
|
+
keepalive,
|
|
219
|
+
signal
|
|
224
220
|
}) {
|
|
225
221
|
if (typeof fetch !== "function") {
|
|
226
222
|
return;
|
|
@@ -229,7 +225,8 @@ async function defaultTransport({
|
|
|
229
225
|
method: "POST",
|
|
230
226
|
headers,
|
|
231
227
|
body,
|
|
232
|
-
keepalive
|
|
228
|
+
keepalive,
|
|
229
|
+
signal
|
|
233
230
|
});
|
|
234
231
|
if (!response.ok) {
|
|
235
232
|
throw new Error(`Analytics transport failed with status ${response.status}.`);
|
|
@@ -783,6 +780,7 @@ function createLocalSpaceAnalyticsClient(config) {
|
|
|
783
780
|
let isFlushing = false;
|
|
784
781
|
let flushQueuedAfterCurrent = false;
|
|
785
782
|
let isDestroyed = false;
|
|
783
|
+
const transportController = new AbortController();
|
|
786
784
|
const issueAggregates = /* @__PURE__ */ new Map();
|
|
787
785
|
const persistQueue = () => writeQueue(resolvedConfig.storageKey, queue);
|
|
788
786
|
const trimQueue = () => {
|
|
@@ -949,12 +947,14 @@ function createLocalSpaceAnalyticsClient(config) {
|
|
|
949
947
|
body,
|
|
950
948
|
headers: resolvedConfig.headers,
|
|
951
949
|
// Normal flushes do not need fetch keepalive; unload/pagehide uses sendBeacon.
|
|
952
|
-
keepalive: false
|
|
950
|
+
keepalive: false,
|
|
951
|
+
signal: transportController.signal
|
|
953
952
|
});
|
|
953
|
+
if (transportController.signal.aborted) return;
|
|
954
954
|
queue = queue.slice(events.length);
|
|
955
955
|
persistQueue();
|
|
956
956
|
} catch (error) {
|
|
957
|
-
resolvedConfig.onError?.(error);
|
|
957
|
+
if (!transportController.signal.aborted) resolvedConfig.onError?.(error);
|
|
958
958
|
} finally {
|
|
959
959
|
isFlushing = false;
|
|
960
960
|
if (flushQueuedAfterCurrent) {
|
|
@@ -1127,7 +1127,14 @@ function createLocalSpaceAnalyticsClient(config) {
|
|
|
1127
1127
|
getConfig() {
|
|
1128
1128
|
return resolvedConfig;
|
|
1129
1129
|
},
|
|
1130
|
-
destroy() {
|
|
1130
|
+
destroy(options) {
|
|
1131
|
+
if (options?.discard) {
|
|
1132
|
+
transportController.abort();
|
|
1133
|
+
queue = [];
|
|
1134
|
+
flushQueuedAfterCurrent = false;
|
|
1135
|
+
issueAggregates.clear();
|
|
1136
|
+
writeQueue(resolvedConfig.storageKey, []);
|
|
1137
|
+
}
|
|
1131
1138
|
if (isDestroyed) {
|
|
1132
1139
|
return;
|
|
1133
1140
|
}
|
|
@@ -1934,6 +1941,99 @@ var SemanticJourneyAggregateStore = class {
|
|
|
1934
1941
|
}
|
|
1935
1942
|
};
|
|
1936
1943
|
|
|
1944
|
+
// src/journey/metrics.ts
|
|
1945
|
+
var durationBuckets = [
|
|
1946
|
+
[100, "lt100ms"],
|
|
1947
|
+
[250, "lt250ms"],
|
|
1948
|
+
[500, "lt500ms"],
|
|
1949
|
+
[1e3, "lt1000ms"],
|
|
1950
|
+
[2500, "lt2500ms"],
|
|
1951
|
+
[5e3, "lt5000ms"],
|
|
1952
|
+
[1e4, "lt10000ms"],
|
|
1953
|
+
[Infinity, "gte10000ms"]
|
|
1954
|
+
];
|
|
1955
|
+
var duration = (category) => ({
|
|
1956
|
+
category,
|
|
1957
|
+
outcome: "unknown",
|
|
1958
|
+
buckets: durationBuckets,
|
|
1959
|
+
maximum: 864e5
|
|
1960
|
+
});
|
|
1961
|
+
var metrics = /* @__PURE__ */ (() => ({
|
|
1962
|
+
"page.load": duration("presentation"),
|
|
1963
|
+
"route.ready": duration("presentation"),
|
|
1964
|
+
"world.ready": duration("presentation"),
|
|
1965
|
+
"request.duration": duration("request"),
|
|
1966
|
+
"vital.lcp": duration("presentation"),
|
|
1967
|
+
"vital.fcp": duration("presentation"),
|
|
1968
|
+
"vital.inp": duration("interaction"),
|
|
1969
|
+
"vital.ttfb": duration("request"),
|
|
1970
|
+
"vital.cls": {
|
|
1971
|
+
category: "presentation",
|
|
1972
|
+
outcome: "unknown",
|
|
1973
|
+
maximum: 100,
|
|
1974
|
+
buckets: [[0.1, "lt01"], [0.25, "lt025"], [Infinity, "gte025"]]
|
|
1975
|
+
},
|
|
1976
|
+
"episode.active-duration": {
|
|
1977
|
+
category: "state",
|
|
1978
|
+
outcome: "unknown",
|
|
1979
|
+
maximum: 864e5,
|
|
1980
|
+
buckets: [
|
|
1981
|
+
[1e4, "lt10s"],
|
|
1982
|
+
[6e4, "lt60s"],
|
|
1983
|
+
[3e5, "lt300s"],
|
|
1984
|
+
[9e5, "lt900s"],
|
|
1985
|
+
[36e5, "lt3600s"],
|
|
1986
|
+
[Infinity, "gte3600s"]
|
|
1987
|
+
]
|
|
1988
|
+
},
|
|
1989
|
+
"episode.started": { category: "state", outcome: "unknown" },
|
|
1990
|
+
"episode.completed": { category: "state", outcome: "unknown" },
|
|
1991
|
+
"request.started": { category: "request", outcome: "unknown" },
|
|
1992
|
+
"request.completed": { category: "request", outcome: "success" },
|
|
1993
|
+
"request.failed": { category: "request", outcome: "failure" },
|
|
1994
|
+
"request.cancelled": { category: "request", outcome: "cancelled" },
|
|
1995
|
+
"request.rejected": { category: "request", outcome: "denied" },
|
|
1996
|
+
"error.runtime": { category: "error", outcome: "failure" },
|
|
1997
|
+
"error.resource": { category: "error", outcome: "failure" },
|
|
1998
|
+
"error.render": { category: "error", outcome: "failure" },
|
|
1999
|
+
"error.unhandled": { category: "error", outcome: "failure" }
|
|
2000
|
+
}))();
|
|
2001
|
+
var USEFUL_METRIC_EVENT_DEFINITIONS = /* @__PURE__ */ (() => Object.freeze(Object.fromEntries(Object.entries(metrics).flatMap(([metric, definition]) => {
|
|
2002
|
+
const names = definition.buckets ? definition.buckets.map(([, bucket]) => `metric.${metric}.${bucket}`) : [`metric.${metric}`];
|
|
2003
|
+
return names.map((name) => [name, Object.freeze({ category: definition.category })]);
|
|
2004
|
+
}))))();
|
|
2005
|
+
function projectUsefulMetric(input) {
|
|
2006
|
+
try {
|
|
2007
|
+
if (typeof input !== "object" || input === null || Array.isArray(input)) return null;
|
|
2008
|
+
const prototype = Object.getPrototypeOf(input);
|
|
2009
|
+
if (prototype !== Object.prototype && prototype !== null) return null;
|
|
2010
|
+
const keys = Reflect.ownKeys(input);
|
|
2011
|
+
if (keys.length < 1 || keys.length > 2 || keys.some((key) => key !== "metric" && key !== "value")) return null;
|
|
2012
|
+
const descriptors = Object.getOwnPropertyDescriptors(input);
|
|
2013
|
+
const metric = descriptors.metric;
|
|
2014
|
+
if (!metric || !("value" in metric) || typeof metric.value !== "string" || !Object.hasOwn(metrics, metric.value)) return null;
|
|
2015
|
+
const definition = metrics[metric.value];
|
|
2016
|
+
let name = `metric.${metric.value}`;
|
|
2017
|
+
if (definition.buckets) {
|
|
2018
|
+
const value = descriptors.value;
|
|
2019
|
+
if (keys.length !== 2 || !value || !("value" in value) || typeof value.value !== "number" || !Number.isFinite(value.value) || value.value < 0 || value.value > definition.maximum) return null;
|
|
2020
|
+
const bucket = definition.buckets.find(([upper]) => value.value < upper);
|
|
2021
|
+
name += `.${bucket[1]}`;
|
|
2022
|
+
} else if (keys.length !== 1) {
|
|
2023
|
+
return null;
|
|
2024
|
+
}
|
|
2025
|
+
return Object.freeze({
|
|
2026
|
+
name,
|
|
2027
|
+
category: definition.category,
|
|
2028
|
+
phase: metric.value.endsWith(".started") ? "start" : "end",
|
|
2029
|
+
outcome: definition.outcome,
|
|
2030
|
+
modality: "system"
|
|
2031
|
+
});
|
|
2032
|
+
} catch {
|
|
2033
|
+
return null;
|
|
2034
|
+
}
|
|
2035
|
+
}
|
|
2036
|
+
|
|
1937
2037
|
// src/journey/transport.ts
|
|
1938
2038
|
var SemanticJourneyTransportError = class extends Error {
|
|
1939
2039
|
constructor(code, retryable, status, retryAfterMs) {
|