@base44-preview/sdk 0.8.6-pr.57.5d40d85 → 0.8.6-pr.57.6db0afd

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.
@@ -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_WINDOW_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;
@@ -11,4 +14,5 @@ export declare const createAnalyticsModule: ({ axiosClient, serverUrl, appId, us
11
14
  track: (params: TrackEventParams) => void;
12
15
  cleanup: () => void;
13
16
  };
14
- export declare function getAnalyticsModuleOptionsFromUrlParams(): AnalyticsModuleOptions | undefined;
17
+ export declare function getAnalyticsModuleOptionsFromWindow(): 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_WINDOW_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
+ ...getAnalyticsModuleOptionsFromWindow(),
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
- stopAnalyticsProcessor();
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,22 +101,21 @@ export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthM
82
101
  onDocVisible();
83
102
  }
84
103
  };
85
- if (typeof window !== "undefined" && enabled) {
86
- window.addEventListener("visibilitychange", onVisibilityChange);
87
- }
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
104
  const cleanup = () => {
96
- if (typeof window === "undefined")
97
- return;
98
- window.removeEventListener("visibilitychange", onVisibilityChange);
99
105
  stopAnalyticsProcessor();
106
+ clearHeartBeatProcessor === null || clearHeartBeatProcessor === void 0 ? void 0 : clearHeartBeatProcessor();
107
+ if (typeof window !== "undefined") {
108
+ window.removeEventListener("visibilitychange", onVisibilityChange);
109
+ }
100
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") {
117
+ window.addEventListener("visibilitychange", onVisibilityChange);
118
+ }
101
119
  return {
102
120
  track,
103
121
  cleanup,
@@ -108,23 +126,33 @@ function stopAnalyticsProcessor() {
108
126
  }
109
127
  async function startAnalyticsProcessor(handleTrack, options) {
110
128
  if (analyticsSharedState.isProcessing) {
129
+ // only one instance of the analytics processor can be running at a time //
111
130
  return;
112
131
  }
113
132
  analyticsSharedState.isProcessing = true;
114
133
  const { throttleTime = 1000, batchSize = 30 } = options !== null && options !== void 0 ? options : {};
115
- while (analyticsSharedState.isProcessing) {
134
+ while (analyticsSharedState.isProcessing &&
135
+ analyticsSharedState.requestsQueue.length > 0) {
116
136
  const requests = analyticsSharedState.requestsQueue.splice(0, batchSize);
117
- if (requests.length > 0) {
118
- try {
119
- await handleTrack(requests);
120
- }
121
- catch (error) {
122
- // TODO: think about retries if needed
123
- console.error("Error processing analytics request:", error);
124
- }
125
- }
137
+ requests.length && (await handleTrack(requests));
126
138
  await new Promise((resolve) => setTimeout(resolve, throttleTime));
127
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
+ };
128
156
  }
129
157
  function getEventIntrinsicData() {
130
158
  return {
@@ -141,17 +169,45 @@ function transformEventDataToApiRequestData(sessionContext) {
141
169
  ...sessionContext,
142
170
  });
143
171
  }
144
- 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 getAnalyticsModuleOptionsFromWindow() {
145
193
  if (typeof window === "undefined")
146
194
  return undefined;
147
- const urlParams = new URLSearchParams(window.location.search);
148
- const jsonString = urlParams.get("analytics");
149
- if (!jsonString)
150
- return undefined;
195
+ return window[ANALYTICS_CONFIG_WINDOW_KEY];
196
+ }
197
+ export function getAnalyticsSessionId() {
198
+ if (typeof window === "undefined") {
199
+ return generateUuid();
200
+ }
151
201
  try {
152
- return JSON.parse(jsonString);
202
+ const sessionId = localStorage.getItem(ANALYTICS_SESSION_ID_LOCAL_STORAGE_KEY);
203
+ if (!sessionId) {
204
+ const newSessionId = generateUuid();
205
+ localStorage.setItem(ANALYTICS_SESSION_ID_LOCAL_STORAGE_KEY, newSessionId);
206
+ return newSessionId;
207
+ }
208
+ return sessionId;
153
209
  }
154
210
  catch (_a) {
155
- return undefined;
211
+ return generateUuid();
156
212
  }
157
213
  }
@@ -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.5d40d85",
3
+ "version": "0.8.6-pr.57.6db0afd",
4
4
  "description": "JavaScript SDK for Base44 API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",