@temps-sdk/react-analytics 0.0.2 → 0.0.4

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
@@ -1,5 +1,5 @@
1
1
  <p align="center">
2
- <img src="https://raw.githubusercontent.com/AnomalyCo/temps/main/temps-demo.gif" alt="Temps Platform" width="700" />
2
+ <img src="https://raw.githubusercontent.com/gotempsh/temps/refs/heads/main/web/public/logo/temps-logo-light.png" alt="Temps Platform" width="700" />
3
3
  </p>
4
4
 
5
5
  <h1 align="center">@temps-sdk/react-analytics</h1>
@@ -1,55 +1,2 @@
1
- export interface EngagementTrackerOptions {
2
- basePath?: string;
3
- domain?: string;
4
- heartbeatInterval?: number;
5
- inactivityTimeout?: number;
6
- engagementThreshold?: number;
7
- onHeartbeat?: (data: EngagementData) => void;
8
- onPageLeave?: (data: EngagementData) => void;
9
- }
10
- export interface EngagementData {
11
- engagement_time_seconds: number;
12
- total_time_seconds: number;
13
- heartbeat_count: number;
14
- is_engaged: boolean;
15
- is_visible: boolean;
16
- time_since_last_activity: number;
17
- }
18
- export declare class EngagementTracker {
19
- private startTime;
20
- private engagementTime;
21
- private lastActivityTime;
22
- private lastEngagementStart;
23
- private heartbeatCount;
24
- private heartbeatInterval;
25
- private inactivityTimeout;
26
- private engagementThreshold;
27
- private isVisible;
28
- private isFocused;
29
- private isActive;
30
- private heartbeatTimer;
31
- private inactivityTimer;
32
- private hasTrackedLeave;
33
- private readonly basePath;
34
- private readonly domain;
35
- private readonly onHeartbeat?;
36
- private readonly onPageLeave?;
37
- constructor(options?: EngagementTrackerOptions);
38
- private setupEventListeners;
39
- private handleVisibilityChange;
40
- private handleFocus;
41
- private handleBlur;
42
- private pauseEngagement;
43
- private handleUserActivity;
44
- private resetInactivityTimer;
45
- private updateEngagementTime;
46
- private startHeartbeat;
47
- private stopHeartbeat;
48
- private sendHeartbeat;
49
- private handlePageLeave;
50
- private handleNavigation;
51
- private getEngagementData;
52
- private reset;
53
- destroy(): void;
54
- }
1
+ export { EngagementTracker, type EngagementTrackerOptions, type EngagementData, } from "@temps-sdk/analytics-core";
55
2
  //# sourceMappingURL=EngagementTracker.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"EngagementTracker.d.ts","sourceRoot":"","sources":["../src/EngagementTracker.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,wBAAwB;IACvC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,cAAc,KAAK,IAAI,CAAC;IAC7C,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,cAAc,KAAK,IAAI,CAAC;CAC9C;AAED,MAAM,WAAW,cAAc;IAC7B,uBAAuB,EAAE,MAAM,CAAC;IAChC,kBAAkB,EAAE,MAAM,CAAC;IAC3B,eAAe,EAAE,MAAM,CAAC;IACxB,UAAU,EAAE,OAAO,CAAC;IACpB,UAAU,EAAE,OAAO,CAAC;IACpB,wBAAwB,EAAE,MAAM,CAAC;CAClC;AAED,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,cAAc,CAAa;IACnC,OAAO,CAAC,gBAAgB,CAAS;IACjC,OAAO,CAAC,mBAAmB,CAAuB;IAClD,OAAO,CAAC,cAAc,CAAa;IACnC,OAAO,CAAC,iBAAiB,CAAS;IAClC,OAAO,CAAC,iBAAiB,CAAS;IAClC,OAAO,CAAC,mBAAmB,CAAS;IACpC,OAAO,CAAC,SAAS,CAAU;IAC3B,OAAO,CAAC,SAAS,CAAU;IAC3B,OAAO,CAAC,QAAQ,CAAiB;IACjC,OAAO,CAAC,cAAc,CAA+C;IACrE,OAAO,CAAC,eAAe,CAA8C;IACrE,OAAO,CAAC,eAAe,CAAkB;IACzC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAiC;IAC9D,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAiC;gBAElD,OAAO,GAAE,wBAA6B;IAsBlD,OAAO,CAAC,mBAAmB;IA6B3B,OAAO,CAAC,sBAAsB,CAW5B;IAEF,OAAO,CAAC,WAAW,CAEjB;IAEF,OAAO,CAAC,UAAU,CAIhB;IAEF,OAAO,CAAC,eAAe;IAQvB,OAAO,CAAC,kBAAkB,CAexB;IAEF,OAAO,CAAC,oBAAoB;IAc5B,OAAO,CAAC,oBAAoB;IAO5B,OAAO,CAAC,cAAc;IAWtB,OAAO,CAAC,aAAa;YAOP,aAAa;IA+B3B,OAAO,CAAC,eAAe,CAyBrB;IAEF,OAAO,CAAC,gBAAgB,CAGtB;IAEF,OAAO,CAAC,iBAAiB;IAczB,OAAO,CAAC,KAAK;IAeN,OAAO,IAAI,IAAI;CAoBvB"}
