@base44/sdk 0.8.7 → 0.8.9

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.
@@ -2,7 +2,7 @@ import { AxiosInstance } from "axios";
2
2
  import { TrackEventParams, AnalyticsModuleOptions } from "./analytics.types";
3
3
  import type { AuthModule } from "./auth.types";
4
4
  export declare const USER_HEARTBEAT_EVENT_NAME = "__user_heartbeat_event__";
5
- export declare const ANALYTICS_CONFIG_WINDOW_KEY = "base44_analytics_config";
5
+ export declare const ANALYTICS_CONFIG_ENABLE_URL_PARAM_KEY = "analytics-enable";
6
6
  export declare const ANALYTICS_SESSION_ID_LOCAL_STORAGE_KEY = "base44_analytics_session_id";
7
7
  export interface AnalyticsModuleArgs {
8
8
  axiosClient: AxiosInstance;
@@ -14,5 +14,5 @@ export declare const createAnalyticsModule: ({ axiosClient, serverUrl, appId, us
14
14
  track: (params: TrackEventParams) => void;
15
15
  cleanup: () => void;
16
16
  };
17
- export declare function getAnalyticsModuleOptionsFromWindow(): AnalyticsModuleOptions | undefined;
17
+ export declare function getAnalyticsConfigFromUrlParams(): AnalyticsModuleOptions | undefined;
18
18
  export declare function getAnalyticsSessionId(): string;
@@ -1,10 +1,11 @@
1
1
  import { getSharedInstance } from "../utils/sharedInstance";
2
2
  import { generateUuid } from "../utils/common";
3
3
  export const USER_HEARTBEAT_EVENT_NAME = "__user_heartbeat_event__";
4
- export const ANALYTICS_CONFIG_WINDOW_KEY = "base44_analytics_config";
4
+ export const ANALYTICS_CONFIG_ENABLE_URL_PARAM_KEY = "analytics-enable";
5
5
  export const ANALYTICS_SESSION_ID_LOCAL_STORAGE_KEY = "base44_analytics_session_id";
6
6
  const defaultConfiguration = {
7
- enabled: true,
7
+ // default to disabled //
8
+ enabled: false,
8
9
  maxQueueSize: 1000,
9
10
  throttleTime: 1000,
10
11
  batchSize: 30,
@@ -22,7 +23,7 @@ const analyticsSharedState = getSharedInstance(ANALYTICS_SHARED_STATE_NAME, () =
22
23
  sessionContext: null,
23
24
  config: {
24
25
  ...defaultConfiguration,
25
- ...getAnalyticsModuleOptionsFromWindow(),
26
+ ...getAnalyticsConfigFromUrlParams(),
26
27
  },
27
28
  }));
28
29
  export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthModule, }) => {
@@ -44,20 +45,27 @@ export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthM
44
45
  data: { events },
45
46
  });
46
47
  };
48
+ // currently disabled, until fully tested //
49
+ const beaconRequest = async (events) => {
50
+ const beaconPayload = JSON.stringify({ events });
51
+ try {
52
+ const blob = new Blob([beaconPayload], { type: "application/json" });
53
+ return (typeof navigator === "undefined" ||
54
+ beaconPayload.length > 60000 ||
55
+ !navigator.sendBeacon(trackBatchUrl, blob));
56
+ }
57
+ catch (_a) {
58
+ return false;
59
+ }
60
+ };
47
61
  const flush = async (eventsData) => {
48
62
  const sessionContext_ = await getSessionContext(userAuthModule);
49
63
  const events = eventsData.map(transformEventDataToApiRequestData(sessionContext_));
50
- const beaconPayload = JSON.stringify({ events });
51
64
  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
- }
65
+ return batchRequestFallback(events);
58
66
  }
59
67
  catch (_a) {
60
- // TODO: think about retries if needed
68
+ // do nothing
61
69
  }
62
70
  };
63
71
  const startProcessing = () => {
@@ -189,10 +197,22 @@ async function getSessionContext(userAuthModule) {
189
197
  }
190
198
  return analyticsSharedState.sessionContext;
191
199
  }
192
- export function getAnalyticsModuleOptionsFromWindow() {
200
+ export function getAnalyticsConfigFromUrlParams() {
193
201
  if (typeof window === "undefined")
194
202
  return undefined;
195
- return window[ANALYTICS_CONFIG_WINDOW_KEY];
203
+ const urlParams = new URLSearchParams(window.location.search);
204
+ const analyticsEnable = urlParams.get(ANALYTICS_CONFIG_ENABLE_URL_PARAM_KEY);
205
+ // if the url param is not set, return undefined //
206
+ if (analyticsEnable == null || !analyticsEnable.length)
207
+ return undefined;
208
+ // remove the url param from the url //
209
+ const newUrlParams = new URLSearchParams(window.location.search);
210
+ newUrlParams.delete(ANALYTICS_CONFIG_ENABLE_URL_PARAM_KEY);
211
+ const newUrl = window.location.pathname +
212
+ (newUrlParams.toString() ? "?" + newUrlParams.toString() : "");
213
+ window.history.replaceState({}, "", newUrl);
214
+ // return the config object //
215
+ return { enabled: analyticsEnable === "true" };
196
216
  }
197
217
  export function getAnalyticsSessionId() {
198
218
  if (typeof window === "undefined") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44/sdk",
3
- "version": "0.8.7",
3
+ "version": "0.8.9",
4
4
  "description": "JavaScript SDK for Base44 API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",