TP-24988 | added clickstream api events (#227)

* TP-24988 | added clickstream api events

* TP-24988 | fix
This commit is contained in:
Aman Chaturvedi
2023-04-12 19:14:48 +05:30
committed by GitHub Enterprise
parent 4dbd843f57
commit 3d2f21e75d
2 changed files with 62 additions and 41 deletions

View File

@@ -7,31 +7,31 @@ import { GLOBAL } from '../../constants/Global';
import { _map } from '../../../RN-UI-LIB/src/utlis/common';
import { BASE_AV_APP_URL } from '../../constants/config';
import { logError } from './errorUtils';
import { ClickstreamAPIToMonitor, CLICKSTREAM_EVENT_NAMES } from '../../common/Constants';
import { addClickstreamEvent } from '../../services/clickstreamEventService';
import { resetCasesData } from '../../reducer/allCasesSlice';
import { getEventNameFromAPIKey, getKeyByValue } from './commonFunctions';
export enum ApiKeys {
GENERATE_OTP,
VERIFY_OTP,
ALL_CASES,
CASE_DETAIL,
PINNED_CASES,
LOGOUT,
FEEDBACK,
FILTERS,
JANUS,
GENERATE_PAYMENT_LINK,
ADDRESSES_GEOLOCATION,
NEW_ADDRESS,
GET_SIGNED_URL,
CASE_UNIFIED_DETAILS,
EMI_SCHEDULES,
PAST_FEEDBACK,
NOTIFICATIONS,
NOTIFICATION_ACTION,
NOTIFICATION_DELIVERED,
SEND_LOCATION,
GENERATE_OTP = 'GENERATE_OTP',
VERIFY_OTP = 'VERIFY_OTP',
ALL_CASES = 'ALL_CASES',
CASE_DETAIL = 'CASE_DETAIL',
PINNED_CASES = 'PINNED_CASES',
LOGOUT = 'LOGOUT',
FEEDBACK = 'FEEDBACK',
FILTERS = 'FILTERS',
JANUS = 'JANUS',
GENERATE_PAYMENT_LINK = 'GENERATE_PAYMENT_LINK',
ADDRESSES_GEOLOCATION = 'ADDRESSES_GEOLOCATION',
NEW_ADDRESS = 'NEW_ADDRESS',
GET_SIGNED_URL = 'GET_SIGNED_URL',
CASE_UNIFIED_DETAILS = 'CASE_UNIFIED_DETAILS',
EMI_SCHEDULES = 'EMI_SCHEDULES',
PAST_FEEDBACK = 'PAST_FEEDBACK',
NOTIFICATIONS = 'NOTIFICATIONS',
NOTIFICATION_ACTION = 'NOTIFICATION_ACTION',
NOTIFICATION_DELIVERED = 'NOTIFICATION_DELIVERED',
SEND_LOCATION = 'SEND_LOCATION',
}
export const API_URLS: Record<ApiKeys, string> = {} as Record<ApiKeys, string>;
@@ -139,12 +139,11 @@ axiosInstance.interceptors.response.use(
const milliseconds = end - Number(start);
response.headers['request-duration'] = String(milliseconds);
if(response?.config?.url) {
const url = response?.config?.url;
if(ClickstreamAPIToMonitor[url]) {
const eventName = ClickstreamAPIToMonitor[url] + '_SUCCESS'
if(CLICKSTREAM_EVENT_NAMES[eventName]) {
addClickstreamEvent(CLICKSTREAM_EVENT_NAMES[eventName], {timeTaken: milliseconds})
}
const url = response.config.url;
const apiKey = getKeyByValue(API_URLS, url);
if(apiKey) {
const eventName = getEventNameFromAPIKey(apiKey, true);
addClickstreamEvent({name: eventName, description: eventName}, {timeTaken: milliseconds})
}
}
}
@@ -153,23 +152,20 @@ axiosInstance.interceptors.response.use(
error => {
const {config, response} = error;
logError(error as Error, config?.baseURL+config?.url);
if(config.headers.donotHandleError) {
return;
}
if (config?.headers) {
if(response?.config?.url && response.status >= API_STATUS_CODE.INTERNAL_SERVER_ERROR) {
const start = response.config.headers['request-start-time'];
const end = Date.now();
const milliseconds = end - Number(start);
if(config?.url) {
const url = response?.config?.url;
if(ClickstreamAPIToMonitor[url]) {
const eventName = ClickstreamAPIToMonitor[url] + '_FAILED'
if(CLICKSTREAM_EVENT_NAMES[eventName]) {
addClickstreamEvent(CLICKSTREAM_EVENT_NAMES[eventName], {timeTaken: milliseconds, response})
}
}
const url = response.config.url;
const apiKey = getKeyByValue(API_URLS, url);
if(apiKey) {
const eventName = getEventNameFromAPIKey(apiKey);
addClickstreamEvent({name: eventName, description: eventName}, {timeTaken: milliseconds});
}
}
if(config.headers.donotHandleError) {
return;
}
if (
!config ||
config.retry <= 1 ||
@@ -189,8 +185,6 @@ axiosInstance.interceptors.response.use(
if ([API_STATUS_CODE.UNAUTHORIZED, API_STATUS_CODE.FORBIDDEN].includes(response.status)) {
// Reset user info
// const apiURL = `${config?.baseURL}${config?.url}`;
// toast({type:'error', text1: `API failed: 401 or 403: ${apiURL}`, visibilityTime: 9999999});
dispatch &&
dispatch(
setAuthData({

View File

@@ -206,4 +206,31 @@ export function debounce(func : any, timeout = 1000){
clearTimeout(timer);
timer = setTimeout(() => { func.apply(this, args); }, timeout);
};
}
function memoizeValue<T extends (...args: any[]) => any>(func: T): T {
const cache: Record<string, any> = {};
return function(...args: Parameters<T>): ReturnType<T> {
const key = JSON.stringify(args);
if (cache[key]) {
return cache[key];
}
const result = func.apply(this, args);
cache[key] = result;
return result;
} as T;
}
export const getKeyByValue = memoizeValue(function(obj: Record<string, string>, value: string) {
for (let key in obj) {
const regex = /\{.*?\}/g;
const str = obj[key].replace(regex, "");
if (value.includes(str)) {
return key;
}
}
});
export const getEventNameFromAPIKey = (apiKey: string, isSuccess?: boolean) => {
return `FA_${apiKey}_${isSuccess ? 'SUCCESS' : 'FAILED'}`;
}