@base44-preview/sdk 0.8.6-pr.57.0298afb → 0.8.6-pr.57.5d40d85

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,4 +1,10 @@
1
- import { getSharedInstance, } 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
+ };
2
8
  ///////////////////////////////////////////////
3
9
  //// shared queue for analytics events ////
4
10
  ///////////////////////////////////////////////
@@ -7,21 +13,24 @@ const ANALYTICS_SHARED_STATE_NAME = "analytics";
7
13
  const analyticsSharedState = getSharedInstance(ANALYTICS_SHARED_STATE_NAME, () => ({
8
14
  requestsQueue: [],
9
15
  isProcessing: false,
16
+ sessionContext: null,
17
+ config: {
18
+ ...defaultConfiguration,
19
+ ...getAnalyticsModuleOptionsFromUrlParams(),
20
+ },
10
21
  }));
11
22
  export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthModule, }) => {
12
- var _a;
13
23
  // prevent overflow of events //
14
- const { enabled = true, maxQueueSize = 1000, throttleTime = 1000, batchSize = 30, } = (_a = getAnalyticsModuleOptionsFromUrlParams()) !== null && _a !== void 0 ? _a : {};
24
+ const { enabled, maxQueueSize, throttleTime, batchSize } = analyticsSharedState.config;
15
25
  const trackBatchUrl = `${serverUrl}/api/apps/${appId}/analytics/track/batch`;
16
- let sessionContext = null;
17
26
  const getSessionContext = async () => {
18
- if (sessionContext)
19
- return sessionContext;
20
- const user = await userAuthModule.me();
21
- sessionContext = {
22
- user_id: user.id,
23
- };
24
- return sessionContext;
27
+ if (!analyticsSharedState.sessionContext) {
28
+ const user = await userAuthModule.me();
29
+ analyticsSharedState.sessionContext = {
30
+ user_id: user.id,
31
+ };
32
+ }
33
+ return analyticsSharedState.sessionContext;
25
34
  };
26
35
  const track = (params) => {
27
36
  if (!enabled || analyticsSharedState.requestsQueue.length >= maxQueueSize) {
@@ -41,7 +50,7 @@ export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthM
41
50
  });
42
51
  };
43
52
  const flush = async (eventsData) => {
44
- const sessionContext_ = sessionContext !== null && sessionContext !== void 0 ? sessionContext : (await getSessionContext());
53
+ const sessionContext_ = await getSessionContext();
45
54
  const events = eventsData.map(transformEventDataToApiRequestData(sessionContext_));
46
55
  const beaconPayload = JSON.stringify({ events });
47
56
  if (typeof navigator === "undefined" ||
@@ -52,7 +61,7 @@ export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthM
52
61
  }
53
62
  };
54
63
  const onDocHidden = () => {
55
- analyticsSharedState.isProcessing = false;
64
+ stopAnalyticsProcessor();
56
65
  // flush entire queue on visibility change and hope for the best //
57
66
  const eventsData = analyticsSharedState.requestsQueue.splice(0);
58
67
  flush(eventsData);
@@ -83,10 +92,20 @@ export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthM
83
92
  batchSize,
84
93
  });
85
94
  }
95
+ const cleanup = () => {
96
+ if (typeof window === "undefined")
97
+ return;
98
+ window.removeEventListener("visibilitychange", onVisibilityChange);
99
+ stopAnalyticsProcessor();
100
+ };
86
101
  return {
87
102
  track,
103
+ cleanup,
88
104
  };
89
105
  };
106
+ function stopAnalyticsProcessor() {
107
+ analyticsSharedState.isProcessing = false;
108
+ }
90
109
  async function startAnalyticsProcessor(handleTrack, options) {
91
110
  if (analyticsSharedState.isProcessing) {
92
111
  return;
@@ -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;
@@ -9,13 +9,7 @@ export function getSharedInstance(name, factory) {
9
9
  if (!windowObj.base44SharedInstances[name]) {
10
10
  windowObj.base44SharedInstances[name] = {
11
11
  instance: factory(),
12
- _refCount: 0,
13
12
  };
14
13
  }
15
- windowObj.base44SharedInstances[name]._refCount++;
16
14
  return windowObj.base44SharedInstances[name].instance;
17
15
  }
18
- export function getSharedInstanceRefCount(name) {
19
- var _a, _b, _c;
20
- return (_c = (_b = (_a = windowObj.base44SharedInstances) === null || _a === void 0 ? void 0 : _a[name]) === null || _b === void 0 ? void 0 : _b._refCount) !== null && _c !== void 0 ? _c : 0;
21
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44-preview/sdk",
3
- "version": "0.8.6-pr.57.0298afb",
3
+ "version": "0.8.6-pr.57.5d40d85",
4
4
  "description": "JavaScript SDK for Base44 API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",