@base44-preview/sdk 0.8.6-pr.57.aa9ac21 → 0.8.6-pr.57.bb6a61e

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
  /**
@@ -9,5 +9,6 @@ export interface AnalyticsModuleArgs {
9
9
  }
10
10
  export declare const createAnalyticsModule: ({ axiosClient, serverUrl, appId, userAuthModule, }: AnalyticsModuleArgs) => {
11
11
  track: (params: TrackEventParams) => void;
12
+ cleanup: () => void;
12
13
  };
13
14
  export declare function getAnalyticsModuleOptionsFromUrlParams(): AnalyticsModuleOptions | undefined;
@@ -1,33 +1,36 @@
1
- import { getSharedInstance, getSharedInstanceRefCount, } from "../utils/singleton";
1
+ import { getSharedInstance } from "../utils/sharedInstance";
2
+ const defaultConfiguration = {
3
+ enabled: true,
4
+ maxQueueSize: 1000,
5
+ throttleTime: 1000,
6
+ batchSize: 30,
7
+ };
8
+ ///////////////////////////////////////////////
9
+ //// shared queue for analytics events ////
10
+ ///////////////////////////////////////////////
2
11
  const ANALYTICS_SHARED_STATE_NAME = "analytics";
3
12
  // shared state//
4
13
  const analyticsSharedState = getSharedInstance(ANALYTICS_SHARED_STATE_NAME, () => ({
5
14
  requestsQueue: [],
15
+ isProcessing: false,
16
+ sessionContext: null,
17
+ config: {
18
+ ...defaultConfiguration,
19
+ ...getAnalyticsModuleOptionsFromUrlParams(),
20
+ },
6
21
  }));
7
22
  export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthModule, }) => {
8
- var _a;
9
23
  // prevent overflow of events //
10
- const { enabled = true, maxQueueSize = 1000, throttleTime = 1000, batchSize = 30, } = (_a = getAnalyticsModuleOptionsFromUrlParams()) !== null && _a !== void 0 ? _a : {};
24
+ const { enabled, maxQueueSize, throttleTime, batchSize } = analyticsSharedState.config;
11
25
  const trackBatchUrl = `${serverUrl}/api/apps/${appId}/analytics/track/batch`;
12
- let sessionContext = null;
13
26
  const getSessionContext = async () => {
14
- if (sessionContext)
15
- return sessionContext;
16
- const user = await userAuthModule.me();
17
- sessionContext = {
18
- user_id: user.id,
19
- };
20
- return sessionContext;
21
- };
22
- const track = (params) => {
23
- if (!enabled || analyticsSharedState.requestsQueue.length >= maxQueueSize) {
24
- return;
27
+ if (!analyticsSharedState.sessionContext) {
28
+ const user = await userAuthModule.me();
29
+ analyticsSharedState.sessionContext = {
30
+ user_id: user.id,
31
+ };
25
32
  }
26
- const intrinsicData = getEventIntrinsicData();
27
- analyticsSharedState.requestsQueue.push({
28
- ...params,
29
- ...intrinsicData,
30
- });
33
+ return analyticsSharedState.sessionContext;
31
34
  };
32
35
  const batchRequestFallback = async (events) => {
33
36
  await axiosClient.request({
@@ -37,7 +40,7 @@ export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthM
37
40
  });
38
41
  };
39
42
  const flush = async (eventsData) => {
40
- const sessionContext_ = sessionContext !== null && sessionContext !== void 0 ? sessionContext : (await getSessionContext());
43
+ const sessionContext_ = await getSessionContext();
41
44
  const events = eventsData.map(transformEventDataToApiRequestData(sessionContext_));
42
45
  const beaconPayload = JSON.stringify({ events });
43
46
  if (typeof navigator === "undefined" ||
@@ -47,28 +50,75 @@ export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthM
47
50
  await batchRequestFallback(events);
48
51
  }
49
52
  };
50
- if (typeof window !== "undefined") {
51
- window.addEventListener("visibilitychange", () => {
52
- // flush entire queue on visibility change and hope for the best //
53
- const eventsData = analyticsSharedState.requestsQueue.splice(0);
54
- flush(eventsData);
53
+ const onDocHidden = () => {
54
+ stopAnalyticsProcessor();
55
+ // flush entire queue on visibility change and hope for the best //
56
+ const eventsData = analyticsSharedState.requestsQueue.splice(0);
57
+ flush(eventsData);
58
+ };
59
+ const onDocVisible = () => {
60
+ startAnalyticsProcessor(flush, {
61
+ throttleTime,
62
+ batchSize,
55
63
  });
64
+ };
65
+ const onVisibilityChange = () => {
66
+ if (typeof window === "undefined")
67
+ return;
68
+ if (document.visibilityState === "hidden") {
69
+ onDocHidden();
70
+ }
71
+ else if (document.visibilityState === "visible") {
72
+ onDocVisible();
73
+ }
74
+ };
75
+ if (typeof window !== "undefined" && enabled) {
76
+ window.addEventListener("visibilitychange", onVisibilityChange);
56
77
  }
57
- // start analytics processor only if it's the first instance and analytics is enabled //
58
- if (getSharedInstanceRefCount(ANALYTICS_SHARED_STATE_NAME) <= 1 && enabled) {
78
+ const startProcessing = () => {
79
+ if (!enabled)
80
+ return;
59
81
  startAnalyticsProcessor(flush, {
60
82
  throttleTime,
61
83
  batchSize,
62
84
  });
63
- }
85
+ };
86
+ const track = (params) => {
87
+ if (!enabled || analyticsSharedState.requestsQueue.length >= maxQueueSize) {
88
+ return;
89
+ }
90
+ const intrinsicData = getEventIntrinsicData();
91
+ analyticsSharedState.requestsQueue.push({
92
+ ...params,
93
+ ...intrinsicData,
94
+ });
95
+ startProcessing();
96
+ };
97
+ const cleanup = () => {
98
+ if (typeof window === "undefined")
99
+ return;
100
+ window.removeEventListener("visibilitychange", onVisibilityChange);
101
+ stopAnalyticsProcessor();
102
+ };
103
+ // start the flusing process ///
104
+ startProcessing();
64
105
  return {
65
106
  track,
107
+ cleanup,
66
108
  };
67
109
  };
110
+ function stopAnalyticsProcessor() {
111
+ analyticsSharedState.isProcessing = false;
112
+ }
68
113
  async function startAnalyticsProcessor(handleTrack, options) {
114
+ if (analyticsSharedState.isProcessing) {
115
+ // only one instance of the analytics processor can be running at a time //
116
+ return;
117
+ }
118
+ analyticsSharedState.isProcessing = true;
69
119
  const { throttleTime = 1000, batchSize = 30 } = options !== null && options !== void 0 ? options : {};
70
- while (true) {
71
- await new Promise((resolve) => setTimeout(resolve, throttleTime));
120
+ while (analyticsSharedState.isProcessing &&
121
+ analyticsSharedState.requestsQueue.length > 0) {
72
122
  const requests = analyticsSharedState.requestsQueue.splice(0, batchSize);
73
123
  if (requests.length > 0) {
74
124
  try {
@@ -79,12 +129,14 @@ async function startAnalyticsProcessor(handleTrack, options) {
79
129
  console.error("Error processing analytics request:", error);
80
130
  }
81
131
  }
132
+ await new Promise((resolve) => setTimeout(resolve, throttleTime));
82
133
  }
134
+ analyticsSharedState.isProcessing = false;
83
135
  }
84
136
  function getEventIntrinsicData() {
85
137
  return {
86
138
  timestamp: new Date().toISOString(),
87
- pageUrl: typeof window !== "undefined" ? window.location.href : null,
139
+ pageUrl: typeof window !== "undefined" ? window.location.pathname : null,
88
140
  };
89
141
  }
90
142
  function transformEventDataToApiRequestData(sessionContext) {
@@ -97,6 +149,8 @@ function transformEventDataToApiRequestData(sessionContext) {
97
149
  });
98
150
  }
99
151
  export function getAnalyticsModuleOptionsFromUrlParams() {
152
+ if (typeof window === "undefined")
153
+ return undefined;
100
154
  const urlParams = new URLSearchParams(window.location.search);
101
155
  const jsonString = urlParams.get("analytics");
102
156
  if (!jsonString)
@@ -35,3 +35,6 @@ export type AnalyticsModuleOptions = {
35
35
  throttleTime?: number;
36
36
  batchSize?: number;
37
37
  };
38
+ export type AnalyticsModule = {
39
+ track: (params: TrackEventParams) => void;
40
+ };
@@ -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.aa9ac21",
3
+ "version": "0.8.6-pr.57.bb6a61e",
4
4
  "description": "JavaScript SDK for Base44 API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,16 +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: 1 };
9
- }
10
- return windowObj.base44[name].instance;
11
- }
12
- export function getSharedInstanceRefCount(name) {
13
- var _a, _b, _c;
14
- const windowObj = typeof window !== "undefined" ? window : { base44: {} };
15
- 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;
16
- }