@applicaster/zapp-react-native-utils
Version:
Applicaster Zapp React Native utilities package
117 lines (93 loc) • 3.1 kB
text/typescript
import * as R from "ramda";
import { log_error, log_debug } from "./logger";
const validate = (screenProperties: AnalyticsScreenProperties) => {
if (!screenProperties) {
return;
}
const {
screen_name,
screen_entry_title,
screen_entry_id,
screen_layout_id,
screen_type,
home_screen,
} = screenProperties;
const validateStringKey = (value): Boolean => R.is(String, value);
const validateBoolKey = (value): Boolean => R.is(Boolean, value);
const sendOptionalKeyNotExist = (key: string) => {
log_debug(`CurrentScreenProperities: optional key: ${key} not exist`);
};
const throwErrorKeyNotExist = (key: string) => {
log_error(`CurrentScreenProperities: expected key: ${key} not exist`);
};
if (!validateStringKey(screen_name)) {
throwErrorKeyNotExist("screen_name");
}
if (!validateStringKey(screen_entry_title)) {
sendOptionalKeyNotExist("screen_entry_title");
}
if (!validateStringKey(screen_entry_id)) {
sendOptionalKeyNotExist("screen_entry_id");
}
if (!validateStringKey(screen_layout_id)) {
throwErrorKeyNotExist("screen_layout_id");
}
if (!validateStringKey(screen_type)) {
throwErrorKeyNotExist("screen_type");
}
if (!validateBoolKey(home_screen)) {
throwErrorKeyNotExist("home_screen");
}
};
const mappedScreenPropertiesScreenHook = (
screenData
): AnalyticsScreenProperties => {
const hookPlugin = screenData?.hookPlugin;
const payload: any = screenData?.payload;
return {
screen_name: hookPlugin?.name,
screen_entry_title: payload?.title,
screen_entry_id: payload?.id,
screen_layout_id: hookPlugin?.id,
screen_type: hookPlugin?.type as string,
home_screen: hookPlugin?.home,
};
};
const mappedScreenPropertiesScreen = (
screenData
): AnalyticsScreenProperties => {
return {
screen_name: screenData?.targetScreen?.name,
screen_entry_title: screenData?.title,
screen_entry_id: R.isNil(screenData?.type) ? null : screenData?.id,
screen_layout_id: screenData?.targetScreen?.id,
screen_type: screenData?.targetScreen?.type,
home_screen: screenData?.targetScreen?.home,
};
};
const mappedScreenProperties = (screenData): AnalyticsScreenProperties =>
screenData?.hookPlugin
? mappedScreenPropertiesScreenHook(screenData)
: mappedScreenPropertiesScreen(screenData);
const mapToScreenPropertiesAndValidate = (
screenData
): AnalyticsScreenProperties | null => {
if (R.isNil(screenData)) {
log_error(
"mapToScreenPropertiesAndValidate set: screenData not exist, failed to prepare analytics screen data"
);
return null;
}
const screenProperties = mappedScreenProperties(screenData);
validate(screenProperties);
log_debug(
`mapToScreenPropertiesAndValidate: screen properties was succesfully saved screen: ${screenProperties.screen_name}`,
screenProperties as any
);
return screenProperties;
};
export const getAnalyticsScreenPropertiesFromScreenData = (
screenData
): AnalyticsScreenProperties | null => {
return mapToScreenPropertiesAndValidate(screenData);
};