Files
super-app/App/common/hooks/useAnalyticsEvent.ts
Prajjaval Verma 0cc95f8027 NTP-8444 | NAP Phase 2 | OE for GI (#13693)
Co-authored-by: shrihari-raju_navi <shrihari.raju@navi.com>
Co-authored-by: Kshitij Pramod Ghongadi <kshitij.pramod@navi.com>
2024-11-20 16:37:33 +00:00

113 lines
2.7 KiB
TypeScript

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";
export const useAnalyticsEvent = () => {
return {
sendAsAnalyticsEvent,
sendAsAppDowntimeEvent,
sendAsGlobalErrorEvent,
sendScreenStateAnalyticsEvents,
};
};
export const sendAsAnalyticsEvent = (analyticsEvent: AnalyticsEvent) => {
let propertiesWithBundleVersion: Record<string, string> | 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,
} = event;
const updatedSource = moduleName + "_" + REACT_NATIVE + "_" + source;
NativeAnalyticsModule.sendAsGlobalErrorEvent({
reason,
source: updatedSource,
moduleName,
globalErrorType,
statusCode,
networkType,
flowName,
methodName,
vendorName,
extras,
journeySource,
isAppDowntimeEvent,
errorCode,
errorTitle,
latency,
});
};
const sendScreenStateAnalyticsEvents = (
eventMapping: Partial<Record<ScreenState, AnalyticsEvent>>,
screenState?: ScreenState | null,
) => {
useEffect(() => {
if (!screenState) return;
const event = eventMapping[screenState];
if (event) {
sendAsAnalyticsEvent(event);
}
}, [screenState, eventMapping]);
};