@tacko/telemetry-core 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,22 @@
1
+ export type TelemetryConfig = {
2
+ ingestUrl: string;
3
+ app: string;
4
+ platform?: string;
5
+ environment?: string;
6
+ release?: string;
7
+ /** Flush interval in ms. Default 5000. Set 0 to disable batching. */
8
+ batchInterval?: number;
9
+ /** Max queue size before flush. Default 10. */
10
+ batchSize?: number;
11
+ };
12
+ export declare function init(c: TelemetryConfig): void;
13
+ export declare function identify(id: string | null): void;
14
+ export declare function trackEvent(name: string, properties?: Record<string, unknown>): void;
15
+ export declare function trackError(error: Error | {
16
+ message: string;
17
+ stack?: string;
18
+ }, context?: Record<string, unknown>): void;
19
+ export declare function screen(name: string): void;
20
+ export declare function getUserId(): string | null;
21
+ export declare function getConfigOrNull(): TelemetryConfig | null;
22
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,eAAe,GAAG;IAC5B,SAAS,EAAE,MAAM,CAAC;IAClB,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,qEAAqE;IACrE,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,+CAA+C;IAC/C,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAiBF,wBAAgB,IAAI,CAAC,CAAC,EAAE,eAAe,GAAG,IAAI,CAQ7C;AAED,wBAAgB,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAEhD;AAwDD,wBAAgB,UAAU,CACxB,IAAI,EAAE,MAAM,EACZ,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACnC,IAAI,CAqBN;AAED,wBAAgB,UAAU,CACxB,KAAK,EAAE,KAAK,GAAG;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,EAClD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAChC,IAAI,CAUN;AAED,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAEzC;AAED,wBAAgB,SAAS,IAAI,MAAM,GAAG,IAAI,CAEzC;AAED,wBAAgB,eAAe,IAAI,eAAe,GAAG,IAAI,CAExD"}
package/dist/index.js ADDED
@@ -0,0 +1,116 @@
1
+ let config = null;
2
+ let userId = null;
3
+ const DEFAULT_BATCH_INTERVAL = 5000;
4
+ const DEFAULT_BATCH_SIZE = 10;
5
+ const eventQueue = [];
6
+ let flushTimer = null;
7
+ export function init(c) {
8
+ config = { ...c };
9
+ const interval = c.batchInterval ?? DEFAULT_BATCH_INTERVAL;
10
+ if (interval > 0 && typeof setInterval !== "undefined") {
11
+ flushTimer = setInterval(flushEvents, interval);
12
+ const t = flushTimer;
13
+ if (typeof t.unref === "function")
14
+ t.unref();
15
+ }
16
+ }
17
+ export function identify(id) {
18
+ userId = id;
19
+ }
20
+ function getConfig() {
21
+ if (!config)
22
+ throw new Error("telemetry-core: init() must be called first");
23
+ return config;
24
+ }
25
+ function ensureUrl(url) {
26
+ const base = getConfig().ingestUrl.replace(/\/$/, "");
27
+ return `${base}${url}`;
28
+ }
29
+ async function send(path, body) {
30
+ const cfg = getConfig();
31
+ try {
32
+ const res = await fetch(ensureUrl(path), {
33
+ method: "POST",
34
+ headers: { "Content-Type": "application/json" },
35
+ body: JSON.stringify({
36
+ ...body,
37
+ app: cfg.app,
38
+ platform: cfg.platform ?? undefined,
39
+ environment: cfg.environment ?? undefined,
40
+ release: cfg.release ?? undefined,
41
+ }),
42
+ });
43
+ if (!res.ok) {
44
+ console.warn("[telemetry] ingest failed:", res.status, await res.text());
45
+ }
46
+ }
47
+ catch (e) {
48
+ console.warn("[telemetry] send error:", e);
49
+ }
50
+ }
51
+ function flushEvents() {
52
+ const cfg = getConfigOrNull();
53
+ if (!cfg || eventQueue.length === 0)
54
+ return;
55
+ const batch = eventQueue.splice(0, eventQueue.length);
56
+ const base = cfg.ingestUrl.replace(/\/$/, "");
57
+ const events = batch.map((e) => ({
58
+ app: cfg.app,
59
+ platform: cfg.platform,
60
+ environment: cfg.environment,
61
+ release: cfg.release,
62
+ name: e.name,
63
+ user_id: e.user_id ?? undefined,
64
+ session_id: e.session_id,
65
+ properties: e.properties,
66
+ }));
67
+ fetch(`${base}/ingest/batch`, {
68
+ method: "POST",
69
+ headers: { "Content-Type": "application/json" },
70
+ body: JSON.stringify({ events }),
71
+ }).catch((e) => console.warn("[telemetry] batch send error:", e));
72
+ }
73
+ export function trackEvent(name, properties) {
74
+ const cfg = getConfigOrNull();
75
+ const interval = cfg?.batchInterval ?? DEFAULT_BATCH_INTERVAL;
76
+ const batchSize = cfg?.batchSize ?? DEFAULT_BATCH_SIZE;
77
+ const item = {
78
+ name,
79
+ user_id: userId,
80
+ session_id: undefined,
81
+ properties: properties ?? undefined,
82
+ };
83
+ if (interval > 0 && typeof setInterval !== "undefined") {
84
+ eventQueue.push(item);
85
+ if (eventQueue.length >= batchSize)
86
+ flushEvents();
87
+ }
88
+ else {
89
+ send("/ingest/event", {
90
+ name: item.name,
91
+ user_id: item.user_id ?? undefined,
92
+ session_id: item.session_id,
93
+ properties: item.properties,
94
+ }).catch(() => { });
95
+ }
96
+ }
97
+ export function trackError(error, context) {
98
+ const message = error instanceof Error ? error.message : error.message;
99
+ const stack = error instanceof Error ? error.stack : error.stack;
100
+ send("/ingest/error", {
101
+ message,
102
+ stack: stack ?? undefined,
103
+ context: context ?? undefined,
104
+ user_id: userId ?? undefined,
105
+ session_id: undefined,
106
+ }).catch(() => { });
107
+ }
108
+ export function screen(name) {
109
+ trackEvent("$screen", { name });
110
+ }
111
+ export function getUserId() {
112
+ return userId;
113
+ }
114
+ export function getConfigOrNull() {
115
+ return config;
116
+ }
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@tacko/telemetry-core",
3
+ "version": "1.0.0",
4
+ "description": "Lightweight telemetry client: events, errors, sessions. Framework-agnostic core.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/unjica/telemetry-tracker.git",
9
+ "directory": "packages/telemetry-core"
10
+ },
11
+ "type": "module",
12
+ "main": "dist/index.js",
13
+ "types": "dist/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "import": "./dist/index.js",
17
+ "types": "./dist/index.d.ts"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "keywords": [
24
+ "telemetry",
25
+ "analytics",
26
+ "events",
27
+ "errors",
28
+ "tracking"
29
+ ],
30
+ "devDependencies": {
31
+ "typescript": "^5.6.3"
32
+ },
33
+ "scripts": {
34
+ "build": "tsc",
35
+ "dev": "tsc --watch"
36
+ }
37
+ }