@base44-preview/sdk 0.8.6-pr.57.faeb046 → 0.8.7-pr.59.b7218c8

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_URL_PARAM_KEY = "analytics-disable";
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 getAnalyticsConfigFromUrlParams(): AnalyticsModuleOptions | undefined;
18
+ export declare function getAnalyticsSessionId(): string;
@@ -1,4 +1,15 @@
1
- import { getSharedInstance, } from "../utils/singleton";
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_URL_PARAM_KEY = "analytics-disable";
5
+ export const ANALYTICS_SESSION_ID_LOCAL_STORAGE_KEY = "base44_analytics_session_id";
6
+ const defaultConfiguration = {
7
+ enabled: true,
8
+ maxQueueSize: 1000,
9
+ throttleTime: 1000,
10
+ batchSize: 30,
11
+ heartBeatInterval: 60 * 1000,
12
+ };
2
13
  ///////////////////////////////////////////////
3
14
  //// shared queue for analytics events ////
4
15
  ///////////////////////////////////////////////
@@ -7,32 +18,25 @@ const ANALYTICS_SHARED_STATE_NAME = "analytics";
7
18
  const analyticsSharedState = getSharedInstance(ANALYTICS_SHARED_STATE_NAME, () => ({
8
19
  requestsQueue: [],
9
20
  isProcessing: false,
21
+ isHeartBeatProcessing: false,
22
+ sessionContext: null,
23
+ config: {
24
+ ...defaultConfiguration,
25
+ ...getAnalyticsConfigFromUrlParams(),
26
+ },
10
27
  }));
11
28
  export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthModule, }) => {
12
29
  var _a;
13
30
  // prevent overflow of events //
14
- const { enabled = true, maxQueueSize = 1000, throttleTime = 1000, batchSize = 30, } = (_a = getAnalyticsModuleOptionsFromUrlParams()) !== null && _a !== void 0 ? _a : {};
15
- const trackBatchUrl = `${serverUrl}/api/apps/${appId}/analytics/track/batch`;
16
- let sessionContext = null;
17
- const getSessionContext = async () => {
18
- if (sessionContext)
19
- return sessionContext;
20
- const user = await userAuthModule.me();
21
- sessionContext = {
22
- user_id: user.id,
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: () => { },
23
36
  };
24
- return sessionContext;
25
- };
26
- const track = (params) => {
27
- if (!enabled || analyticsSharedState.requestsQueue.length >= maxQueueSize) {
28
- return;
29
- }
30
- const intrinsicData = getEventIntrinsicData();
31
- analyticsSharedState.requestsQueue.push({
32
- ...params,
33
- ...intrinsicData,
34
- });
35
- };
37
+ }
38
+ let clearHeartBeatProcessor = undefined;
39
+ const trackBatchUrl = `${serverUrl}/api/apps/${appId}/analytics/track/batch`;
36
40
  const batchRequestFallback = async (events) => {
37
41
  await axiosClient.request({
38
42
  method: "POST",
@@ -40,63 +44,118 @@ export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthM
40
44
  data: { events },
41
45
  });
42
46
  };
