import Logger from '../core/logging/logger'; import { ContentsquareModule } from '../core/nativeModules'; import { CustomVar } from '../types/types'; import { Currency } from './constants'; /** * Re-enables the Contentsquare UX tracking SDK. */ export const resumeTracking = (): void => { ContentsquareModule.resumeTracking(); }; /** * Stops the Contentsquare UX tracking SDK. * Once this is called the SDK will completely stop. */ export const stopTracking = (): void => { ContentsquareModule.stopTracking(); }; /** * Sends a ScreenName for a CustomScreenView Event to Contentsquare tracking * services. * * @param {string} screenName. * @param {CustomVar[]} cvars (optional parameter) */ export const send = (screenName: string, cvars?: CustomVar[]): void => { ContentsquareModule.send(screenName, cvars); }; /** * Sends a transaction event to Contentsquare tracking services. * * @param {number} value * @param {Currency | string} currency * @param {string | null } id (optional parameter) */ export const sendTransaction = ( 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.sendTransactionWithStringCurrency(id, value, currency); } else if (Object.values(Currency).includes(currency)) { ContentsquareModule.sendTransaction(id, value, currency); } else { Logger.error( 'The currency value is not recognized. It is neither a string nor a Currency object type.' ); } }; /** * Sends a (key, value) pair, called DynamicVar, to Contentsquare tracking services. * * @param {string} key * @param {string | number} value * @param {(error: Error) => void} onError */ export const sendDynamicVar = ( key: string, value: string | number, onError?: (error: Error) => void ): void => { if (typeof value === 'string') { ContentsquareModule.sendDynamicStringVar(key, value); } else if (Number.isInteger(value) && value >= 0) { ContentsquareModule.sendDynamicIntVar(key, value); } else { const callbackError = new TypeError( 'The value of the DynamicVar should be either a string or an unsigned integer' ); if (typeof onError === 'function') { onError(callbackError); } else { console.log(callbackError.message); } } };