Skip to content

Commit 4616827

Browse files
committed
add session_id
1 parent 906486a commit 4616827

3 files changed

Lines changed: 53 additions & 8 deletions

File tree

src/modules/analytics.ts

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ import {
1010
} from "./analytics.types";
1111
import { getSharedInstance } from "../utils/sharedInstance";
1212
import type { AuthModule } from "./auth.types";
13+
import { generateUuid } from "../utils/common";
1314

1415
export const USER_HEARTBEAT_EVENT_NAME = "__user_heartbeat_event__";
16+
export const ANALYTICS_CONFIG_LOCAL_STORAGE_KEY = "base44_analytics_config";
17+
export const ANALYTICS_SESSION_ID_LOCAL_STORAGE_KEY =
18+
"base44_analytics_session_id";
1519

1620
const defaultConfiguration: AnalyticsModuleOptions = {
1721
enabled: true,
@@ -57,8 +61,15 @@ export const createAnalyticsModule = ({
5761
userAuthModule,
5862
}: AnalyticsModuleArgs) => {
5963
// prevent overflow of events //
60-
const { enabled, maxQueueSize, throttleTime, batchSize } =
61-
analyticsSharedState.config;
64+
const { maxQueueSize, throttleTime, batchSize } = analyticsSharedState.config;
65+
66+
if (!analyticsSharedState.config?.enabled) {
67+
return {
68+
track: () => {},
69+
cleanup: () => {},
70+
};
71+
}
72+
6273
let clearHeartBeatProcessor: (() => void) | undefined = undefined;
6374
const trackBatchUrl = `${serverUrl}/api/apps/${appId}/analytics/track/batch`;
6475

@@ -91,15 +102,14 @@ export const createAnalyticsModule = ({
91102
};
92103

93104
const startProcessing = () => {
94-
if (!enabled) return;
95105
startAnalyticsProcessor(flush, {
96106
throttleTime,
97107
batchSize,
98108
});
99109
};
100110

101111
const track = (params: TrackEventParams) => {
102-
if (!enabled || analyticsSharedState.requestsQueue.length >= maxQueueSize) {
112+
if (analyticsSharedState.requestsQueue.length >= maxQueueSize) {
103113
return;
104114
}
105115
const intrinsicData = getEventIntrinsicData();
@@ -148,7 +158,7 @@ export const createAnalyticsModule = ({
148158
// start the heart beat processor //
149159
clearHeartBeatProcessor = startHeartBeatProcessor(track);
150160
// start the visibility change listener //
151-
if (typeof window !== "undefined" && enabled) {
161+
if (typeof window !== "undefined") {
152162
window.addEventListener("visibilitychange", onVisibilityChange);
153163
}
154164

@@ -224,16 +234,21 @@ function transformEventDataToApiRequestData(sessionContext: SessionContext) {
224234
}
225235

226236
let sessionContextPromise: Promise<SessionContext> | null = null;
227-
async function getSessionContext(userAuthModule: AuthModule) {
237+
async function getSessionContext(
238+
userAuthModule: AuthModule
239+
): Promise<SessionContext> {
228240
if (!analyticsSharedState.sessionContext) {
229241
if (!sessionContextPromise) {
242+
const sessionId = getAnalyticsSessionId();
230243
sessionContextPromise = userAuthModule
231244
.me()
232245
.then((user) => ({
233246
user_id: user.id,
247+
session_id: sessionId,
234248
}))
235249
.catch(() => ({
236-
user_id: "unknown: error getting session context",
250+
user_id: null,
251+
session_id: sessionId,
237252
}));
238253
}
239254
analyticsSharedState.sessionContext = await sessionContextPromise;
@@ -246,10 +261,32 @@ export function getAnalyticsModuleOptionsFromLocalStorage():
246261
| undefined {
247262
if (typeof window === "undefined") return undefined;
248263
try {
249-
const jsonString = localStorage.getItem("base44_analytics_config");
264+
const jsonString = localStorage.getItem(ANALYTICS_CONFIG_LOCAL_STORAGE_KEY);
250265
if (!jsonString) return undefined;
251266
return JSON.parse(jsonString);
252267
} catch {
253268
return undefined;
254269
}
255270
}
271+
272+
export function getAnalyticsSessionId(): string {
273+
if (typeof window === "undefined") {
274+
return generateUuid();
275+
}
276+
try {
277+
const sessionId = localStorage.getItem(
278+
ANALYTICS_SESSION_ID_LOCAL_STORAGE_KEY
279+
);
280+
if (!sessionId) {
281+
const newSessionId = generateUuid();
282+
localStorage.setItem(
283+
ANALYTICS_SESSION_ID_LOCAL_STORAGE_KEY,
284+
newSessionId
285+
);
286+
return newSessionId;
287+
}
288+
return sessionId;
289+
} catch {
290+
return generateUuid();
291+
}
292+
}

src/modules/analytics.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type TrackEventData = {
1919

2020
export type SessionContext = {
2121
user_id?: string | null;
22+
session_id?: string | null;
2223
};
2324

2425
export type AnalyticsApiRequestData = {

src/utils/common.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
export const isNode = typeof window === "undefined";
22
export const isInIFrame = !isNode && window.self !== window.top;
3+
4+
export const generateUuid = () => {
5+
return (
6+
Math.random().toString(36).substring(2, 15) +
7+
Math.random().toString(36).substring(2, 15)
8+
);
9+
};

0 commit comments

Comments
 (0)