import { Platform } from 'react-native'; import packageJson from '../../package.json'; import Logger from '../core/logging/logger'; import { ContentsquareModule } from '../core/nativeModules'; import { CustomVar, PropertyValue, SourceInfo } from '../types/types'; import { extractReactNativeVersion } from '../utils/utils'; import { Currency } from './constants'; /** * Sends a transaction event to Contentsquare tracking services. * * @param {number} value * @param {Currency | string} currency * @param {string | null } id (optional parameter) */ export const trackTransaction = ( value: number, currency: Currency | string, id: string | null = null ): void => { if (typeof value !== 'number') { Logger.error( 'The value of the transaction is not recognized. It must be a number.', true ); return; } if (typeof currency === 'string') { ContentsquareModule.trackTransactionWithStringCurrency(id, value, currency); } else if (Object.values(Currency).includes(currency)) { ContentsquareModule.trackTransaction(id, value, currency); } else { Logger.error( 'The currency value is not recognized. It is neither a string nor a Currency object type.' ); } }; /** * Sends a ScreenName for a CustomScreenView Event to Contentsquare tracking * services. * * @param {string} screenName. * @param {CustomVar[]} cvars (optional parameter) */ export const trackScreenview = (name: string, cvars?: CustomVar[]): void => { const sourceInfo: SourceInfo = { name: 'react_native_bridge', version: packageJson.version, platform: `React Native ${ extractReactNativeVersion(Platform) || '(unknown version)' }`, properties: {}, }; ContentsquareModule.trackScreenView(name, cvars, sourceInfo); }; /** * Sends an event message to be tracked to the tracking services. * * @param {string} event - The string name of the event. * @param {Object} properties - (Optional) The object of properties to associate with the event. */ export const trackEvent = ( event: string, properties?: Record ): void => { const sourceInfo: SourceInfo = { name: 'react_native_bridge', version: packageJson.version, platform: `React Native ${ extractReactNativeVersion(Platform) || '(unknown version)' }`, properties: {}, }; ContentsquareModule.trackEvent(event, properties, sourceInfo); };