@temps-sdk/react-analytics 0.0.1-beta.11 → 0.0.1-beta.12
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/EngagementTracker.d.ts +51 -0
- package/dist/EngagementTracker.d.ts.map +1 -0
- package/dist/EngagementTracker.js +200 -0
- package/dist/Provider.d.ts +1 -1
- package/dist/Provider.d.ts.map +1 -1
- package/dist/Provider.js +51 -34
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/types.d.ts +8 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/useEngagementTracking.d.ts +34 -0
- package/dist/useEngagementTracking.d.ts.map +1 -0
- package/dist/useEngagementTracking.js +65 -0
- package/package.json +1 -1
|
@@ -0,0 +1,51 @@
|
|
|
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 isActive;
|
|
29
|
+
private heartbeatTimer;
|
|
30
|
+
private inactivityTimer;
|
|
31
|
+
private hasTrackedLeave;
|
|
32
|
+
private readonly basePath;
|
|
33
|
+
private readonly domain;
|
|
34
|
+
private readonly onHeartbeat?;
|
|
35
|
+
private readonly onPageLeave?;
|
|
36
|
+
constructor(options?: EngagementTrackerOptions);
|
|
37
|
+
private setupEventListeners;
|
|
38
|
+
private handleVisibilityChange;
|
|
39
|
+
private handleUserActivity;
|
|
40
|
+
private resetInactivityTimer;
|
|
41
|
+
private updateEngagementTime;
|
|
42
|
+
private startHeartbeat;
|
|
43
|
+
private stopHeartbeat;
|
|
44
|
+
private sendHeartbeat;
|
|
45
|
+
private handlePageLeave;
|
|
46
|
+
private handleNavigation;
|
|
47
|
+
private getEngagementData;
|
|
48
|
+
private reset;
|
|
49
|
+
destroy(): void;
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=EngagementTracker.d.ts.map
|
|
@@ -0,0 +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,QAAQ,CAAiB;IACjC,OAAO,CAAC,cAAc,CAA8C;IACpE,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;IAqBlD,OAAO,CAAC,mBAAmB;IAyB3B,OAAO,CAAC,sBAAsB,CAa5B;IAEF,OAAO,CAAC,kBAAkB,CAYxB;IAEF,OAAO,CAAC,oBAAoB;IAc5B,OAAO,CAAC,oBAAoB;IAO5B,OAAO,CAAC,cAAc;IAUtB,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;CAkBvB"}
|
|
@@ -0,0 +1,200 @@
|
|
|
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
|
|
21
|
+
this.updateEngagementTime();
|
|
22
|
+
this.lastEngagementStart = null;
|
|
23
|
+
this.stopHeartbeat();
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
this.handleUserActivity = () => {
|
|
27
|
+
const now = Date.now();
|
|
28
|
+
if (!this.isActive && this.isVisible) {
|
|
29
|
+
// Resume from inactive state
|
|
30
|
+
this.isActive = true;
|
|
31
|
+
this.lastEngagementStart = now;
|
|
32
|
+
this.startHeartbeat();
|
|
33
|
+
}
|
|
34
|
+
this.lastActivityTime = now;
|
|
35
|
+
this.resetInactivityTimer();
|
|
36
|
+
};
|
|
37
|
+
this.handlePageLeave = () => {
|
|
38
|
+
if (this.hasTrackedLeave)
|
|
39
|
+
return;
|
|
40
|
+
this.hasTrackedLeave = true;
|
|
41
|
+
this.updateEngagementTime();
|
|
42
|
+
const data = this.getEngagementData();
|
|
43
|
+
if (this.onPageLeave) {
|
|
44
|
+
this.onPageLeave(data);
|
|
45
|
+
}
|
|
46
|
+
sendAnalyticsReliable("event", {
|
|
47
|
+
event_name: "page_leave",
|
|
48
|
+
request_path: window.location.pathname,
|
|
49
|
+
request_query: window.location.search,
|
|
50
|
+
domain: this.domain,
|
|
51
|
+
event_data: {
|
|
52
|
+
engagement_time_seconds: data.engagement_time_seconds,
|
|
53
|
+
total_time_seconds: data.total_time_seconds,
|
|
54
|
+
heartbeat_count: this.heartbeatCount,
|
|
55
|
+
was_engaged: data.is_engaged,
|
|
56
|
+
url: window.location.href,
|
|
57
|
+
referrer: document.referrer,
|
|
58
|
+
},
|
|
59
|
+
}, this.basePath);
|
|
60
|
+
};
|
|
61
|
+
this.handleNavigation = () => {
|
|
62
|
+
this.handlePageLeave();
|
|
63
|
+
this.reset();
|
|
64
|
+
};
|
|
65
|
+
this.basePath = options.basePath || "/api/_temps";
|
|
66
|
+
this.domain = options.domain || window.location.hostname;
|
|
67
|
+
this.heartbeatInterval = options.heartbeatInterval || 30000; // 30 seconds
|
|
68
|
+
this.inactivityTimeout = options.inactivityTimeout || 30000; // 30 seconds
|
|
69
|
+
this.engagementThreshold = options.engagementThreshold || 10000; // 10 seconds
|
|
70
|
+
this.onHeartbeat = options.onHeartbeat;
|
|
71
|
+
this.onPageLeave = options.onPageLeave;
|
|
72
|
+
this.startTime = Date.now();
|
|
73
|
+
this.lastActivityTime = Date.now();
|
|
74
|
+
this.isVisible = document.visibilityState === "visible";
|
|
75
|
+
// Don't start engagement tracking until first user interaction
|
|
76
|
+
this.lastEngagementStart = null;
|
|
77
|
+
this.isActive = false;
|
|
78
|
+
this.setupEventListeners();
|
|
79
|
+
this.startHeartbeat();
|
|
80
|
+
}
|
|
81
|
+
setupEventListeners() {
|
|
82
|
+
// Visibility change
|
|
83
|
+
document.addEventListener("visibilitychange", this.handleVisibilityChange);
|
|
84
|
+
// User activity tracking
|
|
85
|
+
const activityEvents = ["mousedown", "mousemove", "keypress", "scroll", "touchstart", "click"];
|
|
86
|
+
activityEvents.forEach(event => {
|
|
87
|
+
document.addEventListener(event, this.handleUserActivity, { passive: true });
|
|
88
|
+
});
|
|
89
|
+
// Page leave events
|
|
90
|
+
window.addEventListener("pagehide", this.handlePageLeave);
|
|
91
|
+
window.addEventListener("beforeunload", this.handlePageLeave);
|
|
92
|
+
// SPA navigation
|
|
93
|
+
const originalPushState = window.history.pushState;
|
|
94
|
+
window.history.pushState = (...args) => {
|
|
95
|
+
this.handlePageLeave();
|
|
96
|
+
originalPushState.apply(window.history, args);
|
|
97
|
+
this.reset();
|
|
98
|
+
};
|
|
99
|
+
window.addEventListener("popstate", this.handleNavigation);
|
|
100
|
+
}
|
|
101
|
+
resetInactivityTimer() {
|
|
102
|
+
if (this.inactivityTimer) {
|
|
103
|
+
clearTimeout(this.inactivityTimer);
|
|
104
|
+
}
|
|
105
|
+
this.inactivityTimer = setTimeout(() => {
|
|
106
|
+
if (this.isVisible) {
|
|
107
|
+
this.updateEngagementTime();
|
|
108
|
+
this.isActive = false;
|
|
109
|
+
this.lastEngagementStart = null;
|
|
110
|
+
}
|
|
111
|
+
}, this.inactivityTimeout);
|
|
112
|
+
}
|
|
113
|
+
updateEngagementTime() {
|
|
114
|
+
if (this.lastEngagementStart !== null) {
|
|
115
|
+
const currentEngagement = Date.now() - this.lastEngagementStart;
|
|
116
|
+
this.engagementTime += currentEngagement;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
startHeartbeat() {
|
|
120
|
+
if (this.heartbeatTimer)
|
|
121
|
+
return;
|
|
122
|
+
this.heartbeatTimer = setInterval(() => {
|
|
123
|
+
if (this.isVisible && this.isActive) {
|
|
124
|
+
this.sendHeartbeat();
|
|
125
|
+
}
|
|
126
|
+
}, this.heartbeatInterval);
|
|
127
|
+
}
|
|
128
|
+
stopHeartbeat() {
|
|
129
|
+
if (this.heartbeatTimer) {
|
|
130
|
+
clearInterval(this.heartbeatTimer);
|
|
131
|
+
this.heartbeatTimer = null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
async sendHeartbeat() {
|
|
135
|
+
this.updateEngagementTime();
|
|
136
|
+
this.heartbeatCount++;
|
|
137
|
+
const data = this.getEngagementData();
|
|
138
|
+
if (this.onHeartbeat) {
|
|
139
|
+
this.onHeartbeat(data);
|
|
140
|
+
}
|
|
141
|
+
await sendAnalytics("event", {
|
|
142
|
+
event_name: "heartbeat",
|
|
143
|
+
request_path: window.location.pathname,
|
|
144
|
+
request_query: window.location.search,
|
|
145
|
+
domain: this.domain,
|
|
146
|
+
event_data: {
|
|
147
|
+
engagement_time: Math.round(this.engagementTime),
|
|
148
|
+
total_time: Math.round(Date.now() - this.startTime),
|
|
149
|
+
is_engaged: data.is_engaged,
|
|
150
|
+
is_visible: this.isVisible,
|
|
151
|
+
time_since_last_activity: data.time_since_last_activity,
|
|
152
|
+
heartbeat_count: this.heartbeatCount,
|
|
153
|
+
},
|
|
154
|
+
}, "POST", this.basePath);
|
|
155
|
+
// Reset engagement start for next period
|
|
156
|
+
if (this.isActive && this.isVisible) {
|
|
157
|
+
this.lastEngagementStart = Date.now();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
getEngagementData() {
|
|
161
|
+
const totalTime = Date.now() - this.startTime;
|
|
162
|
+
const timeSinceLastActivity = Date.now() - this.lastActivityTime;
|
|
163
|
+
return {
|
|
164
|
+
engagement_time_seconds: Math.round(this.engagementTime / 1000),
|
|
165
|
+
total_time_seconds: Math.round(totalTime / 1000),
|
|
166
|
+
heartbeat_count: this.heartbeatCount,
|
|
167
|
+
is_engaged: this.engagementTime >= this.engagementThreshold,
|
|
168
|
+
is_visible: this.isVisible,
|
|
169
|
+
time_since_last_activity: Math.round(timeSinceLastActivity / 1000),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
reset() {
|
|
173
|
+
this.startTime = Date.now();
|
|
174
|
+
this.engagementTime = 0;
|
|
175
|
+
this.lastActivityTime = Date.now();
|
|
176
|
+
this.heartbeatCount = 0;
|
|
177
|
+
this.hasTrackedLeave = false;
|
|
178
|
+
// Reset to inactive state - wait for user interaction
|
|
179
|
+
this.isActive = false;
|
|
180
|
+
this.lastEngagementStart = null;
|
|
181
|
+
if (this.isVisible) {
|
|
182
|
+
this.startHeartbeat();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
destroy() {
|
|
186
|
+
this.handlePageLeave();
|
|
187
|
+
this.stopHeartbeat();
|
|
188
|
+
if (this.inactivityTimer) {
|
|
189
|
+
clearTimeout(this.inactivityTimer);
|
|
190
|
+
}
|
|
191
|
+
document.removeEventListener("visibilitychange", this.handleVisibilityChange);
|
|
192
|
+
window.removeEventListener("pagehide", this.handlePageLeave);
|
|
193
|
+
window.removeEventListener("beforeunload", this.handlePageLeave);
|
|
194
|
+
window.removeEventListener("popstate", this.handleNavigation);
|
|
195
|
+
const activityEvents = ["mousedown", "mousemove", "keypress", "scroll", "touchstart", "click"];
|
|
196
|
+
activityEvents.forEach(event => {
|
|
197
|
+
document.removeEventListener(event, this.handleUserActivity);
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
package/dist/Provider.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { AnalyticsContextValue, TempsAnalyticsProviderProps } from "./types";
|
|
2
|
-
export declare function TempsAnalyticsProvider({ basePath, disabled, ignoreLocalhost, autoTrackPageviews, autoTrackPageLeave, pageLeaveEventName, autoTrackSpeedAnalytics, domain, children, }: TempsAnalyticsProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
2
|
+
export declare function TempsAnalyticsProvider({ basePath, disabled, ignoreLocalhost, autoTrackPageviews, autoTrackPageLeave, pageLeaveEventName, autoTrackSpeedAnalytics, autoTrackEngagement, heartbeatInterval, inactivityTimeout, engagementThreshold, domain, children, }: TempsAnalyticsProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
3
3
|
export declare function useTempsAnalytics(): AnalyticsContextValue;
|
|
4
4
|
//# sourceMappingURL=Provider.d.ts.map
|
package/dist/Provider.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Provider.d.ts","sourceRoot":"","sources":["../src/Provider.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,qBAAqB,EAAE,2BAA2B,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"Provider.d.ts","sourceRoot":"","sources":["../src/Provider.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,qBAAqB,EAAE,2BAA2B,EAAE,MAAM,SAAS,CAAC;AAOlF,wBAAgB,sBAAsB,CAAC,EACrC,QAAwB,EACxB,QAAgB,EAChB,eAAsB,EACtB,kBAAyB,EACzB,kBAAyB,EACzB,kBAAiC,EACjC,uBAA8B,EAC9B,mBAA0B,EAC1B,iBAAyB,EACzB,iBAAyB,EACzB,mBAA2B,EAC3B,MAAM,EACN,QAAQ,GACT,EAAE,2BAA2B,2CAwM7B;AAED,wBAAgB,iBAAiB,IAAI,qBAAqB,CAMzD"}
|
package/dist/Provider.js
CHANGED
|
@@ -3,8 +3,9 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
3
3
|
import { createContext, useCallback, useContext, useEffect, useMemo, useRef } from "react";
|
|
4
4
|
import { isLocalhostLike, isTestEnvironment, sendAnalytics, sendAnalyticsReliable } from "./utils";
|
|
5
5
|
import { useSpeedAnalytics } from "./useSpeedAnalytics";
|
|
6
|
+
import { EngagementTracker } from "./EngagementTracker";
|
|
6
7
|
const AnalyticsContext = createContext(undefined);
|
|
7
|
-
export function TempsAnalyticsProvider({ basePath = "/api/_temps", disabled = false, ignoreLocalhost = true, autoTrackPageviews = true, autoTrackPageLeave = true, pageLeaveEventName = "page_leave", autoTrackSpeedAnalytics = true, domain, children, }) {
|
|
8
|
+
export function TempsAnalyticsProvider({ basePath = "/api/_temps", disabled = false, ignoreLocalhost = true, autoTrackPageviews = true, autoTrackPageLeave = true, pageLeaveEventName = "page_leave", autoTrackSpeedAnalytics = true, autoTrackEngagement = true, heartbeatInterval = 30000, inactivityTimeout = 30000, engagementThreshold = 10000, domain, children, }) {
|
|
8
9
|
const enabled = useMemo(() => {
|
|
9
10
|
if (disabled)
|
|
10
11
|
return false;
|
|
@@ -123,41 +124,57 @@ export function TempsAnalyticsProvider({ basePath = "/api/_temps", disabled = fa
|
|
|
123
124
|
document.addEventListener("click", onClick);
|
|
124
125
|
return () => document.removeEventListener("click", onClick);
|
|
125
126
|
}, [enabled, trackEvent]);
|
|
126
|
-
//
|
|
127
|
+
// Engagement tracking with heartbeats and page leave
|
|
127
128
|
useEffect(() => {
|
|
128
|
-
if (!
|
|
129
|
+
if (!enabled)
|
|
129
130
|
return;
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
131
|
+
// If engagement tracking is enabled, it handles both heartbeats and page leave
|
|
132
|
+
if (autoTrackEngagement) {
|
|
133
|
+
const tracker = new EngagementTracker({
|
|
134
|
+
basePath,
|
|
135
|
+
domain,
|
|
136
|
+
heartbeatInterval,
|
|
137
|
+
inactivityTimeout,
|
|
138
|
+
engagementThreshold,
|
|
139
|
+
});
|
|
140
|
+
return () => {
|
|
141
|
+
tracker.destroy();
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
// Legacy page leave tracking without engagement metrics
|
|
145
|
+
else if (autoTrackPageLeave) {
|
|
146
|
+
let hasTracked = false;
|
|
147
|
+
let startTime = Date.now();
|
|
148
|
+
const trackPageLeave = () => {
|
|
149
|
+
if (hasTracked)
|
|
150
|
+
return;
|
|
151
|
+
hasTracked = true;
|
|
152
|
+
const timeOnPage = Date.now() - startTime;
|
|
153
|
+
sendAnalyticsReliable("event", {
|
|
154
|
+
event_name: pageLeaveEventName,
|
|
155
|
+
request_query: window.location.search,
|
|
156
|
+
request_path: window.location.pathname,
|
|
157
|
+
domain: domain || window.location.hostname,
|
|
158
|
+
event_data: {
|
|
159
|
+
time_on_page_ms: timeOnPage,
|
|
160
|
+
timestamp: new Date().toISOString(),
|
|
161
|
+
url: window.location.href,
|
|
162
|
+
referrer: document.referrer,
|
|
163
|
+
},
|
|
164
|
+
}, basePath);
|
|
165
|
+
};
|
|
166
|
+
// Use pagehide as primary (most reliable), with beforeunload as fallback
|
|
167
|
+
const handlePageLeave = () => trackPageLeave();
|
|
168
|
+
// pagehide is the most reliable for modern browsers
|
|
169
|
+
window.addEventListener("pagehide", handlePageLeave);
|
|
170
|
+
// beforeunload as fallback for older browsers
|
|
171
|
+
window.addEventListener("beforeunload", handlePageLeave);
|
|
172
|
+
return () => {
|
|
173
|
+
window.removeEventListener("pagehide", handlePageLeave);
|
|
174
|
+
window.removeEventListener("beforeunload", handlePageLeave);
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
}, [autoTrackPageLeave, autoTrackEngagement, enabled, pageLeaveEventName, domain, basePath, heartbeatInterval, inactivityTimeout, engagementThreshold]);
|
|
161
178
|
// Speed analytics tracking
|
|
162
179
|
useSpeedAnalytics({
|
|
163
180
|
basePath,
|
package/dist/index.d.ts
CHANGED
|
@@ -5,4 +5,6 @@ export * from "./useTrackEvent";
|
|
|
5
5
|
export * from "./useTrackPageview";
|
|
6
6
|
export * from "./usePageLeave";
|
|
7
7
|
export * from "./useSpeedAnalytics";
|
|
8
|
+
export * from "./useEngagementTracking";
|
|
9
|
+
export { EngagementTracker, type EngagementTrackerOptions, type EngagementData } from "./EngagementTracker";
|
|
8
10
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC;AACnC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC;AACnC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC;AACpC,cAAc,yBAAyB,CAAC;AACxC,OAAO,EAAE,iBAAiB,EAAE,KAAK,wBAAwB,EAAE,KAAK,cAAc,EAAE,MAAM,qBAAqB,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/types.d.ts
CHANGED
|
@@ -38,6 +38,14 @@ export interface TempsAnalyticsProviderProps extends AnalyticsClientOptions {
|
|
|
38
38
|
pageLeaveEventName?: string;
|
|
39
39
|
/** Auto track speed analytics (Web Vitals). Defaults to true. */
|
|
40
40
|
autoTrackSpeedAnalytics?: boolean;
|
|
41
|
+
/** Auto track engagement with heartbeats. Defaults to true. */
|
|
42
|
+
autoTrackEngagement?: boolean;
|
|
43
|
+
/** Heartbeat interval in milliseconds. Defaults to 30000 (30 seconds). */
|
|
44
|
+
heartbeatInterval?: number;
|
|
45
|
+
/** Inactivity timeout in milliseconds. Defaults to 30000 (30 seconds). */
|
|
46
|
+
inactivityTimeout?: number;
|
|
47
|
+
/** Engagement threshold in milliseconds to consider session engaged. Defaults to 10000 (10 seconds). */
|
|
48
|
+
engagementThreshold?: number;
|
|
41
49
|
/** Children to render inside the provider. */
|
|
42
50
|
children: React.ReactNode;
|
|
43
51
|
}
|
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,MAAM,MAAM,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Ac;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,GAAG,mBAAmB,GAAG,MAAM,CAAC;CAC/C;AAED,MAAM,WAAW,WAAW;IAC1B,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;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,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACvB"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type EngagementTrackerOptions, type EngagementData } from "./EngagementTracker";
|
|
2
|
+
export interface UseEngagementTrackingOptions extends Omit<EngagementTrackerOptions, "basePath" | "domain"> {
|
|
3
|
+
/** Whether to enable engagement tracking. Defaults to true. */
|
|
4
|
+
enabled?: boolean;
|
|
5
|
+
/** Callback when engagement data is updated via heartbeat */
|
|
6
|
+
onEngagementUpdate?: (data: EngagementData) => void;
|
|
7
|
+
/** Callback when page leave is triggered */
|
|
8
|
+
onPageLeave?: (data: EngagementData) => void;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Hook to manually control engagement tracking for specific components or pages.
|
|
12
|
+
* This is useful when you want fine-grained control over engagement tracking
|
|
13
|
+
* or need to track engagement for specific sections of your app.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* function ArticlePage() {
|
|
18
|
+
* const { engagementData } = useEngagementTracking({
|
|
19
|
+
* heartbeatInterval: 15000, // Send heartbeat every 15 seconds
|
|
20
|
+
* engagementThreshold: 5000, // Consider engaged after 5 seconds
|
|
21
|
+
* onEngagementUpdate: (data) => {
|
|
22
|
+
* console.log('Engagement updated:', data);
|
|
23
|
+
* }
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* return <article>...</article>;
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function useEngagementTracking(options?: UseEngagementTrackingOptions): {
|
|
31
|
+
engagementData: EngagementData;
|
|
32
|
+
isTracking: boolean;
|
|
33
|
+
};
|
|
34
|
+
//# sourceMappingURL=useEngagementTracking.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useEngagementTracking.d.ts","sourceRoot":"","sources":["../src/useEngagementTracking.ts"],"names":[],"mappings":"AAEA,OAAO,EAAqB,KAAK,wBAAwB,EAAE,KAAK,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAG5G,MAAM,WAAW,4BAA6B,SAAQ,IAAI,CAAC,wBAAwB,EAAE,UAAU,GAAG,QAAQ,CAAC;IACzG,+DAA+D;IAC/D,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,6DAA6D;IAC7D,kBAAkB,CAAC,EAAE,CAAC,IAAI,EAAE,cAAc,KAAK,IAAI,CAAC;IACpD,4CAA4C;IAC5C,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,cAAc,KAAK,IAAI,CAAC;CAC9C;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,GAAE,4BAAiC;;;EAkD/E"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
|
+
import { EngagementTracker } from "./EngagementTracker";
|
|
4
|
+
import { useTempsAnalytics } from "./Provider";
|
|
5
|
+
/**
|
|
6
|
+
* Hook to manually control engagement tracking for specific components or pages.
|
|
7
|
+
* This is useful when you want fine-grained control over engagement tracking
|
|
8
|
+
* or need to track engagement for specific sections of your app.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```tsx
|
|
12
|
+
* function ArticlePage() {
|
|
13
|
+
* const { engagementData } = useEngagementTracking({
|
|
14
|
+
* heartbeatInterval: 15000, // Send heartbeat every 15 seconds
|
|
15
|
+
* engagementThreshold: 5000, // Consider engaged after 5 seconds
|
|
16
|
+
* onEngagementUpdate: (data) => {
|
|
17
|
+
* console.log('Engagement updated:', data);
|
|
18
|
+
* }
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* return <article>...</article>;
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function useEngagementTracking(options = {}) {
|
|
26
|
+
const analytics = useTempsAnalytics();
|
|
27
|
+
const trackerRef = useRef(null);
|
|
28
|
+
const engagementDataRef = useRef({
|
|
29
|
+
engagement_time_seconds: 0,
|
|
30
|
+
total_time_seconds: 0,
|
|
31
|
+
heartbeat_count: 0,
|
|
32
|
+
is_engaged: false,
|
|
33
|
+
is_visible: true,
|
|
34
|
+
time_since_last_activity: 0,
|
|
35
|
+
});
|
|
36
|
+
const { enabled = true, onEngagementUpdate, onPageLeave, ...trackerOptions } = options;
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
if (!enabled || !analytics.enabled) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
// Create tracker instance
|
|
42
|
+
trackerRef.current = new EngagementTracker({
|
|
43
|
+
...trackerOptions,
|
|
44
|
+
onHeartbeat: (data) => {
|
|
45
|
+
engagementDataRef.current = data;
|
|
46
|
+
onEngagementUpdate?.(data);
|
|
47
|
+
},
|
|
48
|
+
onPageLeave: (data) => {
|
|
49
|
+
engagementDataRef.current = data;
|
|
50
|
+
onPageLeave?.(data);
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
// Cleanup on unmount
|
|
54
|
+
return () => {
|
|
55
|
+
if (trackerRef.current) {
|
|
56
|
+
trackerRef.current.destroy();
|
|
57
|
+
trackerRef.current = null;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}, [enabled, analytics.enabled]);
|
|
61
|
+
return {
|
|
62
|
+
engagementData: engagementDataRef.current,
|
|
63
|
+
isTracking: Boolean(trackerRef.current),
|
|
64
|
+
};
|
|
65
|
+
}
|