@base44-preview/sdk 0.8.6-pr.57.2eea21a → 0.8.6-pr.57.5bc7e40

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.
@@ -2,6 +2,8 @@ import { AxiosInstance } from "axios";
2
2
  import { TrackEventParams, AnalyticsModuleOptions } from "./analytics.types";
3
3
  import type { AuthModule } from "./auth.types";
4
4
  export declare const USER_HEARTBEAT_EVENT_NAME = "__user_heartbeat_event__";
5
+ export declare const ANALYTICS_CONFIG_LOCAL_STORAGE_KEY = "base44_analytics_config";
6
+ export declare const ANALYTICS_SESSION_ID_LOCAL_STORAGE_KEY = "base44_analytics_session_id";
5
7
  export interface AnalyticsModuleArgs {
6
8
  axiosClient: AxiosInstance;
7
9
  serverUrl: string;
@@ -13,3 +15,4 @@ export declare const createAnalyticsModule: ({ axiosClient, serverUrl, appId, us
13
15
  cleanup: () => void;
14
16
  };
15
17
  export declare function getAnalyticsModuleOptionsFromLocalStorage(): AnalyticsModuleOptions | undefined;
18
+ export declare function getAnalyticsSessionId(): string;
@@ -1,5 +1,8 @@
1
1
  import { getSharedInstance } from "../utils/sharedInstance";
2
+ import { generateUuid } from "../utils/common";
2
3
  export const USER_HEARTBEAT_EVENT_NAME = "__user_heartbeat_event__";
4
+ export const ANALYTICS_CONFIG_LOCAL_STORAGE_KEY = "base44_analytics_config";
5
+ export const ANALYTICS_SESSION_ID_LOCAL_STORAGE_KEY = "base44_analytics_session_id";
3
6
  const defaultConfiguration = {
4
7
  enabled: true,
5
8
  maxQueueSize: 1000,
@@ -23,8 +26,15 @@ const analyticsSharedState = getSharedInstance(ANALYTICS_SHARED_STATE_NAME, () =
23
26
  },
24
27
  }));
25
28
  export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthModule, }) => {
29
+ var _a;
26
30
  // prevent overflow of events //
27
- const { enabled, maxQueueSize, throttleTime, batchSize } = analyticsSharedState.config;
31
+ const { maxQueueSize, throttleTime, batchSize } = analyticsSharedState.config;
32
+ if (!((_a = analyticsSharedState.config) === null || _a === void 0 ? void 0 : _a.enabled)) {
33
+ return {
34
+ track: () => { },
35
+ cleanup: () => { },
36
+ };
37
+ }
28
38
  let clearHeartBeatProcessor = undefined;
29
39
  const trackBatchUrl = `${serverUrl}/api/apps/${appId}/analytics/track/batch`;
30
40
  const batchRequestFallback = async (events) => {
@@ -51,15 +61,13 @@ export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthM
51
61
  }
52
62
  };
53
63
  const startProcessing = () => {
54
- if (!enabled)
55
- return;
56
64
  startAnalyticsProcessor(flush, {
57
65
  throttleTime,
58
66
  batchSize,
59
67
  });
60
68
  };
61
69
  const track = (params) => {
62
- if (!enabled || analyticsSharedState.requestsQueue.length >= maxQueueSize) {
70
+ if (analyticsSharedState.requestsQueue.length >= maxQueueSize) {
63
71
  return;
64
72
  }
65
73
  const intrinsicData = getEventIntrinsicData();
@@ -105,7 +113,7 @@ export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthM
105
113
  // start the heart beat processor //
106
114
  clearHeartBeatProcessor = startHeartBeatProcessor(track);
107
115
  // start the visibility change listener //
108
- if (typeof window !== "undefined" && enabled) {
116
+ if (typeof window !== "undefined") {
109
117
  window.addEventListener("visibilitychange", onVisibilityChange);
110
118
  }
111
119
  return {
@@ -165,13 +173,16 @@ let sessionContextPromise = null;
165
173
  async function getSessionContext(userAuthModule) {
166
174
  if (!analyticsSharedState.sessionContext) {
167
175
  if (!sessionContextPromise) {
176
+ const sessionId = getAnalyticsSessionId();
168
177
  sessionContextPromise = userAuthModule
169
178
  .me()
170
179
  .then((user) => ({
171
180
  user_id: user.id,
181
+ session_id: sessionId,
172
182
  }))
173
183
  .catch(() => ({
174
- user_id: "unknown: error getting session context",
184
+ user_id: null,
185
+ session_id: sessionId,
175
186
  }));
176
187
  }
177
188
  analyticsSharedState.sessionContext = await sessionContextPromise;
@@ -182,7 +193,7 @@ export function getAnalyticsModuleOptionsFromLocalStorage() {
182
193
  if (typeof window === "undefined")
183
194
  return undefined;
184
195
  try {
185
- const jsonString = localStorage.getItem("base44_analytics_config");
196
+ const jsonString = localStorage.getItem(ANALYTICS_CONFIG_LOCAL_STORAGE_KEY);
186
197
  if (!jsonString)
187
198
  return undefined;
188
199
  return JSON.parse(jsonString);
@@ -191,3 +202,20 @@ export function getAnalyticsModuleOptionsFromLocalStorage() {
191
202
  return undefined;
192
203
  }
193
204
  }
205
+ export function getAnalyticsSessionId() {
206
+ if (typeof window === "undefined") {
207
+ return generateUuid();
208
+ }
209
+ try {
210
+ const sessionId = localStorage.getItem(ANALYTICS_SESSION_ID_LOCAL_STORAGE_KEY);
211
+ if (!sessionId) {
212
+ const newSessionId = generateUuid();
213
+ localStorage.setItem(ANALYTICS_SESSION_ID_LOCAL_STORAGE_KEY, newSessionId);
214
+ return newSessionId;
215
+ }
216
+ return sessionId;
217
+ }
218
+ catch (_a) {
219
+ return generateUuid();
220
+ }
221
+ }
@@ -15,6 +15,7 @@ export type TrackEventData = {
15
15
  } & TrackEventIntrinsicData;
16
16
  export type SessionContext = {
17
17
  user_id?: string | null;
18
+ session_id?: string | null;
18
19
  };
19
20
  export type AnalyticsApiRequestData = {
20
21
  event_name: string;
@@ -1,2 +1,3 @@
1
1
  export declare const isNode: boolean;
2
2
  export declare const isInIFrame: boolean;
3
+ export declare const generateUuid: () => string;
@@ -1,2 +1,6 @@
1
1
  export const isNode = typeof window === "undefined";
2
2
  export const isInIFrame = !isNode && window.self !== window.top;
3
+ export const generateUuid = () => {
4
+ return (Math.random().toString(36).substring(2, 15) +
5
+ Math.random().toString(36).substring(2, 15));
6
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44-preview/sdk",
3
- "version": "0.8.6-pr.57.2eea21a",
3
+ "version": "0.8.6-pr.57.5bc7e40",
4
4
  "description": "JavaScript SDK for Base44 API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",