@temps-sdk/react-analytics 0.0.1-beta.1 → 0.0.1-beta.2

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.
@@ -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;AAIlF,wBAAgB,sBAAsB,CAAC,EACrC,QAAwB,EACxB,QAAgB,EAChB,eAAsB,EACtB,kBAAyB,EACzB,QAAQ,GACT,EAAE,2BAA2B,2CAqI7B;AAED,wBAAgB,iBAAiB,IAAI,qBAAqB,CAMzD"}
1
+ {"version":3,"file":"Provider.d.ts","sourceRoot":"","sources":["../src/Provider.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,qBAAqB,EAAE,2BAA2B,EAAE,MAAM,SAAS,CAAC;AAIlF,wBAAgB,sBAAsB,CAAC,EACrC,QAAwB,EACxB,QAAgB,EAChB,eAAsB,EACtB,kBAAyB,EACzB,QAAQ,GACT,EAAE,2BAA2B,2CAqI7B;AAED,wBAAgB,iBAAiB,IAAI,qBAAqB,CAMzD"}
@@ -0,0 +1,130 @@
1
+ "use client";
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import React, { createContext, useCallback, useContext, useEffect, useMemo, useRef } from "react";
4
+ import { isLocalhostLike, isTestEnvironment, sendAnalytics } from "./utils";
5
+ const AnalyticsContext = createContext(undefined);
6
+ export function TempsAnalyticsProvider({ basePath = "/api/_temps", disabled = false, ignoreLocalhost = true, autoTrackPageviews = true, children, }) {
7
+ const enabled = useMemo(() => {
8
+ if (disabled)
9
+ return false;
10
+ if (ignoreLocalhost && (isLocalhostLike() || isTestEnvironment()))
11
+ return false;
12
+ try {
13
+ if (window.localStorage?.temps_ignore === "true")
14
+ return false;
15
+ }
16
+ catch { }
17
+ return true;
18
+ }, [disabled, ignoreLocalhost]);
19
+ const trackEvent = useCallback(async (eventName, data = {}) => {
20
+ if (!enabled)
21
+ return;
22
+ await sendAnalytics("event", {
23
+ event_name: eventName,
24
+ request_query: window.location.search,
25
+ request_path: window.location.pathname,
26
+ event_data: data,
27
+ }, "POST", basePath);
28
+ }, [enabled, basePath]);
29
+ const trackPageview = useCallback(() => {
30
+ if (!enabled)
31
+ return;
32
+ void sendAnalytics("event", {
33
+ event_name: "pageview",
34
+ request_query: window.location.search,
35
+ request_path: window.location.pathname,
36
+ event_data: {
37
+ referrer: document.referrer,
38
+ userAgent: navigator.userAgent,
39
+ timestamp: new Date().toISOString(),
40
+ },
41
+ }, "POST", basePath);
42
+ }, [enabled, basePath]);
43
+ const identify = useCallback(async () => {
44
+ // no-op placeholder for SDK parity; implement when identity endpoint is available
45
+ }, []);
46
+ // Route change monitoring (pushState/popstate)
47
+ const currentPathRef = useRef(typeof window !== "undefined" ? window.location.pathname : "");
48
+ useEffect(() => {
49
+ if (!autoTrackPageviews || !enabled)
50
+ return;
51
+ let initialLoad = true;
52
+ const originalPushState = window.history?.pushState?.bind(window.history);
53
+ function maybeTrack() {
54
+ const nextPath = window.location.pathname;
55
+ if (currentPathRef.current !== nextPath) {
56
+ currentPathRef.current = nextPath;
57
+ trackPageview();
58
+ }
59
+ }
60
+ if (originalPushState) {
61
+ window.history.pushState = ((data, unused, url) => {
62
+ originalPushState(data, unused, url);
63
+ maybeTrack();
64
+ });
65
+ const onPop = () => maybeTrack();
66
+ window.addEventListener("popstate", onPop);
67
+ const cleanup = () => {
68
+ window.removeEventListener("popstate", onPop);
69
+ if (originalPushState) {
70
+ window.history.pushState = originalPushState;
71
+ }
72
+ };
73
+ // Initial load or prerender visibility handling
74
+ if (document.visibilityState === "prerender") {
75
+ const onVisibility = () => {
76
+ if (document.visibilityState === "visible") {
77
+ if (initialLoad) {
78
+ initialLoad = false;
79
+ trackPageview();
80
+ }
81
+ document.removeEventListener("visibilitychange", onVisibility);
82
+ }
83
+ };
84
+ document.addEventListener("visibilitychange", onVisibility);
85
+ return () => {
86
+ document.removeEventListener("visibilitychange", onVisibility);
87
+ cleanup();
88
+ };
89
+ }
90
+ else if (initialLoad) {
91
+ initialLoad = false;
92
+ trackPageview();
93
+ }
94
+ return cleanup;
95
+ }
96
+ }, [autoTrackPageviews, enabled, trackPageview]);
97
+ // Click delegation for [temps-event-name] with temps-data-* attributes
98
+ useEffect(() => {
99
+ if (!enabled)
100
+ return;
101
+ const onClick = (event) => {
102
+ const target = event.target;
103
+ const eventElement = target?.closest?.("[temps-event-name]");
104
+ if (!(eventElement instanceof HTMLElement))
105
+ return;
106
+ const eventName = eventElement.getAttribute("temps-event-name");
107
+ if (!eventName)
108
+ return;
109
+ const eventData = {};
110
+ for (const attr of eventElement.getAttributeNames()) {
111
+ if (attr.startsWith("temps-data-")) {
112
+ const dataKey = attr.replace("temps-data-", "");
113
+ eventData[dataKey] = eventElement.getAttribute(attr);
114
+ }
115
+ }
116
+ void trackEvent(eventName, eventData);
117
+ };
118
+ document.addEventListener("click", onClick);
119
+ return () => document.removeEventListener("click", onClick);
120
+ }, [enabled, trackEvent]);
121
+ const value = useMemo(() => ({ trackEvent, identify, trackPageview, enabled }), [trackEvent, identify, trackPageview, enabled]);
122
+ return _jsx(AnalyticsContext.Provider, { value: value, children: children });
123
+ }
124
+ export function useTempsAnalytics() {
125
+ const ctx = useContext(AnalyticsContext);
126
+ if (!ctx) {
127
+ throw new Error("useTempsAnalytics must be used within a TempsAnalyticsProvider");
128
+ }
129
+ return ctx;
130
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,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"}