@causalfoundry/react-native-cf-loyalty
Version:
causalfoundry.ai SDK for tracking logs for Loyalty content block.
185 lines (170 loc) • 8.79 kB
text/typescript
import {
NativeModules,
Platform
} from 'react-native';
import {
MilestoneProperties,
LevelProperties,
PromoProperties, SurveyProperties, SurveyEventProperties, RewardEventProperties, RewardProperties
} from "./typings";
const { CfLogLoyalty } = NativeModules;
/**
* logLevelEvent is to log the update of user level. It required the user level before update and
* user level after the update. You need to pass module id as well if the level update event is
* triggered because of the e-learning content block, any achievement in the e-learning platform.
*
* @param previous_level is required to set the previous score/level number for the user.
* @param new_level is required to set the new score/level number for the user. This
* can be from any source, e-learning or e-commerce, or even social.
* @param module_id is for specific use-case when the update of level is from e-learning.
* In such case module id is required, otherwise, you can pass null for
* this as well.
* @param meta is to send any other data you want to send with ingest, can also be null.
* @param updateImmediately is default set to true, you can use that to log events when the app
* goes in the background or closed.
*/
const logLevelEvent = (properties: LevelProperties) => {
console.log(properties)
if(Platform.OS === 'android'){
CfLogLoyalty.logLevelEvent(properties.previous_level, properties.new_level, properties.module_id, null, false)
}else if(Platform.OS === 'ios'){
CfLogLoyalty.logLevelEvent(properties.previous_level.toString(), properties.new_level.toString(), properties.module_id, false)
}
}
/**
* logMilestoneEvent is to log actions regarding milestones which can be when the user achieved
* a milestone.
*
* @param id is required for logging the milestone user achieved. The is should
* be in a string format and must be per the catalog provided.
* @param action is required to set the Action type for the Milestone event. SDK
* provides enum classes to support available log types. SDK provides 2
* approaches to log this event, one being
* enum type and the other is string type.
* @param meta is to send any other data you want to send with ingest, can be null.
* @param updateImmediately is default set to true, you can use that to log events when the app
* goes in the background or closed.
*/
const logMilestoneEvent = (properties: MilestoneProperties) => {
console.log(properties)
if(Platform.OS === 'android'){
CfLogLoyalty.logMilestoneEvent(properties.id, properties.action, null, false)
}else if(Platform.OS === 'ios'){
CfLogLoyalty.logMilestoneEvent(properties.id, properties.action, false)
}
}
/**
* logPromoEvent is to log the events associated of the promo lists and promo items and when they are clicked on.
*
* @param promoId is required to set the Id of the promo
* @param action is required to set the action for the promo
* @param title is required to set the title of the promo (if any)
* @param type is required to set the type of the promo
* @param promoItemsList to add the whole list to the log at once, the format should be
* Array<PromoItemDetail> to log the event successfully.
* @param meta is to send any other data you want to send with the ingest, can be null.
* @param updateImmediately is default set to true, you can use that to log events when the app
* goes in the background or closed.
*/
const logPromoEvent = (properties: PromoProperties) => {
console.log(properties)
if(Platform.OS === 'android'){
CfLogLoyalty.logPromoEvent(properties.id, properties.action, properties.title, properties.type, JSON.stringify(properties.items), null, false)
}
else if(Platform.OS === 'ios'){
CfLogLoyalty.logPromoEvent(properties.id, properties.action, properties.title, properties.type, JSON.stringify(properties.items), false)
}
}
/**
* logSurveyEvent is to log the user viewing, attempting the survey.
*
* @param action is required to set the Action type for the Survey Action.
* @param surveyObject is for the providing item info details about survey. The object
* should be based on the SurveyObject or a string that can be
* converted to the object with proper param names.
* @param responseList is for the providing responses item details about survey. The
* object should be based on the SurveyResponseItem List or a
* string list that can be converted to the object with proper param names.
* @param meta is to send any other data you want to send with the ingest, can be null.
* @param updateImmediately is default set to true, you can use that to log events when the app
* goes in the background or closed.
*/
const logSurveyEvent = (properties: SurveyEventProperties) => {
console.log(properties)
if(Platform.OS === 'android'){
CfLogLoyalty.logSurveyEvent(properties.action,
JSON.stringify(properties.survey),
JSON.stringify(properties.response), null, false)
}
else if(Platform.OS === 'ios'){
CfLogLoyalty.logSurveyEvent(properties.action,
JSON.stringify(properties.survey),
JSON.stringify(properties.response), false)
}
}
/**
* logRewardEvent is to log the reward related events for user, which includes viewing,
* redeeming, adding.
*
* @param rewardId is for the providing Id for the reward event. Can be userId if the
* reward is not redeemed individually.
* @param action is required to set the Action type for the Reward Action.
* @param acc_points is for the providing achieved points in case of add reward event.
* @param total_points logs the total points achieved so far by the user.
* @param redeemObject is for the providing details about reward redeeming. The object
* should be based on the RedeemObject or a string that can be
* converted to the object with proper param names.
* @param meta is to send any other data you want to send with ingest, can be null.
* @param updateImmediately is default set to true, you can use that to log events when the app
* goes in the background or closed.
*/
const logRewardEvent = (properties: RewardEventProperties) => {
console.log(properties)
if(Platform.OS === 'android'){
CfLogLoyalty.logRewardEvent(
properties.id, properties.action, properties.acc_points, properties.total_points,
JSON.stringify(properties.redeem), null, false)
}
else if(Platform.OS === 'ios'){
CfLogLoyalty.logRewardEvent(
properties.id, properties.action, properties.total_points ?? 0, properties.acc_points ?? 0,
JSON.stringify(properties.redeem), false)
}
}
/**
* Survey catalog values
*
* @param surveyId Survey id in the system
* @param surveyCatalogModel Props for the survey item
*/
const updateSurveyCatalog = (surveyId: string, surveyCatalogModel: SurveyProperties) => {
console.log(surveyCatalogModel)
if (Platform.OS === 'android') {
CfLogLoyalty.updateSurveyCatalog(surveyId, JSON.stringify(surveyCatalogModel))
}else if (Platform.OS === 'ios') {
CfLogLoyalty.updateSurveyCatalog(surveyId, JSON.stringify(surveyCatalogModel))
}
}
/**
* Reward catalog values
*
* @param rewardId Reward id in the system
* @param rewardCatalogModel Props for the reward item
*/
const updateRewardCatalog = (rewardId: string, rewardCatalogModel: RewardProperties) => {
console.log(rewardCatalogModel)
if (Platform.OS === 'android') {
CfLogLoyalty.updateRewardCatalog(rewardId, JSON.stringify(rewardCatalogModel))
}else if (Platform.OS === 'ios') {
CfLogLoyalty.updateRewardCatalog(rewardId, JSON.stringify(rewardCatalogModel))
}
}
export default {
logLevelEvent,
logMilestoneEvent,
logPromoEvent,
logSurveyEvent,
logRewardEvent,
updateSurveyCatalog,
updateRewardCatalog
}