@benshi.ai/react-native-bsh-e-com
Version:
benshi.ai SDK for tracking logs for E-Commerce content block
212 lines (200 loc) • 12.6 kB
text/typescript
import {
NativeModules,
Platform
} from 'react-native';
import {
CartProperties,
CheckoutProperties,
DeliveryProperties,
DrugProperties,
ItemProperties,
ScheduledDeliveryProperties ,
CancelCheckoutProperties
} from "./typings";
const { BsLogECom } = NativeModules;
/**
* logItemEvent refers to an item in E-commerce. It can be called when a user taps on an item to
* view it in full screen and also when a user taps on the description of the item to view more
* details. This event refers to the item view log that reflects the user has seen specific
* item or view its details.
*
* @param action is required to set Type for the type of log in this case, if a
* user is viewing an item or its details.
* @param item required to pass the whole item as a JSON String object as well.
* You can use the POJO ItemModel to parse the data in the required
* format and pass that to this function as a string to log the event.
* @param search_id is used to associate the search id with the item being viewed by
* the user. It is required to track if the item is a result of some
* search performed by the user in the app.
* @param catalogObject is used to set the drug catalog for the item being viewed.
* @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 logItemEvent = (properties: ItemProperties, catalogObject : any) => {
if (Platform.OS === 'android') {
console.log(properties)
var catalogProperties = null
if(catalogObject != null){
catalogProperties = JSON.stringify(catalogObject)
}
BsLogECom.logItemEvent(properties.action, JSON.stringify(properties.item), properties.search_id, catalogProperties, null, false)
}
}
/**
* logCartEvent is used to log the events related to the cart. You can use this event to log
* When an item was added or removed from the cart.
*
* @param id used to log the cartId the event is logged for. It is recommended to
* include the cartIds for the defined list types so that they can be
* tracked.
* @param cartAction is required to pass the actions for cart the logged is triggered on.
* By default, the SDK provides 2 main list actions for e-commerce apps.
* Which includes addItem and removeItem
* @param item is used to pass the whole item as a JSON String object as well. You
* can use the POJO ItemModel to parse the data in the required format
* and pass that to this function as a string to log the event.
* @param cart_price is required to log the total price for the cart being logged. Details
* about the cart are to be provided in the catalog.
* @param currency is required to log the currency for cart price being logged. Details
* about the cart are to be provided in the catalog.
* @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 logCartEvent = (properties: CartProperties) => {
if (Platform.OS === 'android') {
console.log(properties)
BsLogECom.logCartEvent(properties.id, properties.action, JSON.stringify(properties.item), properties.cart_price, properties.currency, null, false)
}
}
/**
* logCheckoutEvent is to track the success/failed use cases for the order placement. it can track
* kpis for the order being placed based on the ratio of how many orders were successfully and
* how many were not and what are the order aspects.
*
* @param id is for the orderId being placed. In case of not being placed and not
* having a valid orderId, you can pass the cartId instead.
* @param cartId is for the cart being checked out.
* @param is_successful is for the order if it was placed successfully or not.
* @param cart_price is required to log the total price of the order being logged.
* Price format should be in accordance with the currency selected.
* @param currency is required to log the currency for the order logged. Currency
* Should be in ISO 4217 format. For ease, SDK provides the enums
* CountryCode to log the currency so that it would be easy to log.
* You can also use the string function to provide the currency.
* @param items_list can be used to add the item being ordered to the checkout list.
* Order items should be in a valid format. With elements of the
* order Object as: OrderItem(itemID, price, quantity, promoId)
* Promo Id can be an empty string or no value at all if the item
* does not have a promo offer that is obtained by the user. You can
* add multiple addOrder functions to one checkout event to include
* all the items in order.
* @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 logCheckoutEvent = (properties: CheckoutProperties) => {
if (Platform.OS === 'android') {
console.log(properties)
BsLogECom.logCheckoutEvent(properties.id, properties.cart_id, properties.is_successful, properties.cart_price, properties.currency, JSON.stringify(properties.items_list), null, false)
}
}
/**
* logDeliveryEvent is used to log the status for the delivery. It can be used to log the
* delivered status of the order or partial order. Details about the items in the specific
* delivery should be provided in the catalog.
*
* @param id is required to associate the rating obtained for the order. OrderId
* Should be a valid orderId and can be tracked from the catalog.
* @param action is required to set the delivery action for the log. For the order
* being prepared for delivery or left the shipment center or
* delivered to the customer.
* @param delivery_id is required to associate the rating obtained for the order.
* deliveryId should be a valid deliveryId and can be tracked from the
* catalog for the items in that specific delivery.
* @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 logDeliveryEvent = (properties: DeliveryProperties) => {
if (Platform.OS === 'android') {
console.log(properties)
BsLogECom.logDeliveryEvent(properties.order_id, properties.action, properties.id, null, false)
}
}
/**
* logScheduleDeliveryEvent is used to log the status for the delivery. It can be used to log the
* delivered status of the order or a partial order. Details about the items in the specific
* delivery should be provided in the catalog.
*
* @param order_id is required to associate the rating obtained for the order. OrderId should
* be a valid orderId and can be tracked from the catalog
* @param is_urgent isUrgent is to mark the log if the delivery is scheduled to be an immediate or emergency
* delivery.
* @param action is required to set the delivery action for the log. For the order
* being prepared for delivery that if the item is being scheduled or updated in terms of
* delivery elements
* @param delivery_ts The timestamp in time in milliseconds format for the date and time for which the
* delivery is set to be scheduled
* @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 logScheduleDeliveryEvent = (properties: ScheduledDeliveryProperties) => {
if (Platform.OS === 'android') {
console.log(properties)
BsLogECom.logScheduleDeliveryEvent(properties.order_id, properties.is_urgent, properties.action,
properties.delivery_ts, null, false)
}
}
/**
* logCancelCheckoutEvent is used to log the event when the order/cart is canceled.
*
* @param id can be used to log the orderId and cartId the event is logged for.
* This should be in accordance to the type provided in the log and
* should represent the actual element being cancelled/discarded.
* @param type is the type of checkout element being canceled, cart or order
* @param itemsList is required to provide the list of item types that are present
* in the order being cancelled.
* @param reason is required to define the reason for which the item is being cancelled.
* @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 logCancelCheckoutEvent = (properties: CancelCheckoutProperties) => {
if (Platform.OS === 'android') {
console.log(properties)
BsLogECom.logCancelCheckoutEvent(properties.id, properties.type, JSON.stringify(properties.items_list), properties.reason, null, false)
}
}
/**
* logItemImpressionEvent is used to log the impression on items in a list that can be on home screen or as
* a search result. In case of a search result, the search Id returned by the search log should be
* included in the log to map the results.
*
* @param collection_id is required to associate the current recycler impression listener to a
* unique UI element.
* @param visible_item_indices is the list if current visible elements at any time on a screen, provided
* by {onVisibleIndicesChanged} from the recyclerListView
* @param content_list is the mapped list of elements for the impression view to look into.
* This should be mapped with Item Model provided by the SDK
* @param search_id is the for use case when the recyclerListView is for the search results
* and searchId should be the one provided by the search log.
*/
const logItemImpressionEvent = (collection_id : string, visible_item_indices : any, content_list : any, search_id : string) => {
if (Platform.OS === 'android') {
var visibleContent = visible_item_indices.map((x: any) => content_list[x]);
console.log(JSON.stringify(visibleContent))
BsLogECom.logItemImpressionEvent(collection_id, JSON.stringify(visibleContent), search_id);
}
}
export default {
logItemEvent,
logCartEvent,
logCheckoutEvent,
logDeliveryEvent,
logItemImpressionEvent,
logScheduleDeliveryEvent,
logCancelCheckoutEvent
}