Skip to content

Commit 1faced4

Browse files
committed
cleanup and tests
1 parent a51c69a commit 1faced4

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

src/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ export function createClient(config: CreateClientConfig): Base44Client {
159159
userAuthModule,
160160
}),
161161
cleanup: () => {
162+
userModules.analytics.cleanup();
162163
if (socket) {
163164
socket.disconnect();
164165
}

src/client.types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { ConnectorsModule } from "./modules/connectors.types.js";
66
import type { FunctionsModule } from "./modules/functions.types.js";
77
import type { AgentsModule } from "./modules/agents.types.js";
88
import type { AppLogsModule } from "./modules/app-logs.types.js";
9+
import type { AnalyticsModule } from "./modules/analytics.types.js";
910

1011
/**
1112
* Options for creating a Base44 client.
@@ -85,6 +86,8 @@ export interface Base44Client {
8586
agents: AgentsModule;
8687
/** {@link AppLogsModule | App logs module} for tracking app usage. */
8788
appLogs: AppLogsModule;
89+
/** {@link AnalyticsModule | Analytics module} for tracking app usage. */
90+
analytics: AnalyticsModule;
8891
/** Cleanup function to disconnect WebSocket connections. Call when you're done with the client. */
8992
cleanup: () => void;
9093

src/modules/analytics.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export const createAnalyticsModule = ({
105105
};
106106

107107
const onDocHidden = () => {
108-
analyticsSharedState.isProcessing = false;
108+
stopAnalyticsProcessor();
109109
// flush entire queue on visibility change and hope for the best //
110110
const eventsData = analyticsSharedState.requestsQueue.splice(0);
111111
flush(eventsData);
@@ -139,11 +139,22 @@ export const createAnalyticsModule = ({
139139
});
140140
}
141141

142+
const cleanup = () => {
143+
if (typeof window === "undefined") return;
144+
window.removeEventListener("visibilitychange", onVisibilityChange);
145+
stopAnalyticsProcessor();
146+
};
147+
142148
return {
143149
track,
150+
cleanup,
144151
};
145152
};
146153

154+
function stopAnalyticsProcessor() {
155+
analyticsSharedState.isProcessing = false;
156+
}
157+
147158
async function startAnalyticsProcessor(
148159
handleTrack: (eventsData: TrackEventData[]) => Promise<void>,
149160
options?: {

tests/unit/analytics.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { describe, test, expect, beforeEach, afterEach, vi } from "vitest";
2+
import {
3+
AnalyticsModuleOptions,
4+
createClient,
5+
SessionContext,
6+
TrackEventData,
7+
} from "../../src/index.ts";
8+
import { getSharedInstance } from "../../src/utils/sharedInstance.ts";
9+
10+
describe("Analytics Module", () => {
11+
let base44: ReturnType<typeof createClient>;
12+
let sharedState: null | {
13+
requestsQueue: TrackEventData[];
14+
isProcessing: boolean;
15+
sessionContext: SessionContext;
16+
config: AnalyticsModuleOptions;
17+
};
18+
const appId = "test-app-id";
19+
const serverUrl = "https://api.base44.com";
20+
21+
beforeEach(() => {
22+
base44 = createClient({
23+
serverUrl,
24+
appId,
25+
});
26+
sharedState = getSharedInstance("analytics", () => ({
27+
requestsQueue: [],
28+
isProcessing: false,
29+
sessionContext: {
30+
user_id: "test-user-id",
31+
},
32+
config: {
33+
enabled: true,
34+
maxQueueSize: 1000,
35+
throttleTime: 1000,
36+
batchSize: 30,
37+
},
38+
}));
39+
});
40+
41+
afterEach(() => {
42+
base44.cleanup();
43+
sharedState = null;
44+
});
45+
46+
test("should create analytics module with shared state", () => {
47+
expect(base44.analytics).toBeDefined();
48+
expect(sharedState).toBeDefined();
49+
expect(sharedState?.requestsQueue).toBeDefined();
50+
expect(sharedState?.isProcessing).toBe(true);
51+
});
52+
53+
test("should track an event", () => {
54+
vi.spyOn(base44.analytics, "track").mockImplementation(() => {
55+
console.log("track called");
56+
});
57+
58+
base44.analytics.track({ eventName: "test-event" });
59+
expect(base44.analytics.track).toHaveBeenCalledWith({
60+
eventName: "test-event",
61+
});
62+
});
63+
});

tests/unit/client.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe('Client Creation', () => {
1212
expect(client.entities).toBeDefined();
1313
expect(client.integrations).toBeDefined();
1414
expect(client.auth).toBeDefined();
15+
expect(client.analytics).toBeDefined();
1516

1617
const config = client.getConfig();
1718
expect(config.appId).toBe('test-app-id');

0 commit comments

Comments
 (0)