import { useEffect } from "react"; import { getBundleVersion } from "../../../network/NetworkUtils"; import { EVENT_PROPERTY_KEYS, REACT_NATIVE } from "../constants"; import { AnalyticsEvent } from "../interface"; import { NativeAnalyticsModule } from "../native-module/NativeModules"; import { ScreenState } from "../screen/BaseScreen"; import { BASE_URL } from "../../../network/NetworkConstant"; export const useAnalyticsEvent = () => { return { sendAsAnalyticsEvent, sendAsAppDowntimeEvent, sendAsGlobalErrorEvent, sendScreenStateAnalyticsEvents, }; }; export const sendAsAnalyticsEvent = (analyticsEvent: AnalyticsEvent) => { let propertiesWithBundleVersion: Record | undefined = analyticsEvent.properties; propertiesWithBundleVersion = { ...propertiesWithBundleVersion, [EVENT_PROPERTY_KEYS.BUNDLE_VERSION]: getBundleVersion().toString(), }; NativeAnalyticsModule.sendAsAnalyticsEvent( analyticsEvent.name, propertiesWithBundleVersion, ); }; export const sendAsAppDowntimeEvent = (event: AppDowntimeData) => { const { reason = null, screenName = null, moduleName = null, statusCode = null, networkType = null, flowName = null, methodName = null, vendorName = null, extras = null, eventName = "global_app_downtime", } = event; const updatedScreenName = moduleName + "_" + REACT_NATIVE + "_" + screenName; NativeAnalyticsModule.sendAsAppDowntimeEvent({ reason, screenName: updatedScreenName, moduleName, statusCode, networkType, flowName, methodName, vendorName, extras, eventName, }); }; export const sendAsGlobalErrorEvent = (event: GlobalErrorData) => { const { reason = null, source = null, moduleName, globalErrorType, statusCode = null, networkType = null, flowName = null, methodName = null, vendorName = null, extras = null, journeySource = null, isAppDowntimeEvent = null, errorCode = null, errorTitle = null, latency = null, isNae = null, apiUrl = null, apiMethod = null, } = event; const updatedSource = moduleName + "_" + REACT_NATIVE + "_" + source; const updatedApiUrl = BASE_URL + apiUrl; NativeAnalyticsModule.sendAsGlobalErrorEvent({ reason, source: updatedSource, moduleName, globalErrorType, statusCode, networkType, flowName, methodName, vendorName, extras, journeySource, isAppDowntimeEvent, errorCode, errorTitle, latency, isNae, apiUrl: updatedApiUrl, apiMethod, }); }; const sendScreenStateAnalyticsEvents = ( eventMapping: Partial>, screenState?: ScreenState | null, ) => { useEffect(() => { if (!screenState) return; const event = eventMapping[screenState]; if (event) { sendAsAnalyticsEvent(event); } }, [screenState, eventMapping]); };