@base44-preview/sdk 0.8.6-pr.57.346f67a → 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.
package/dist/client.js CHANGED
@@ -124,6 +124,7 @@ export function createClient(config) {
124
124
  userAuthModule,
125
125
  }),
126
126
  cleanup: () => {
127
+ userModules.analytics.cleanup();
127
128
  if (socket) {
128
129
  socket.disconnect();
129
130
  }
@@ -6,6 +6,7 @@ import type { ConnectorsModule } from "./modules/connectors.types.js";
6
6
  import type { FunctionsModule } from "./modules/functions.types.js";
7
7
  import type { AgentsModule } from "./modules/agents.types.js";
8
8
  import type { AppLogsModule } from "./modules/app-logs.types.js";
9
+ import type { AnalyticsModule } from "./modules/analytics.types.js";
9
10
  /**
10
11
  * Options for creating a Base44 client.
11
12
  */
@@ -82,6 +83,8 @@ export interface Base44Client {
82
83
  agents: AgentsModule;
83
84
  /** {@link AppLogsModule | App logs module} for tracking app usage. */
84
85
  appLogs: AppLogsModule;
86
+ /** {@link AnalyticsModule | Analytics module} for tracking app usage. */
87
+ analytics: AnalyticsModule;
85
88
  /** Cleanup function to disconnect WebSocket connections. Call when you're done with the client. */
86
89
  cleanup: () => void;
87
90
  /**
@@ -1,6 +1,9 @@
1
1
  import { AxiosInstance } from "axios";
2
2
  import { TrackEventParams, AnalyticsModuleOptions } from "./analytics.types";
3
3
  import type { AuthModule } from "./auth.types";
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";
4
7
  export interface AnalyticsModuleArgs {
5
8
  axiosClient: AxiosInstance;
6
9
  serverUrl: string;
@@ -9,5 +12,7 @@ export interface AnalyticsModuleArgs {
9
12
  }
10
13
  export declare const createAnalyticsModule: ({ axiosClient, serverUrl, appId, userAuthModule, }: AnalyticsModuleArgs) => {
11
14
  track: (params: TrackEventParams) => void;
15
+ cleanup: () => void;
12
16
  };
13
- export declare function getAnalyticsModuleOptionsFromUrlParams(): AnalyticsModuleOptions | undefined;
17
+ export declare function getAnalyticsModuleOptionsFromLocalStorage(): AnalyticsModuleOptions | undefined;
18
+ export declare function getAnalyticsSessionId(): string;
@@ -1,9 +1,14 @@
1
1
  import { getSharedInstance } from "../utils/sharedInstance";
2
+ import { generateUuid } from "../utils/common";
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";
2
6
  const defaultConfiguration = {
3
7
  enabled: true,
4
8
  maxQueueSize: 1000,
5
9
  throttleTime: 1000,
6
10
  batchSize: 30,
11
+ heartBeatInterval: 60 * 1000,
7
12
  };
8
13
  ///////////////////////////////////////////////
9
14
  //// shared queue for analytics events ////
@@ -13,35 +18,25 @@ const ANALYTICS_SHARED_STATE_NAME = "analytics";
13
18
  const analyticsSharedState = getSharedInstance(ANALYTICS_SHARED_STATE_NAME, () => ({
14
19
  requestsQueue: [],
15
20
  isProcessing: false,
21
+ isHeartBeatProcessing: false,
16
22
  sessionContext: null,
17
23
  config: {
18
24
  ...defaultConfiguration,
19
- ...getAnalyticsModuleOptionsFromUrlParams(),
25
+ ...getAnalyticsModuleOptionsFromLocalStorage(),
20
26
  },
21
27
  }));
22
28
  export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthModule, }) => {
29
+ var _a;
23
30
  // prevent overflow of events //
24
- 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
+ }
38
+ let clearHeartBeatProcessor = undefined;
25
39
  const trackBatchUrl = `${serverUrl}/api/apps/${appId}/analytics/track/batch`;
26
- const getSessionContext = async () => {
27
- if (!analyticsSharedState.sessionContext) {
28
- const user = await userAuthModule.me();
29
- analyticsSharedState.sessionContext = {
30
- user_id: user.id,
31
- };
32
- }
33
- return analyticsSharedState.sessionContext;
34
- };
35
- const track = (params) => {
36
- if (!enabled || analyticsSharedState.requestsQueue.length >= maxQueueSize) {
37
- return;
38
- }
39
- const intrinsicData = getEventIntrinsicData();
40
- analyticsSharedState.requestsQueue.push({
41
- ...params,
42
- ...intrinsicData,
43
- });
44
- };
45
40
  const batchRequestFallback = async (events) => {
46
41
  await axiosClient.request({
47
42
  method: "POST",
@@ -50,27 +45,51 @@ export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthM
50
45
  });
51
46
  };
52
47
  const flush = async (eventsData) => {
53
- const sessionContext_ = await getSessionContext();
48
+ const sessionContext_ = await getSessionContext(userAuthModule);
54
49
  const events = eventsData.map(transformEventDataToApiRequestData(sessionContext_));
55
50
  const beaconPayload = JSON.stringify({ events });
56
- if (typeof navigator === "undefined" ||
57
- beaconPayload.length > 60000 ||
58
- !navigator.sendBeacon(trackBatchUrl, beaconPayload)) {
59
- // beacon didn't work, fallback to axios
60
- await batchRequestFallback(events);
51
+ try {
52
+ if (typeof navigator === "undefined" ||
53
+ beaconPayload.length > 60000 ||
54
+ !navigator.sendBeacon(trackBatchUrl, beaconPayload)) {
55
+ // beacon didn't work, fallback to axios
56
+ await batchRequestFallback(events);
57
+ }
58
+ }
59
+ catch (_a) {
60
+ // TODO: think about retries if needed
61
61
  }
62
62
  };
63
- const onDocHidden = () => {
64
- analyticsSharedState.isProcessing = false;
65
- // flush entire queue on visibility change and hope for the best //
66
- const eventsData = analyticsSharedState.requestsQueue.splice(0);
67
- flush(eventsData);
63
+ const startProcessing = () => {
64
+ startAnalyticsProcessor(flush, {
65
+ throttleTime,
66
+ batchSize,
67
+ });
68
+ };
69
+ const track = (params) => {
70
+ if (analyticsSharedState.requestsQueue.length >= maxQueueSize) {
71
+ return;
72
+ }
73
+ const intrinsicData = getEventIntrinsicData();
74
+ analyticsSharedState.requestsQueue.push({
75
+ ...params,
76
+ ...intrinsicData,
77
+ });
78
+ startProcessing();
68
79
  };
69
80
  const onDocVisible = () => {
70
81
  startAnalyticsProcessor(flush, {
71
82
  throttleTime,
72
83
  batchSize,
73
84
  });
85
+ clearHeartBeatProcessor = startHeartBeatProcessor(track);
86
+ };
87
+ const onDocHidden = () => {
88
+ stopAnalyticsProcessor();
89
+ // flush entire queue on visibility change and hope for the best //
90
+ const eventsData = analyticsSharedState.requestsQueue.splice(0);
91
+ flush(eventsData);
92
+ clearHeartBeatProcessor === null || clearHeartBeatProcessor === void 0 ? void 0 : clearHeartBeatProcessor();
74
93
  };
75
94
  const onVisibilityChange = () => {
76
95
  if (typeof window === "undefined")
@@ -82,39 +101,58 @@ export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthM
82
101
  onDocVisible();
83
102
  }
84
103
  };
85
- if (typeof window !== "undefined" && enabled) {
104
+ const cleanup = () => {
105
+ stopAnalyticsProcessor();
106
+ clearHeartBeatProcessor === null || clearHeartBeatProcessor === void 0 ? void 0 : clearHeartBeatProcessor();
107
+ if (typeof window !== "undefined") {
108
+ window.removeEventListener("visibilitychange", onVisibilityChange);
109
+ }
110
+ };
111
+ // start the flusing process ///
112
+ startProcessing();
113
+ // start the heart beat processor //
114
+ clearHeartBeatProcessor = startHeartBeatProcessor(track);
115
+ // start the visibility change listener //
116
+ if (typeof window !== "undefined") {
86
117
  window.addEventListener("visibilitychange", onVisibilityChange);
87
118
  }
88
- // start analytics processor only if it's the first instance and analytics is enabled //
89
- if (enabled) {
90
- startAnalyticsProcessor(flush, {
91
- throttleTime,
92
- batchSize,
93
- });
94
- }
95
119
  return {
96
120
  track,
121
+ cleanup,
97
122
  };
98
123
  };
124
+ function stopAnalyticsProcessor() {
125
+ analyticsSharedState.isProcessing = false;
126
+ }
99
127
  async function startAnalyticsProcessor(handleTrack, options) {
100
128
  if (analyticsSharedState.isProcessing) {
129
+ // only one instance of the analytics processor can be running at a time //
101
130
  return;
102
131
  }
103
132
  analyticsSharedState.isProcessing = true;
104
133
  const { throttleTime = 1000, batchSize = 30 } = options !== null && options !== void 0 ? options : {};
105
- while (analyticsSharedState.isProcessing) {
134
+ while (analyticsSharedState.isProcessing &&
135
+ analyticsSharedState.requestsQueue.length > 0) {
106
136
  const requests = analyticsSharedState.requestsQueue.splice(0, batchSize);
107
- if (requests.length > 0) {
108
- try {
109
- await handleTrack(requests);
110
- }
111
- catch (error) {
112
- // TODO: think about retries if needed
113
- console.error("Error processing analytics request:", error);
114
- }
115
- }
137
+ requests.length && (await handleTrack(requests));
116
138
  await new Promise((resolve) => setTimeout(resolve, throttleTime));
117
139
  }
140
+ analyticsSharedState.isProcessing = false;
141
+ }
142
+ function startHeartBeatProcessor(track) {
143
+ var _a;
144
+ if (analyticsSharedState.isHeartBeatProcessing ||
145
+ ((_a = analyticsSharedState.config.heartBeatInterval) !== null && _a !== void 0 ? _a : 0) < 10) {
146
+ return () => { };
147
+ }
148
+ analyticsSharedState.isHeartBeatProcessing = true;
149
+ const interval = setInterval(() => {
150
+ track({ eventName: USER_HEARTBEAT_EVENT_NAME });
151
+ }, analyticsSharedState.config.heartBeatInterval);
152
+ return () => {
153
+ clearInterval(interval);
154
+ analyticsSharedState.isHeartBeatProcessing = false;
155
+ };
118
156
  }
119
157
  function getEventIntrinsicData() {
120
158
  return {
@@ -131,17 +169,53 @@ function transformEventDataToApiRequestData(sessionContext) {
131
169
  ...sessionContext,
132
170
  });
133
171
  }
134
- export function getAnalyticsModuleOptionsFromUrlParams() {
172
+ let sessionContextPromise = null;
173
+ async function getSessionContext(userAuthModule) {
174
+ if (!analyticsSharedState.sessionContext) {
175
+ if (!sessionContextPromise) {
176
+ const sessionId = getAnalyticsSessionId();
177
+ sessionContextPromise = userAuthModule
178
+ .me()
179
+ .then((user) => ({
180
+ user_id: user.id,
181
+ session_id: sessionId,
182
+ }))
183
+ .catch(() => ({
184
+ user_id: null,
185
+ session_id: sessionId,
186
+ }));
187
+ }
188
+ analyticsSharedState.sessionContext = await sessionContextPromise;
189
+ }
190
+ return analyticsSharedState.sessionContext;
191
+ }
192
+ export function getAnalyticsModuleOptionsFromLocalStorage() {
135
193
  if (typeof window === "undefined")
136
194
  return undefined;
137
- const urlParams = new URLSearchParams(window.location.search);
138
- const jsonString = urlParams.get("analytics");
139
- if (!jsonString)
140
- return undefined;
141
195
  try {
196
+ const jsonString = localStorage.getItem(ANALYTICS_CONFIG_LOCAL_STORAGE_KEY);
197
+ if (!jsonString)
198
+ return undefined;
142
199
  return JSON.parse(jsonString);
143
200
  }
144
201
  catch (_a) {
145
202
  return undefined;
146
203
  }
147
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;
@@ -34,6 +35,7 @@ export type AnalyticsModuleOptions = {
34
35
  maxQueueSize?: number;
35
36
  throttleTime?: number;
36
37
  batchSize?: number;
38
+ heartBeatInterval?: number;
37
39
  };
38
40
  export type AnalyticsModule = {
39
41
  track: (params: TrackEventParams) => void;
@@ -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.346f67a",
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",