@@ -10,8 +10,12 @@ import {
1010} from "./analytics.types" ;
1111import { getSharedInstance } from "../utils/sharedInstance" ;
1212import type { AuthModule } from "./auth.types" ;
13+ import { generateUuid } from "../utils/common" ;
1314
1415export 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
1620const 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
226236let 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+ }
0 commit comments