47
+ const beaconRequest = async (events) => {
48
+ const beaconPayload = JSON.stringify({ events });
49
+ try {
50
+ const blob = new Blob([beaconPayload], { type: "application/json" });
51
+ return (typeof navigator === "undefined" ||
52
+ beaconPayload.length > 60000 ||
53
+ !navigator.sendBeacon(trackBatchUrl, blob));
54
+ }
55
+ catch (_a) {
56
+ return false;
57
+ }
58
+ };
43
59
  const flush = async (eventsData) => {
44
- const sessionContext_ = sessionContext !== null && sessionContext !== void 0 ? sessionContext : (await getSessionContext());
60
+ const sessionContext_ = await getSessionContext(userAuthModule);
45
61
  const events = eventsData.map(transformEventDataToApiRequestData(sessionContext_));
46
- const beaconPayload = JSON.stringify({ events });
47
- if (typeof navigator === "undefined" ||
48
- beaconPayload.length > 60000 ||
49
- !navigator.sendBeacon(trackBatchUrl, beaconPayload)) {
50
- // beacon didn't work, fallback to axios
51
- await batchRequestFallback(events);
62
+ if (!(await beaconRequest(events))) {
63
+ return batchRequestFallback(events);
52
64
  }
53
65
  };
54
- if (typeof window !== "undefined" && enabled) {
55
- window.addEventListener("visibilitychange", () => {
56
- if (document.visibilityState === "hidden") {
57
- analyticsSharedState.isProcessing = false;
58
- // flush entire queue on visibility change and hope for the best //
59
- const eventsData = analyticsSharedState.requestsQueue.splice(0);
60
- flush(eventsData);
61
- }
62
- else if (document.visibilityState === "visible") {
63
- startAnalyticsProcessor(flush, {
64
- throttleTime,
65
- batchSize,
66
- });
67
- }
66
+ const startProcessing = () => {
67
+ startAnalyticsProcessor(flush, {
68
+ throttleTime,
69
+ batchSize,
68
70
  });
69
- }
70
- // start analytics processor only if it's the first instance and analytics is enabled //
71
- if (enabled) {
71
+ };
72
+ const track = (params) => {
73
+ if (analyticsSharedState.requestsQueue.length >= maxQueueSize) {
74
+ return;
75
+ }
76
+ const intrinsicData = getEventIntrinsicData();
77
+ analyticsSharedState.requestsQueue.push({
78
+ ...params,
79
+ ...intrinsicData,
80
+ });
81
+ startProcessing();
82
+ };
83
+ const onDocVisible = () => {
72
84
  startAnalyticsProcessor(flush, {
73
85
  throttleTime,
74
86
  batchSize,
75
87
  });
88
+ clearHeartBeatProcessor = startHeartBeatProcessor(track);
89
+ };
90
+ const onDocHidden = () => {
91
+ stopAnalyticsProcessor();
92
+ // flush entire queue on visibility change and hope for the best //
93
+ const eventsData = analyticsSharedState.requestsQueue.splice(0);
94
+ flush(eventsData);
95
+ clearHeartBeatProcessor === null || clearHeartBeatProcessor === void 0 ? void 0 : clearHeartBeatProcessor();
96
+ };
97
+ const onVisibilityChange = () => {
98
+ if (typeof window === "undefined")
99
+ return;
100
+ if (document.visibilityState === "hidden") {
101
+ onDocHidden();
102
+ }
103
+ else if (document.visibilityState === "visible") {
104
+ onDocVisible();
105
+ }
106
+ };
107
+ const cleanup = () => {
108
+ stopAnalyticsProcessor();
109
+ clearHeartBeatProcessor === null || clearHeartBeatProcessor === void 0 ? void 0 : clearHeartBeatProcessor();
110
+ if (typeof window !== "undefined") {
111
+ window.removeEventListener("visibilitychange", onVisibilityChange);
112
+ }
113
+ };
114
+ // start the flusing process ///
115
+ startProcessing();
116
+ // start the heart beat processor //
117
+ clearHeartBeatProcessor = startHeartBeatProcessor(track);
118
+ // start the visibility change listener //
119
+ if (typeof window !== "undefined") {
120
+ window.addEventListener("visibilitychange", onVisibilityChange);
76
121
  }
77
122
  return {
78
123
  track,
124
+ cleanup,
79
125
  };
80
126
  };
127
+ function stopAnalyticsProcessor() {
128
+ analyticsSharedState.isProcessing = false;
129
+ }
81
130
  async function startAnalyticsProcessor(handleTrack, options) {
82
131
  if (analyticsSharedState.isProcessing) {
132
+ // only one instance of the analytics processor can be running at a time //
83
133
  return;
84
134
  }
85
135
  analyticsSharedState.isProcessing = true;
86
136
  const { throttleTime = 1000, batchSize = 30 } = options !== null && options !== void 0 ? options : {};
87
- while (analyticsSharedState.isProcessing) {
137
+ while (analyticsSharedState.isProcessing &&
138
+ analyticsSharedState.requestsQueue.length > 0) {
88
139
  const requests = analyticsSharedState.requestsQueue.splice(0, batchSize);
89
- if (requests.length > 0) {
90
- try {
91
- await handleTrack(requests);
92
- }
93
- catch (error) {
94
- // TODO: think about retries if needed
95
- console.error("Error processing analytics request:", error);
96
- }
97
- }
140
+ requests.length && (await handleTrack(requests));
98
141
  await new Promise((resolve) => setTimeout(resolve, throttleTime));
99
142
  }
143
+ analyticsSharedState.isProcessing = false;
144
+ }
145
+ function startHeartBeatProcessor(track) {
146
+ var _a;
147
+ if (analyticsSharedState.isHeartBeatProcessing ||
148
+ ((_a = analyticsSharedState.config.heartBeatInterval) !== null && _a !== void 0 ? _a : 0) < 10) {
149
+ return () => { };
150
+ }
151
+ analyticsSharedState.isHeartBeatProcessing = true;
152
+ const interval = setInterval(() => {
153
+ track({ eventName: USER_HEARTBEAT_EVENT_NAME });
154
+ }, analyticsSharedState.config.heartBeatInterval);
155
+ return () => {
156
+ clearInterval(interval);
157
+ analyticsSharedState.isHeartBeatProcessing = false;
158
+ };
100
159
  }
101
160
  function getEventIntrinsicData() {
102
161
  return {
@@ -113,15 +172,57 @@ function transformEventDataToApiRequestData(sessionContext) {
113
172
  ...sessionContext,
114
173
  });
115
174
  }
116
- export function getAnalyticsModuleOptionsFromUrlParams() {
175
+ let sessionContextPromise = null;
176
+ async function getSessionContext(userAuthModule) {
177
+ if (!analyticsSharedState.sessionContext) {
178
+ if (!sessionContextPromise) {
179
+ const sessionId = getAnalyticsSessionId();
180
+ sessionContextPromise = userAuthModule
181
+ .me()
182
+ .then((user) => ({
183
+ user_id: user.id,
184
+ session_id: sessionId,
185
+ }))
186
+ .catch(() => ({
187
+ user_id: null,
188
+ session_id: sessionId,
189
+ }));
190
+ }
191
+ analyticsSharedState.sessionContext = await sessionContextPromise;
192
+ }
193
+ return analyticsSharedState.sessionContext;
194
+ }
195
+ export function getAnalyticsConfigFromUrlParams() {
196
+ if (typeof window === "undefined")
197
+ return undefined;
117
198
  const urlParams = new URLSearchParams(window.location.search);
118
- const jsonString = urlParams.get("analytics");
119
- if (!jsonString)
199
+ const analyticsDisable = urlParams.get(ANALYTICS_CONFIG_URL_PARAM_KEY);
200
+ // if the url param is not set, return undefined //
201
+ if (analyticsDisable == null || !analyticsDisable.length)
120
202
  return undefined;
203
+ // remove the url param from the url //
204
+ const newUrlParams = new URLSearchParams(window.location.search);
205
+ newUrlParams.delete(ANALYTICS_CONFIG_URL_PARAM_KEY);
206
+ const newUrl = window.location.pathname +
207
+ (newUrlParams.toString() ? "?" + newUrlParams.toString() : "");
208
+ window.history.replaceState({}, "", newUrl);
209
+ // return the config object //
210
+ return analyticsDisable === "true" ? { enabled: false } : undefined;
211
+ }
212
+ export function getAnalyticsSessionId() {
213
+ if (typeof window === "undefined") {
214
+ return generateUuid();
215
+ }
121
216
  try {
122
- return JSON.parse(jsonString);
217
+ const sessionId = localStorage.getItem(ANALYTICS_SESSION_ID_LOCAL_STORAGE_KEY);
218
+ if (!sessionId) {
219
+ const newSessionId = generateUuid();
220
+ localStorage.setItem(ANALYTICS_SESSION_ID_LOCAL_STORAGE_KEY, newSessionId);
221
+ return newSessionId;
222
+ }
223
+ return sessionId;
123
224
  }
124
225
  catch (_a) {
125
- return undefined;
226
+ return generateUuid();
126
227
  }
127
228
  }
@@ -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,4 +35,8 @@ export type AnalyticsModuleOptions = {
34
35
  maxQueueSize?: number;
35
36
  throttleTime?: number;
36
37
  batchSize?: number;
38
+ heartBeatInterval?: number;
39
+ };
40
+ export type AnalyticsModule = {
41
+ track: (params: TrackEventParams) => void;
37
42
  };
@@ -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
+ };
@@ -1,2 +1 @@
1
1
  export declare function getSharedInstance<T>(name: string, factory: () => T): T;
2
- export declare function getSharedInstanceRefCount<T>(name: string): number;
@@ -0,0 +1,15 @@
1
+ const windowObj = typeof window !== "undefined"
2
+ ? window
3
+ : { base44SharedInstances: {} };
4
+ // Singleton (shared between sdk instances)//
5
+ export function getSharedInstance(name, factory) {
6
+ if (!windowObj.base44SharedInstances) {
7
+ windowObj.base44SharedInstances = {};
8
+ }
9
+ if (!windowObj.base44SharedInstances[name]) {
10
+ windowObj.base44SharedInstances[name] = {
11
+ instance: factory(),
12
+ };
13
+ }
14
+ return windowObj.base44SharedInstances[name].instance;
15
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44-preview/sdk",
3
- "version": "0.8.6-pr.57.faeb046",
3
+ "version": "0.8.7-pr.59.b7218c8",
4
4
  "description": "JavaScript SDK for Base44 API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,17 +0,0 @@
1
- // Singleton (shared between sdk instances)//
2
- export function getSharedInstance(name, factory) {
3
- const windowObj = typeof window !== "undefined" ? window : { base44: {} };
4
- if (!windowObj.base44) {
5
- windowObj.base44 = {};
6
- }
7
- if (!windowObj.base44[name]) {
8
- windowObj.base44[name] = { instance: factory(), _refCount: 0 };
9
- }
10
- windowObj.base44[name]._refCount++;
11
- return windowObj.base44[name].instance;
12
- }
13
- export function getSharedInstanceRefCount(name) {
14
- var _a, _b, _c;
15
- const windowObj = typeof window !== "undefined" ? window : { base44: {} };
16
- return (_c = (_b = (_a = windowObj.base44) === null || _a === void 0 ? void 0 : _a[name]) === null || _b === void 0 ? void 0 : _b._refCount) !== null && _c !== void 0 ? _c : 0;
17
- }