@temps-sdk/react-analytics 0.0.1-beta.1 → 0.0.1-beta.10
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/Provider.d.ts +1 -1
- package/dist/Provider.d.ts.map +1 -1
- package/dist/Provider.js +175 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1262
- package/dist/types.d.ts +22 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -0
- package/dist/useAnalytics.d.ts.map +1 -1
- package/dist/useAnalytics.js +15 -0
- package/dist/usePageLeave.d.ts +13 -0
- package/dist/usePageLeave.d.ts.map +1 -0
- package/dist/usePageLeave.js +65 -0
- package/dist/useSpeedAnalytics.d.ts +8 -0
- package/dist/useSpeedAnalytics.d.ts.map +1 -0
- package/dist/useSpeedAnalytics.js +60 -0
- package/dist/useTrackEvent.d.ts.map +1 -1
- package/dist/useTrackEvent.js +7 -0
- package/dist/useTrackPageview.d.ts.map +1 -1
- package/dist/useTrackPageview.js +7 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +97 -0
- package/package.json +10 -3
- package/dist/index.js.map +0 -18
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, children, }: TempsAnalyticsProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
2
|
+
export declare function TempsAnalyticsProvider({ basePath, disabled, ignoreLocalhost, autoTrackPageviews, autoTrackPageLeave, pageLeaveEventName, autoTrackSpeedAnalytics, 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;AAMlF,wBAAgB,sBAAsB,CAAC,EACrC,QAAwB,EACxB,QAAgB,EAChB,eAAsB,EACtB,kBAAyB,EACzB,kBAAyB,EACzB,kBAAiC,EACjC,uBAA8B,EAC9B,MAAM,EACN,QAAQ,GACT,EAAE,2BAA2B,2CAuL7B;AAED,wBAAgB,iBAAiB,IAAI,qBAAqB,CAMzD"}
|
package/dist/Provider.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useRef } from "react";
|
|
4
|
+
import { isLocalhostLike, isTestEnvironment, sendAnalytics, sendAnalyticsReliable } from "./utils";
|
|
5
|
+
import { useSpeedAnalytics } from "./useSpeedAnalytics";
|
|
6
|
+
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
|
+
const enabled = useMemo(() => {
|
|
9
|
+
if (disabled)
|
|
10
|
+
return false;
|
|
11
|
+
if (typeof window === "undefined")
|
|
12
|
+
return false;
|
|
13
|
+
if (ignoreLocalhost && (isLocalhostLike() || isTestEnvironment()))
|
|
14
|
+
return false;
|
|
15
|
+
try {
|
|
16
|
+
if (window.localStorage?.temps_ignore === "true")
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
catch { }
|
|
20
|
+
return true;
|
|
21
|
+
}, [disabled, ignoreLocalhost]);
|
|
22
|
+
const trackEvent = useCallback(async (eventName, data = {}) => {
|
|
23
|
+
if (!enabled)
|
|
24
|
+
return;
|
|
25
|
+
await sendAnalytics("event", {
|
|
26
|
+
event_name: eventName,
|
|
27
|
+
request_query: window.location.search,
|
|
28
|
+
request_path: window.location.pathname,
|
|
29
|
+
domain: domain || window.location.hostname,
|
|
30
|
+
event_data: data,
|
|
31
|
+
}, "POST", basePath);
|
|
32
|
+
}, [enabled, basePath, domain]);
|
|
33
|
+
const trackPageview = useCallback(() => {
|
|
34
|
+
if (!enabled)
|
|
35
|
+
return;
|
|
36
|
+
void sendAnalytics("event", {
|
|
37
|
+
event_name: "pageview",
|
|
38
|
+
request_query: window.location.search,
|
|
39
|
+
request_path: window.location.pathname,
|
|
40
|
+
domain: domain || window.location.hostname,
|
|
41
|
+
event_data: {
|
|
42
|
+
referrer: document.referrer,
|
|
43
|
+
userAgent: navigator.userAgent,
|
|
44
|
+
timestamp: new Date().toISOString(),
|
|
45
|
+
},
|
|
46
|
+
}, "POST", basePath);
|
|
47
|
+
}, [enabled, basePath, domain]);
|
|
48
|
+
const identify = useCallback(async () => {
|
|
49
|
+
// no-op placeholder for SDK parity; implement when identity endpoint is available
|
|
50
|
+
}, []);
|
|
51
|
+
// Route change monitoring (pushState/popstate)
|
|
52
|
+
const currentPathRef = useRef(typeof window !== "undefined" ? window.location.pathname : "");
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
if (!autoTrackPageviews || !enabled)
|
|
55
|
+
return;
|
|
56
|
+
let initialLoad = true;
|
|
57
|
+
const originalPushState = window.history?.pushState?.bind(window.history);
|
|
58
|
+
function maybeTrack() {
|
|
59
|
+
const nextPath = window.location.pathname;
|
|
60
|
+
if (currentPathRef.current !== nextPath) {
|
|
61
|
+
currentPathRef.current = nextPath;
|
|
62
|
+
trackPageview();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (originalPushState) {
|
|
66
|
+
window.history.pushState = ((data, unused, url) => {
|
|
67
|
+
originalPushState(data, unused, url);
|
|
68
|
+
maybeTrack();
|
|
69
|
+
});
|
|
70
|
+
const onPop = () => maybeTrack();
|
|
71
|
+
window.addEventListener("popstate", onPop);
|
|
72
|
+
const cleanup = () => {
|
|
73
|
+
window.removeEventListener("popstate", onPop);
|
|
74
|
+
if (originalPushState) {
|
|
75
|
+
window.history.pushState = originalPushState;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
// Initial load or prerender visibility handling
|
|
79
|
+
if (document.visibilityState === "prerender") {
|
|
80
|
+
const onVisibility = () => {
|
|
81
|
+
if (document.visibilityState === "visible") {
|
|
82
|
+
if (initialLoad) {
|
|
83
|
+
initialLoad = false;
|
|
84
|
+
trackPageview();
|
|
85
|
+
}
|
|
86
|
+
document.removeEventListener("visibilitychange", onVisibility);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
document.addEventListener("visibilitychange", onVisibility);
|
|
90
|
+
return () => {
|
|
91
|
+
document.removeEventListener("visibilitychange", onVisibility);
|
|
92
|
+
cleanup();
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
else if (initialLoad) {
|
|
96
|
+
initialLoad = false;
|
|
97
|
+
trackPageview();
|
|
98
|
+
}
|
|
99
|
+
return cleanup;
|
|
100
|
+
}
|
|
101
|
+
}, [autoTrackPageviews, enabled, trackPageview]);
|
|
102
|
+
// Click delegation for [temps-event-name] with temps-data-* attributes
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
if (!enabled)
|
|
105
|
+
return;
|
|
106
|
+
const onClick = (event) => {
|
|
107
|
+
const target = event.target;
|
|
108
|
+
const eventElement = target?.closest?.("[temps-event-name]");
|
|
109
|
+
if (!(eventElement instanceof HTMLElement))
|
|
110
|
+
return;
|
|
111
|
+
const eventName = eventElement.getAttribute("temps-event-name");
|
|
112
|
+
if (!eventName)
|
|
113
|
+
return;
|
|
114
|
+
const eventData = {};
|
|
115
|
+
for (const attr of eventElement.getAttributeNames()) {
|
|
116
|
+
if (attr.startsWith("temps-data-")) {
|
|
117
|
+
const dataKey = attr.replace("temps-data-", "");
|
|
118
|
+
eventData[dataKey] = eventElement.getAttribute(attr);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
void trackEvent(eventName, eventData);
|
|
122
|
+
};
|
|
123
|
+
document.addEventListener("click", onClick);
|
|
124
|
+
return () => document.removeEventListener("click", onClick);
|
|
125
|
+
}, [enabled, trackEvent]);
|
|
126
|
+
// Page leave tracking
|
|
127
|
+
useEffect(() => {
|
|
128
|
+
if (!autoTrackPageLeave || !enabled)
|
|
129
|
+
return;
|
|
130
|
+
let hasTracked = false;
|
|
131
|
+
let startTime = Date.now();
|
|
132
|
+
const trackPageLeave = () => {
|
|
133
|
+
if (hasTracked)
|
|
134
|
+
return;
|
|
135
|
+
hasTracked = true;
|
|
136
|
+
const timeOnPage = Date.now() - startTime;
|
|
137
|
+
sendAnalyticsReliable("event", {
|
|
138
|
+
event_name: pageLeaveEventName,
|
|
139
|
+
request_query: window.location.search,
|
|
140
|
+
request_path: window.location.pathname,
|
|
141
|
+
domain: domain || window.location.hostname,
|
|
142
|
+
event_data: {
|
|
143
|
+
time_on_page_ms: timeOnPage,
|
|
144
|
+
timestamp: new Date().toISOString(),
|
|
145
|
+
url: window.location.href,
|
|
146
|
+
referrer: document.referrer,
|
|
147
|
+
},
|
|
148
|
+
}, basePath);
|
|
149
|
+
};
|
|
150
|
+
// Use pagehide as primary (most reliable), with beforeunload as fallback
|
|
151
|
+
const handlePageLeave = () => trackPageLeave();
|
|
152
|
+
// pagehide is the most reliable for modern browsers
|
|
153
|
+
window.addEventListener("pagehide", handlePageLeave);
|
|
154
|
+
// beforeunload as fallback for older browsers
|
|
155
|
+
window.addEventListener("beforeunload", handlePageLeave);
|
|
156
|
+
return () => {
|
|
157
|
+
window.removeEventListener("pagehide", handlePageLeave);
|
|
158
|
+
window.removeEventListener("beforeunload", handlePageLeave);
|
|
159
|
+
};
|
|
160
|
+
}, [autoTrackPageLeave, enabled, pageLeaveEventName, domain, basePath]);
|
|
161
|
+
// Speed analytics tracking
|
|
162
|
+
useSpeedAnalytics({
|
|
163
|
+
basePath,
|
|
164
|
+
disabled: !enabled || !autoTrackSpeedAnalytics,
|
|
165
|
+
});
|
|
166
|
+
const value = useMemo(() => ({ trackEvent, identify, trackPageview, enabled }), [trackEvent, identify, trackPageview, enabled]);
|
|
167
|
+
return _jsx(AnalyticsContext.Provider, { value: value, children: children });
|
|
168
|
+
}
|
|
169
|
+
export function useTempsAnalytics() {
|
|
170
|
+
const ctx = useContext(AnalyticsContext);
|
|
171
|
+
if (!ctx) {
|
|
172
|
+
throw new Error("useTempsAnalytics must be used within a TempsAnalyticsProvider");
|
|
173
|
+
}
|
|
174
|
+
return ctx;
|
|
175
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
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"}
|