@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 +1 -1
- package/dist/EngagementTracker.d.ts +1 -54
- package/dist/EngagementTracker.d.ts.map +1 -1
- package/dist/EngagementTracker.js +1 -222
- package/dist/constants.d.ts +1 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +1 -1
- package/dist/types.d.ts +3 -58
- package/dist/types.d.ts.map +1 -1
- package/dist/useSpeedAnalytics.d.ts.map +1 -1
- package/dist/useSpeedAnalytics.js +5 -2
- package/dist/utils.d.ts +1 -6
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +1 -96
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<img src="https://raw.githubusercontent.com/
|
|
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
|
|
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":"
|
|
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
|
-
|
|
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";
|
package/dist/constants.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { DEFAULT_BASE_PATH } from "@temps-sdk/analytics-core";
|
|
2
2
|
//# sourceMappingURL=constants.d.ts.map
|
package/dist/constants.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,
|
|
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
|
|
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
|
-
|
|
3
|
-
export type 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
|
package/dist/types.d.ts.map
CHANGED
|
@@ -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,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,KAAK,EACV,SAAS,EACT,sBAAsB,EACtB,sBAAsB,EACvB,MAAM,2BAA2B,CAAC;AAEnC,YAAY,EACV,aAAa,EACb,SAAS,EACT,kBAAkB,EAClB,sBAAsB,EACtB,cAAc,EACd,WAAW,GACZ,MAAM,2BAA2B,CAAC;AAEnC,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;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,
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
package/dist/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
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
|
|
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.
|
|
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.
|
|
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.
|
|
61
|
+
"vitest": "^4.1.0"
|
|
61
62
|
},
|
|
62
63
|
"sideEffects": false
|
|
63
64
|
}
|