1
+ {"version":3,"file":"EngagementTracker.d.ts","sourceRoot":"","sources":["../src/EngagementTracker.ts"],"names":[],"mappings":"AACA,OAAO,EACL,iBAAiB,EACjB,KAAK,wBAAwB,EAC7B,KAAK,cAAc,GACpB,MAAM,2BAA2B,CAAC"}
@@ -1,223 +1,2 @@
1
1
  "use client";
2
- import { sendAnalytics, sendAnalyticsReliable } from "./utils";
3
- export class EngagementTracker {
4
- constructor(options = {}) {
5
- this.engagementTime = 0;
6
- this.lastEngagementStart = null;
7
- this.heartbeatCount = 0;
8
- this.isActive = true;
9
- this.heartbeatTimer = null;
10
- this.inactivityTimer = null;
11
- this.hasTrackedLeave = false;
12
- this.handleVisibilityChange = () => {
13
- const wasVisible = this.isVisible;
14
- this.isVisible = document.visibilityState === "visible";
15
- if (!wasVisible && this.isVisible) {
16
- // Tab became visible - but don't start engagement until user interacts
17
- this.startHeartbeat();
18
- }
19
- else if (wasVisible && !this.isVisible) {
20
- // Tab became hidden - pause engagement
21
- this.pauseEngagement();
22
- }
23
- };
24
- this.handleFocus = () => {
25
- this.isFocused = true;
26
- };
27
- this.handleBlur = () => {
28
- this.isFocused = false;
29
- // When window loses focus, pause engagement tracking
30
- this.pauseEngagement();
31
- };
32
- this.handleUserActivity = () => {
33
- // Only track activity if tab is visible AND focused
34
- if (!this.isVisible || !this.isFocused)
35
- return;
36
- const now = Date.now();
37
- if (!this.isActive) {
38
- // Resume from inactive state
39
- this.isActive = true;
40
- this.lastEngagementStart = now;
41
- this.startHeartbeat();
42
- }
43
- this.lastActivityTime = now;
44
- this.resetInactivityTimer();
45
- };
46
- this.handlePageLeave = () => {
47
- if (this.hasTrackedLeave)
48
- return;
49
- this.hasTrackedLeave = true;
50
- this.updateEngagementTime();
51
- const data = this.getEngagementData();
52
- if (this.onPageLeave) {
53
- this.onPageLeave(data);
54
- }
55
- sendAnalyticsReliable("event", {
56
- event_name: "page_leave",
57
- request_path: window.location.pathname,
58
- request_query: window.location.search,
59
- domain: this.domain,
60
- event_data: {
61
- engagement_time_seconds: data.engagement_time_seconds,
62
- total_time_seconds: data.total_time_seconds,
63
- heartbeat_count: this.heartbeatCount,
64
- was_engaged: data.is_engaged,
65
- url: window.location.href,
66
- referrer: document.referrer,
67
- },
68
- }, this.basePath);
69
- };
70
- this.handleNavigation = () => {
71
- this.handlePageLeave();
72
- this.reset();
73
- };
74
- this.basePath = options.basePath || "/api/_temps";
75
- this.domain = options.domain || window.location.hostname;
76
- this.heartbeatInterval = options.heartbeatInterval || 30000; // 30 seconds
77
- this.inactivityTimeout = options.inactivityTimeout || 30000; // 30 seconds
78
- this.engagementThreshold = options.engagementThreshold || 10000; // 10 seconds
79
- this.onHeartbeat = options.onHeartbeat;
80
- this.onPageLeave = options.onPageLeave;
81
- this.startTime = Date.now();
82
- this.lastActivityTime = Date.now();
83
- this.isVisible = document.visibilityState === "visible";
84
- this.isFocused = document.hasFocus();
85
- // Don't start engagement tracking until first user interaction
86
- this.lastEngagementStart = null;
87
- this.isActive = false;
88
- this.setupEventListeners();
89
- this.startHeartbeat();
90
- }
91
- setupEventListeners() {
92
- // Visibility change
93
- document.addEventListener("visibilitychange", this.handleVisibilityChange);
94
- // Focus/blur for better multi-monitor support
95
- window.addEventListener("focus", this.handleFocus);
96
- window.addEventListener("blur", this.handleBlur);
97
- // User activity tracking - use more specific events to avoid false positives
98
- const activityEvents = ["mousedown", "keypress", "scroll", "touchstart", "click"];
99
- activityEvents.forEach(event => {
100
- document.addEventListener(event, this.handleUserActivity, { passive: true });
101
- });
102
- // Page leave events
103
- window.addEventListener("pagehide", this.handlePageLeave);
104
- window.addEventListener("beforeunload", this.handlePageLeave);
105
- // SPA navigation
106
- const originalPushState = window.history.pushState;
107
- window.history.pushState = (...args) => {
108
- this.handlePageLeave();
109
- originalPushState.apply(window.history, args);
110
- this.reset();
111
- };
112
- window.addEventListener("popstate", this.handleNavigation);
113
- }
114
- pauseEngagement() {
115
- this.updateEngagementTime();
116
- this.lastEngagementStart = null;
117
- if (!this.isVisible) {
118
- this.stopHeartbeat();
119
- }
120
- }
121
- resetInactivityTimer() {
122
- if (this.inactivityTimer) {
123
- clearTimeout(this.inactivityTimer);
124
- }
125
- this.inactivityTimer = setTimeout(() => {
126
- if (this.isVisible) {
127
- this.updateEngagementTime();
128
- this.isActive = false;
129
- this.lastEngagementStart = null;
130
- }
131
- }, this.inactivityTimeout);
132
- }
133
- updateEngagementTime() {
134
- if (this.lastEngagementStart !== null) {
135
- const currentEngagement = Date.now() - this.lastEngagementStart;
136
- this.engagementTime += currentEngagement;
137
- }
138
- }
139
- startHeartbeat() {
140
- if (this.heartbeatTimer)
141
- return;
142
- this.heartbeatTimer = setInterval(() => {
143
- // Only send heartbeat if tab is visible AND user has been active
144
- if (this.isVisible && this.isActive) {
145
- this.sendHeartbeat();
146
- }
147
- }, this.heartbeatInterval);
148
- }
149
- stopHeartbeat() {
150
- if (this.heartbeatTimer) {
151
- clearInterval(this.heartbeatTimer);
152
- this.heartbeatTimer = null;
153
- }
154
- }
155
- async sendHeartbeat() {
156
- this.updateEngagementTime();
157
- this.heartbeatCount++;
158
- const data = this.getEngagementData();
159
- if (this.onHeartbeat) {
160
- this.onHeartbeat(data);
161
- }
162
- await sendAnalytics("event", {
163
- event_name: "heartbeat",
164
- request_path: window.location.pathname,
165
- request_query: window.location.search,
166
- domain: this.domain,
167
- event_data: {
168
- engagement_time: Math.round(this.engagementTime),
169
- total_time: Math.round(Date.now() - this.startTime),
170
- is_engaged: data.is_engaged,
171
- is_visible: this.isVisible,
172
- time_since_last_activity: data.time_since_last_activity,
173
- heartbeat_count: this.heartbeatCount,
174
- },
175
- }, "POST", this.basePath);
176
- // Reset engagement start for next period
177
- if (this.isActive && this.isVisible) {
178
- this.lastEngagementStart = Date.now();
179
- }
180
- }
181
- getEngagementData() {
182
- const totalTime = Date.now() - this.startTime;
183
- const timeSinceLastActivity = Date.now() - this.lastActivityTime;
184
- return {
185
- engagement_time_seconds: Math.round(this.engagementTime / 1000),
186
- total_time_seconds: Math.round(totalTime / 1000),
187
- heartbeat_count: this.heartbeatCount,
188
- is_engaged: this.engagementTime >= this.engagementThreshold,
189
- is_visible: this.isVisible,
190
- time_since_last_activity: Math.round(timeSinceLastActivity / 1000),
191
- };
192
- }
193
- reset() {
194
- this.startTime = Date.now();
195
- this.engagementTime = 0;
196
- this.lastActivityTime = Date.now();
197
- this.heartbeatCount = 0;
198
- this.hasTrackedLeave = false;
199
- // Reset to inactive state - wait for user interaction
200
- this.isActive = false;
201
- this.lastEngagementStart = null;
202
- if (this.isVisible) {
203
- this.startHeartbeat();
204
- }
205
- }
206
- destroy() {
207
- this.handlePageLeave();
208
- this.stopHeartbeat();
209
- if (this.inactivityTimer) {
210
- clearTimeout(this.inactivityTimer);
211
- }
212
- document.removeEventListener("visibilitychange", this.handleVisibilityChange);
213
- window.removeEventListener("focus", this.handleFocus);
214
- window.removeEventListener("blur", this.handleBlur);
215
- window.removeEventListener("pagehide", this.handlePageLeave);
216
- window.removeEventListener("beforeunload", this.handlePageLeave);
217
- window.removeEventListener("popstate", this.handleNavigation);
218
- const activityEvents = ["mousedown", "keypress", "scroll", "touchstart", "click"];
219
- activityEvents.forEach(event => {
220
- document.removeEventListener(event, this.handleUserActivity);
221
- });
222
- }
223
- }
2
+ export { EngagementTracker, } from "@temps-sdk/analytics-core";
@@ -1,2 +1,2 @@
1
- export declare const DEFAULT_BASE_PATH = "/api/_temps";
1
+ export { DEFAULT_BASE_PATH } from "@temps-sdk/analytics-core";
2
2
  //# sourceMappingURL=constants.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,iBAAiB,gBAAgB,CAAC"}
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC"}
package/dist/constants.js CHANGED
@@ -1 +1 @@
1
- export const DEFAULT_BASE_PATH = "/api/_temps";
1
+ export { DEFAULT_BASE_PATH } from "@temps-sdk/analytics-core";
package/dist/types.d.ts CHANGED
@@ -1,24 +1,6 @@
1
1
  import type React from "react";
