@arthurreira/analytics 0.22.0 → 0.23.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.
@@ -133,6 +133,16 @@ var AfAnalytics = (() => {
133
133
  }).catch(() => {
134
134
  });
135
135
  }
136
+ function sendHeartbeat(apiUrl, apiKey, sessionId) {
137
+ if (isOptedOut()) return;
138
+ fetch(`${apiUrl}/sessions/${sessionId}/heartbeat`, {
139
+ method: "PATCH",
140
+ headers: { "Content-Type": "application/json" },
141
+ body: JSON.stringify({ api_key: apiKey }),
142
+ keepalive: true
143
+ }).catch(() => {
144
+ });
145
+ }
136
146
  function baseFields(sessionId, eventType) {
137
147
  return {
138
148
  session_id: sessionId,
@@ -293,7 +303,10 @@ var AfAnalytics = (() => {
293
303
  function startSession(id) {
294
304
  sessionId = id;
295
305
  closeResponder = openSessionResponder(id);
296
- idleTimer = setInterval(touchSession, IDLE_TOUCH_INTERVAL_MS);
306
+ idleTimer = setInterval(() => {
307
+ touchSession();
308
+ sendHeartbeat(apiUrl, apiKey, id);
309
+ }, IDLE_TOUCH_INTERVAL_MS);
297
310
  pending.splice(0).forEach((fn) => fn(id));
298
311
  }
299
312
  function endSession() {
@@ -304,7 +317,7 @@ var AfAnalytics = (() => {
304
317
  idleTimer = null;
305
318
  }
306
319
  closeResponder();
307
- navigator.sendBeacon(`${apiUrl}/sessions/${sessionId}/end`);
320
+ navigator.sendBeacon(`${apiUrl}/sessions/${sessionId}/end`, JSON.stringify({ api_key: apiKey }));
308
321
  clearSession();
309
322
  }
310
323
  function restartSession() {
package/dist/client.js CHANGED
@@ -126,6 +126,17 @@ async function createSession(apiUrl, apiKey, visitorId) {
126
126
  return null;
127
127
  }
128
128
  }
129
+ async function sendHeartbeat(apiUrl, apiKey, sessionId) {
130
+ if (isOptedOut()) return;
131
+ try {
132
+ await fetch(`${apiUrl}/sessions/${sessionId}/heartbeat`, {
133
+ method: "PATCH",
134
+ headers: { "Content-Type": "application/json" },
135
+ body: JSON.stringify({ api_key: apiKey })
136
+ });
137
+ } catch {
138
+ }
139
+ }
129
140
  async function sendEvent(apiUrl, apiKey, payload) {
130
141
  if (isOptedOut()) return;
131
142
  await fetch(`${apiUrl}/events`, {
@@ -416,6 +427,7 @@ function useAnalytics(apiUrl, apiKey, currentPath, wsUrl, routeName) {
416
427
  idleTimerRef.current = setInterval(() => {
417
428
  if (!hasSentEnd.current) {
418
429
  localStorage.setItem("af_session_last_activity", String(Date.now()));
430
+ sendHeartbeat(apiUrl, apiKey, id);
419
431
  }
420
432
  }, IDLE_TOUCH_INTERVAL_MS);
421
433
  if (wsUrl) {
@@ -440,7 +452,7 @@ function useAnalytics(apiUrl, apiKey, currentPath, wsUrl, routeName) {
440
452
  closeResponderRef.current();
441
453
  presenceRef.current?.disconnect();
442
454
  presenceRef.current = null;
443
- navigator.sendBeacon(`${apiUrl}/sessions/${sessionId.current}/end`);
455
+ navigator.sendBeacon(`${apiUrl}/sessions/${sessionId.current}/end`, JSON.stringify({ api_key: apiKey }));
444
456
  localStorage.removeItem("af_session_id");
445
457
  localStorage.removeItem("af_session_last_activity");
446
458
  };
@@ -464,6 +476,7 @@ function useAnalytics(apiUrl, apiKey, currentPath, wsUrl, routeName) {
464
476
  idleTimerRef.current = setInterval(() => {
465
477
  if (!hasSentEnd.current) {
466
478
  localStorage.setItem("af_session_last_activity", String(Date.now()));
479
+ sendHeartbeat(apiUrl, apiKey, id);
467
480
  }
468
481
  }, IDLE_TOUCH_INTERVAL_MS);
469
482
  if (wsUrl) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arthurreira/analytics",
3
- "version": "0.22.0",
3
+ "version": "0.23.0",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "build": "tsup",