@arthurreira/analytics 0.16.0 → 0.16.1

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.
@@ -136,12 +136,20 @@ var AfAnalytics = (() => {
136
136
  utm_term: params.get("utm_term"),
137
137
  utm_content: params.get("utm_content")
138
138
  };
139
+ console.log(`[AF Analytics] Creating session at ${apiUrl}/sessions with API key: ${apiKey.slice(0, 10)}...`);
139
140
  const res = await fetch(`${apiUrl}/sessions`, {
140
141
  method: "POST",
141
142
  headers: { "X-API-Key": apiKey, "Content-Type": "application/json" },
142
143
  body: JSON.stringify(body)
143
144
  });
145
+ console.log(`[AF Analytics] Session creation response: ${res.status} ${res.statusText}`);
146
+ if (!res.ok) {
147
+ const errText = await res.text();
148
+ console.error(`[AF Analytics] Session creation failed: ${errText}`);
149
+ return "";
150
+ }
144
151
  const data = await res.json();
152
+ console.log(`[AF Analytics] Session created: ${data.id}`);
145
153
  return data.id;
146
154
  }
147
155
  async function getOrCreateSession(apiUrl, apiKey) {
package/dist/client.js CHANGED
@@ -51,16 +51,29 @@ async function createSession(apiUrl, apiKey, visitorId) {
51
51
  sessionData.utm_term = params.get("utm_term");
52
52
  sessionData.utm_content = params.get("utm_content");
53
53
  }
54
- const response = await fetch(`${apiUrl}/sessions`, {
55
- method: "POST",
56
- headers: {
57
- "X-API-Key": apiKey,
58
- "Content-Type": "application/json"
59
- },
60
- body: JSON.stringify(sessionData)
61
- });
62
- const data = await response.json();
63
- return data.id;
54
+ console.log(`[AF Analytics SDK] Creating session at ${apiUrl}/sessions with API key: ${apiKey.slice(0, 10)}...`);
55
+ try {
56
+ const response = await fetch(`${apiUrl}/sessions`, {
57
+ method: "POST",
58
+ headers: {
59
+ "X-API-Key": apiKey,
60
+ "Content-Type": "application/json"
61
+ },
62
+ body: JSON.stringify(sessionData)
63
+ });
64
+ console.log(`[AF Analytics SDK] Session creation response: ${response.status} ${response.statusText}`);
65
+ if (!response.ok) {
66
+ const errText = await response.text();
67
+ console.error(`[AF Analytics SDK] Session creation failed: ${errText}`);
68
+ return null;
69
+ }
70
+ const data = await response.json();
71
+ console.log(`[AF Analytics SDK] Session created: ${data.id}`);
72
+ return data.id ?? null;
73
+ } catch (err) {
74
+ console.error(`[AF Analytics SDK] Session creation error: ${err}`);
75
+ return null;
76
+ }
64
77
  }
65
78
  async function sendEvent(apiUrl, apiKey, payload) {
66
79
  await fetch(`${apiUrl}/events`, {
@@ -143,11 +156,15 @@ async function getOrCreateSession(apiUrl, apiKey, visitorId) {
143
156
  localStorage.setItem("af_session_last_activity", String(now));
144
157
  return storedSessionId;
145
158
  }
159
+ localStorage.removeItem("af_session_id");
160
+ localStorage.removeItem("af_session_last_activity");
146
161
  }
147
162
  if (_sessionFlight) return _sessionFlight;
148
163
  _sessionFlight = createSession(apiUrl, apiKey, visitorId).then((id) => {
149
- localStorage.setItem("af_session_id", id);
150
- localStorage.setItem("af_session_last_activity", String(Date.now()));
164
+ if (id) {
165
+ localStorage.setItem("af_session_id", id);
166
+ localStorage.setItem("af_session_last_activity", String(Date.now()));
167
+ }
151
168
  _sessionFlight = null;
152
169
  return id;
153
170
  }).catch((err) => {
@@ -167,7 +184,7 @@ function useAnalytics(apiUrl, apiKey) {
167
184
  localStorage.setItem("af_analytics_visitor_id", visitor_id);
168
185
  let cancelled = false;
169
186
  getOrCreateSession(apiUrl, apiKey, visitor_id).then((id) => {
170
- if (cancelled) return;
187
+ if (cancelled || !id) return;
171
188
  sessionId.current = id;
172
189
  pendingEvents.current.forEach((fn) => fn());
173
190
  pendingEvents.current = [];
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- declare function createSession(apiUrl: string, apiKey: string, visitorId?: string): Promise<any>;
1
+ declare function createSession(apiUrl: string, apiKey: string, visitorId?: string): Promise<string | null>;
2
2
  declare function trackPageview(apiUrl: string, apiKey: string, sessionId: string, path: string): Promise<void>;
3
3
  declare function trackClick(apiUrl: string, apiKey: string, sessionId: string, path: string, e: MouseEvent, element: HTMLElement): Promise<void>;
4
4
  declare function trackScroll(apiUrl: string, apiKey: string, sessionId: string, path: string, depth: number): Promise<void>;
package/dist/index.js CHANGED
@@ -42,16 +42,29 @@ async function createSession(apiUrl, apiKey, visitorId) {
42
42
  sessionData.utm_term = params.get("utm_term");
43
43
  sessionData.utm_content = params.get("utm_content");
44
44
  }
45
- const response = await fetch(`${apiUrl}/sessions`, {
46
- method: "POST",
47
- headers: {
48
- "X-API-Key": apiKey,
49
- "Content-Type": "application/json"
50
- },
51
- body: JSON.stringify(sessionData)
52
- });
53
- const data = await response.json();
54
- return data.id;
45
+ console.log(`[AF Analytics SDK] Creating session at ${apiUrl}/sessions with API key: ${apiKey.slice(0, 10)}...`);
46
+ try {
47
+ const response = await fetch(`${apiUrl}/sessions`, {
48
+ method: "POST",
49
+ headers: {
50
+ "X-API-Key": apiKey,
51
+ "Content-Type": "application/json"
52
+ },
53
+ body: JSON.stringify(sessionData)
54
+ });
55
+ console.log(`[AF Analytics SDK] Session creation response: ${response.status} ${response.statusText}`);
56
+ if (!response.ok) {
57
+ const errText = await response.text();
58
+ console.error(`[AF Analytics SDK] Session creation failed: ${errText}`);
59
+ return null;
60
+ }
61
+ const data = await response.json();
62
+ console.log(`[AF Analytics SDK] Session created: ${data.id}`);
63
+ return data.id ?? null;
64
+ } catch (err) {
65
+ console.error(`[AF Analytics SDK] Session creation error: ${err}`);
66
+ return null;
67
+ }
55
68
  }
56
69
  async function sendEvent(apiUrl, apiKey, payload) {
57
70
  await fetch(`${apiUrl}/events`, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arthurreira/analytics",
3
- "version": "0.16.0",
3
+ "version": "0.16.1",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "build": "tsup",