2
- export type JsonPrimitive = string | number | boolean | null;
3
- export type JsonValue = JsonPrimitive | JsonValue[] | {
4
- [key: string]: JsonValue;
5
- };
6
- export interface AnalyticsEventBase {
7
- event_name: string;
8
- request_query?: string;
9
- request_path?: string;
10
- event_data?: Record<string, JsonValue>;
11
- }
12
- export interface AnalyticsClientOptions {
13
- /** Base endpoint path. Defaults to `/_temps`. */
14
- basePath?: string;
15
- /** Set to true to disable analytics (e.g., for tests). */
16
- disabled?: boolean;
17
- /** Ignore localhost/test env automatically. Defaults to true. */
18
- ignoreLocalhost?: boolean;
19
- /** Custom domain to use for analytics. Defaults to window.location.hostname. */
20
- domain?: string;
21
- }
2
+ import type { JsonValue, AnalyticsClientOptions, SessionRecordingConfig } from "@temps-sdk/analytics-core";
3
+ export type { JsonPrimitive, JsonValue, AnalyticsEventBase, AnalyticsClientOptions, WebVitalMetric, SpeedMetric, } from "@temps-sdk/analytics-core";
22
4
  export interface AnalyticsContextValue {
23
5
  /** Send a custom event. */
24
6
  trackEvent: (eventName: string, data?: Record<string, JsonValue>) => Promise<void>;
@@ -49,45 +31,8 @@ export interface TempsAnalyticsProviderProps extends AnalyticsClientOptions {
49
31
  /** Enable session recording. Defaults to false. */
50
32
  enableSessionRecording?: boolean;
51
33
  /** Session recording configuration. */
52
- sessionRecordingConfig?: {
53
- /** Paths to exclude from recording. */
54
- excludedPaths?: string[];
55
- /** Sample rate for recording sessions (0.0 to 1.0). Defaults to 1.0. */
56
- sessionSampleRate?: number;
57
- /** Mask all inputs. Defaults to true. */
58
- maskAllInputs?: boolean;
59
- /** CSS selector for masking text. Defaults to "[data-mask]". */
60
- maskTextSelector?: string;
61
- /** CSS class to block from recording. Defaults to "rr-block". */
62
- blockClass?: string;
63
- /** CSS class to ignore from recording. Defaults to "rr-ignore". */
64
- ignoreClass?: string;
65
- /** CSS class to mask text. Defaults to "rr-mask". */
66
- maskTextClass?: string;
67
- /** Record canvas elements. Defaults to false. */
68
- recordCanvas?: boolean;
69
- /** Collect fonts. Defaults to false. */
70
- collectFonts?: boolean;
71
- /** Number of events to batch before sending. Defaults to 100. */
72
- batchSize?: number;
73
- /** Interval in ms to flush events. Defaults to 5000. */
74
- flushInterval?: number;
75
- };
34
+ sessionRecordingConfig?: SessionRecordingConfig;
76
35
  /** Children to render inside the provider. */
77
36
  children: React.ReactNode;
78
37
  }
79
- export interface WebVitalMetric {
80
- value: number;
81
- rating: "good" | "needs-improvement" | "poor";
82
- }
83
- export interface SpeedMetric {
84
- ttfb?: number | null;
85
- lcp?: number | null;
86
- fid?: number | null;
87
- fcp?: number | null;
88
- cls?: number | null;
89
- inp?: number | null;
90
- path?: string | null;
91
- query?: string | null;
92
- }
93
38
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,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,iDAAiD;IACjD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,0DAA0D;IAC1D,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,iEAAiE;IACjE,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,gFAAgF;IAChF,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,qBAAqB;IACpC,2BAA2B;IAC3B,UAAU,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACnF,oDAAoD;IACpD,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IACvF,mCAAmC;IACnC,aAAa,EAAE,MAAM,IAAI,CAAC;IAC1B,+CAA+C;I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,EAAE,MAAM,IAAI,CAAC;IAC1B,+CAA+C;IAC/C,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,2BAA4B,SAAQ,sBAAsB;IACzE,+DAA+D;IAC/D,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,sDAAsD;IACtD,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,yEAAyE;IACzE,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,iEAAiE;IACjE,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC,+DAA+D;IAC/D,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,0EAA0E;IAC1E,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,0EAA0E;IAC1E,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,wGAAwG;IACxG,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,mDAAmD;IACnD,sBAAsB,CAAC,EAAE,OAAO,CAAC;IACjC,uCAAuC;IACvC,sBAAsB,CAAC,EAAE,sBAAsB,CAAC;IAChD,8CAA8C;IAC9C,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B"}
@@ -1 +1 @@
1
- {"version":3,"file":"useSpeedAnalytics.d.ts","sourceRoot":"","sources":["../src/useSpeedAnalytics.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,wBAAwB;IACvC,iDAAiD;IACjD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iEAAiE;IACjE,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,wBAAgB,iBAAiB,CAAC,OAAO,GAAE,wBAA6B,QAgEvE"}
1
+ {"version":3,"file":"useSpeedAnalytics.d.ts","sourceRoot":"","sources":["../src/useSpeedAnalytics.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,wBAAwB;IACvC,iDAAiD;IACjD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iEAAiE;IACjE,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,wBAAgB,iBAAiB,CAAC,OAAO,GAAE,wBAA6B,QAmEvE"}
@@ -16,7 +16,10 @@ export function useSpeedAnalytics(options = {}) {
16
16
  lcp: initialMetrics.LCP?.value ?? null,
17
17
  fid: initialMetrics.FID?.value ?? null,
18
18
  fcp: initialMetrics.FCP?.value ?? null,
19
- path: window.location.pathname,
19
+ // The ingest endpoint's field is `pathname`; a `path` key is not
20
+ // recognized and the page ends up NULL in storage, breaking
21
+ // per-page performance breakdowns.
22
+ pathname: window.location.pathname,
20
23
  query: window.location.search,
21
24
  };
22
25
  sendAnalytics("speed", metricsPayload, "POST", basePath);
@@ -25,7 +28,7 @@ export function useSpeedAnalytics(options = {}) {
25
28
  const sendLateMetric = (metricName, value) => {
26
29
  const payload = {
27
30
  [metricName.toLowerCase()]: value,
28
- path: window.location.pathname,
31
+ pathname: window.location.pathname,
29
32
  query: window.location.search,
30
33
  };
31
34
  sendAnalytics("speed", payload, "POST", basePath);
package/dist/utils.d.ts CHANGED
@@ -1,7 +1,2 @@
1
- import type { JsonValue } from "./types";
2
- export declare function getRequestId(): string | undefined;
3
- export declare function isLocalhostLike(): boolean;
4
- export declare function isTestEnvironment(): boolean;
5
- export declare function sendAnalytics(endpoint: string, data: Record<string, JsonValue>, method: "POST" | "PUT" | "PATCH" | undefined, basePath: string): Promise<void>;
6
- export declare function sendAnalyticsReliable(endpoint: string, data: Record<string, JsonValue>, basePath: string): boolean;
1
+ export { getRequestId, isLocalhostLike, isTestEnvironment, sendAnalytics, sendAnalyticsReliable, } from "@temps-sdk/analytics-core";
7
2
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,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,CAY3C;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,GACf,OAAO,CAAC,IAAI,CAAC,CAoBf;AAED,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAC/B,QAAQ,EAAE,MAAM,GACf,OAAO,CAqCT"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,EACL,YAAY,EACZ,eAAe,EACf,iBAAiB,EACjB,aAAa,EACb,qBAAqB,GACtB,MAAM,2BAA2B,CAAC"}
package/dist/utils.js CHANGED
@@ -1,97 +1,2 @@
1
1
  "use client";
2
- export function getRequestId() {
3
- if (typeof document === "undefined")
4
- return undefined;
5
- const metaElement = document.querySelector('meta[name="temps-metadata"]');
6
- if (metaElement) {
7
- try {
8
- const content = metaElement.getAttribute("content") || "{}";
9
- const metadata = JSON.parse(content);
10
- return metadata.request_id;
11
- }
12
- catch (error) {
13
- // eslint-disable-next-line no-console
14
- console.error("Failed to parse metadata:", error);
15
- }
16
- }
17
- return undefined;
18
- }
19
- export function isLocalhostLike() {
20
- try {
21
- const host = window.location.hostname;
22
- const isFile = window.location.protocol === "file:";
23
- const isLocalhost = /^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(host);
24
- return isFile || isLocalhost;
25
- }
26
- catch {
27
- return false;
28
- }
29
- }
30
- export function isTestEnvironment() {
31
- if (typeof window === "undefined")
32
- return false;
33
- // @ts-expect-error - non-standard globals from test frameworks
34
- const isPhantom = Boolean(window._phantom);
35
- // @ts-expect-error - non-standard globals from test frameworks
36
- const isNightmare = Boolean(window.__nightmare);
37
- const isWebdriver = Boolean(window.navigator?.webdriver);
38
- // @ts-expect-error - non-standard globals from test frameworks
39
- const isCypress = Boolean(window.Cypress);
40
- // @ts-expect-error - allow explicit override via window.__temps
41
- const allowTemps = Boolean(window.__temps);
42
- return (isPhantom || isNightmare || isWebdriver || isCypress) && !allowTemps;
43
- }
44
- export async function sendAnalytics(endpoint, data, method = "POST", basePath) {
45
- try {
46
- const enrichedData = {
47
- ...data,
48
- request_id: getRequestId(),
49
- session_id: typeof localStorage !== "undefined"
50
- ? localStorage.getItem("session_id") || undefined
51
- : undefined,
52
- };
53
- await fetch(`${basePath}/${endpoint}`, {
54
- method,
55
- headers: { "Content-Type": "application/json" },
56
- body: JSON.stringify(enrichedData),
57
- });
58
- }
59
- catch (error) {
60
- // eslint-disable-next-line no-console
61
- console.error("Failed to send analytics:", error);
62
- }
63
- }
64
- export function sendAnalyticsReliable(endpoint, data, basePath) {
65
- try {
66
- const enrichedData = {
67
- ...data,
68
- request_id: getRequestId(),
69
- session_id: typeof localStorage !== "undefined"
70
- ? localStorage.getItem("session_id") || undefined
71
- : undefined,
72
- };
73
- const url = `${basePath}/${endpoint}`;
74
- const payload = JSON.stringify(enrichedData);
75
- // Try sendBeacon first (most reliable for page unload)
76
- if (navigator.sendBeacon && typeof navigator.sendBeacon === "function") {
77
- const blob = new Blob([payload], { type: "application/json" });
78
- return navigator.sendBeacon(url, blob);
79
- }
80
- // Fallback to synchronous fetch with keepalive
81
- fetch(url, {
82
- method: "POST",
83
- headers: { "Content-Type": "application/json" },
84
- body: payload,
85
- keepalive: true,
86
- }).catch((error) => {
87
- // eslint-disable-next-line no-console
88
- console.error("Failed to send analytics (reliable):", error);
89
- });
90
- return true;
91
- }
92
- catch (error) {
93
- // eslint-disable-next-line no-console
94
- console.error("Failed to send analytics (reliable):", error);
95
- return false;
96
- }
97
- }
2
+ export { getRequestId, isLocalhostLike, isTestEnvironment, sendAnalytics, sendAnalyticsReliable, } from "@temps-sdk/analytics-core";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@temps-sdk/react-analytics",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "private": false,
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -38,6 +38,7 @@
38
38
  },
39
39
  "dependencies": {
40
40
  "@rrweb/packer": "^2.0.0-alpha.17",
41
+ "@temps-sdk/analytics-core": "0.0.2",
41
42
  "rrweb": "^2.0.0-alpha.17",
42
43
  "web-vitals": "^4.2.4"
43
44
  },
@@ -49,7 +50,7 @@
49
50
  "@types/react-dom": "^18.2.19",
50
51
  "@typescript-eslint/eslint-plugin": "^8.46.1",
51
52
  "@typescript-eslint/parser": "^8.46.1",
52
- "@vitest/coverage-v8": "^1.3.1",
53
+ "@vitest/coverage-v8": "^4.1.0",
53
54
  "eslint": "^9.37.0",
54
55
  "eslint-plugin-react": "^7.37.5",
55
56
  "eslint-plugin-react-hooks": "^7.0.0",
@@ -57,7 +58,7 @@
57
58
  "react": "^18.2.0",
58
59
  "react-dom": "^18.2.0",
59
60
  "typescript": "^5.5.4",
60
- "vitest": "^1.3.1"
61
+ "vitest": "^4.1.0"
61
62
  },
62
63
  "sideEffects": false
63
64
  }