@benshi.ai/js-sdk
Version:
Benshi SDK
44 lines (35 loc) • 1.21 kB
text/typescript
import { createCheckers } from "ts-interface-checker"
import { getCoreInstance } from "../coreInstanceGetter"
import { AnyKnownType, EventTeaser } from "./typings"
import CommonTypesTI from './commonTypes-ti'
import { ContentBlock } from "../modules/Navigation/typings"
const injectEvent = (
properties: any,
modulePropertiesTI: any[],
eventType: any,
moduleName: ContentBlock,
userId = '',
sendNow = false
) => {
const capitalize = word => `${word[0].toUpperCase()}${word.slice(1)}`
// Generates the type name that defines the properties (ex.: VirtualCreditProperties),
// from the type of event (virtual_credit)
//
// ex.: virtual_credit -> VirtualCreditProperties
const typeName = eventType
.split('_')
.map(capitalize)
.join('')
.concat('Properties')
const { [typeName]: PropertiesChecker } = createCheckers(...modulePropertiesTI, CommonTypesTI)
PropertiesChecker.check(properties)
const bsLog = getCoreInstance()
const eventTeaser: EventTeaser = {
type: eventType,
props: properties
}
bsLog.trackEvent(eventTeaser, moduleName, userId, sendNow)
}
export {
injectEvent
}