@arthurreira/analytics 0.4.0 → 0.5.0

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.
Files changed (2) hide show
  1. package/dist/client.js +38 -1
  2. package/package.json +1 -1
package/dist/client.js CHANGED
@@ -106,6 +106,23 @@ async function trackCTA(apiUrl, apiKey, sessionId, path, ctaId, ctaVariant) {
106
106
  }
107
107
 
108
108
  // src/hooks/useAnalytics.ts
109
+ var SESSION_EXPIRY_MINUTES = 30;
110
+ async function getOrCreateSession(apiUrl, apiKey, visitorId) {
111
+ const storedSessionId = localStorage.getItem("af_session_id");
112
+ const lastActivity = localStorage.getItem("af_session_last_activity");
113
+ const now = Date.now();
114
+ if (storedSessionId && lastActivity) {
115
+ const minutesSinceActivity = (now - parseInt(lastActivity)) / 1e3 / 60;
116
+ if (minutesSinceActivity < SESSION_EXPIRY_MINUTES) {
117
+ localStorage.setItem("af_session_last_activity", String(now));
118
+ return storedSessionId;
119
+ }
120
+ }
121
+ const newSessionId = await createSession(apiUrl, apiKey, visitorId);
122
+ localStorage.setItem("af_session_id", newSessionId);
123
+ localStorage.setItem("af_session_last_activity", String(now));
124
+ return newSessionId;
125
+ }
109
126
  function useAnalytics(apiUrl, apiKey) {
110
127
  const sessionId = useRef(null);
111
128
  const pendingEvents = useRef([]);
@@ -114,14 +131,34 @@ function useAnalytics(apiUrl, apiKey) {
114
131
  if (typeof window === "undefined") return;
115
132
  const visitor_id = localStorage.getItem("af_analytics_visitor_id") || crypto.randomUUID();
116
133
  localStorage.setItem("af_analytics_visitor_id", visitor_id);
117
- createSession(apiUrl, apiKey, visitor_id).then((id) => {
134
+ getOrCreateSession(apiUrl, apiKey, visitor_id).then((id) => {
118
135
  sessionId.current = id;
119
136
  pendingEvents.current.forEach((fn) => fn());
120
137
  pendingEvents.current = [];
121
138
  });
122
139
  }, []);
140
+ useEffect(() => {
141
+ if (typeof window === "undefined") return;
142
+ const sendEnd = () => {
143
+ if (!sessionId.current) return;
144
+ navigator.sendBeacon(`${apiUrl}/sessions/${sessionId.current}/end`);
145
+ localStorage.removeItem("af_session_id");
146
+ localStorage.removeItem("af_session_last_activity");
147
+ };
148
+ const handleVisibility = () => {
149
+ if (document.visibilityState === "hidden") sendEnd();
150
+ };
151
+ const handleUnload = () => sendEnd();
152
+ document.addEventListener("visibilitychange", handleVisibility);
153
+ window.addEventListener("beforeunload", handleUnload);
154
+ return () => {
155
+ document.removeEventListener("visibilitychange", handleVisibility);
156
+ window.removeEventListener("beforeunload", handleUnload);
157
+ };
158
+ }, [apiUrl]);
123
159
  function enqueueOrRun(fn) {
124
160
  if (sessionId.current) {
161
+ localStorage.setItem("af_session_last_activity", String(Date.now()));
125
162
  fn();
126
163
  } else {
127
164
  pendingEvents.current.push(fn);
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  { "name": "@arthurreira/analytics",
2
- "version": "0.4.0",
2
+ "version": "0.5.0",
3
3
  "type": "module",
4
4
  "scripts": {
5
5
  "build": "tsup",