UNPKG

react-native-site24x7-rn

Version:

Monitor react native mobile applications with site24x7 Mobile RUM

283 lines (265 loc) 8.17 kB
import { NativeModules } from 'react-native'; import networkTracker from './site24x7RnNetworkTracker'; import errorTracker from './site24x7RnErrorTracker'; import screenTracker from './site24x7RnScreenTracker'; import utils from './site24x7Utils'; import ErrorBoundary from './site24x7ErrorBoundary'; const { Site24x7Rn } = NativeModules; const screenTrackerInstance = new screenTracker(); let isInitialised = false, isErrorMonitoringInitialised=false; let errorMonitor; module.exports = { /** * Starts Tracking with basic configuration (apiKey, endPoint, uploadInterval) * * @param {object} configuration */ startMonitoringWithConfig : (configuration)=>{ if(utils.isObject(configuration) && !utils.isEmpty(configuration) && !isInitialised){ const { apiKey='',endPoint='https://col.site24x7rum.com/rum/',uploadInterval=60,ignoredScreens=[]} = configuration; if(!utils.isString(apiKey)){ throw new Error( "startMonitoringWithConfig() 'apiKey' must be a string.",//No I18N ); } if(!utils.isString(endPoint)){ throw new Error( "startMonitoringWithConfig() 'endPoint' must be a string.",//No I18N ); } if(!utils.isNumber(uploadInterval) && !utils.isFinite(uploadInterval)){ throw new Error( "startMonitoringWithConfig() 'uploadInterval' must be a number.",//No I18N ); } Site24x7Rn.startMonitoring(apiKey,''+uploadInterval); if(ignoredScreens.length>0){ screenTrackerInstance.ignoreScreens(ignoredScreens); } isInitialised = true; } }, /** * Starts Tracking network http calls and option to configure http calls that should be ignored * * @param {Array} ignoredHttpCalls */ initializeNetworkMonitoring : (ignoredHttpCalls)=>{ if(!isInitialised){ return; } if(ignoredHttpCalls && !utils.isArray(ignoredHttpCalls)){ throw new Error( "initializeNetworkMonitoring() 'Ignored Http Calls' must be a array.",//No I18N ); } const networkMonitor = new networkTracker(true,utils.isEmpty(ignoredHttpCalls)?[]:ignoredHttpCalls); }, /** * Starts Tracking jsErrors with options to enable disable tracking error types (trackUnhandledErrors,trackConsoleErrors,trackUnhandledRejections,trackNativeErrors) * * @param {object} configuration */ initializeErrorMonitoring : (configuration)=>{ if(utils.isObject(configuration) && !utils.isEmpty(configuration) && !isErrorMonitoringInitialised){ const { trackUnhandledErrors=false,trackConsoleErrors=false,trackUnhandledRejections=false,trackNativeErrors=false} = configuration; if(!utils.isBoolean(trackUnhandledErrors)){ throw new Error( "initializeErrorMonitoring() 'trackUnhandledErrors' must be a boolean.",//No I18N ); } if(!utils.isBoolean(trackConsoleErrors)){ throw new Error( "initializeErrorMonitoring() 'trackConsoleErrors' must be a boolean.",//No I18N ); } if(!utils.isBoolean(trackUnhandledRejections)){ throw new Error( "initializeErrorMonitoring() 'trackUnhandledRejections' must be a boolean.",//No I18N ); } if(!utils.isBoolean(trackNativeErrors)){ throw new Error( "initializeErrorMonitoring() 'trackNativeErrors' must be a boolean.",//No I18N ); } if(utils.isEmpty(errorMonitor)){ errorMonitor = new errorTracker(trackUnhandledErrors,trackConsoleErrors,trackUnhandledRejections); } if(trackNativeErrors){ Site24x7Rn.trackNativeExceptions(); } isErrorMonitoringInitialised = true; } }, /** * Sets an user identifier * * @param {string} userId */ setUserId : (userId)=>{ if(!isInitialised){ return; } if(!utils.isString(userId)){ throw new Error( "setUserId() 'userId' must be a string.",//No I18N ); } Site24x7Rn.setUserId(utils.isEmpty(userId)?'':userId); }, /** * Option to add screen manually with loadtime of the screen * * @param {string} screenName * @param {number} loadTime */ addScreen: (screenName,loadTime)=>{ if(!isInitialised){ return; } if(!utils.isString(screenName)){ throw new Error( "addScreen() 'screenName' must be a string.",//No I18N ); } if(!utils.isNumber(loadTime) && !utils.isFinite(loadTime)){ loadTime = 0; } screenTrackerInstance.addScreen(screenName,loadTime); }, /** * Option to crash the app with site24x7 error message * */ crashApplication : ()=>{ throw new Error("Site24x7 RN error");//No I18N }, crashNative : ()=>{ Site24x7Rn.crashNative(); }, /** * Starts transaction with unique txnName * * @param {string} txnName */ startTransaction : function(txnName){ if(!isInitialised){ return; } if(!utils.isString(txnName) || utils.isEmptyString(txnName)){ throw new Error( "startTransaction() 'txnName' must be a string & not empty.",//No I18N ); } Site24x7Rn.startTransaction(txnName); }, /** * Stops transaction with unique txnName * * @param {string} txnName */ stopTransaction :(txnName)=>{ if(!isInitialised){ return; } if(!utils.isString(txnName) || utils.isEmptyString(txnName)){ throw new Error( "stopTransaction() 'txnName' must be a string & not empty.",//No I18N ); } Site24x7Rn.stopTransaction(txnName); }, /** * Starts component with unique txnName & unique componentName * * @param {string} txnName * @param {string} componentName */ startComponent :(txnName,componentName)=>{ if(!isInitialised){ return; } if(!utils.isString(txnName) || utils.isEmptyString(txnName) || !utils.isString(componentName) || utils.isEmptyString(componentName)){ throw new Error( "startComponent() 'txnName' 'componentName' must be a string & not empty.",//No I18N ); } Site24x7Rn.startComponent(txnName,componentName); }, /** * stops component with unique txnName & unique componentName * * @param {string} txnName * @param {string} componentName */ stopComponent :(txnName,componentName)=>{ if(!isInitialised){ return; } if(!utils.isString(txnName) || utils.isEmptyString(txnName) || !utils.isString(componentName) || utils.isEmptyString(componentName)){ throw new Error( "stopComponent() 'txnName' 'componentName' must be a string & not empty.",//No I18N ); } Site24x7Rn.stopComponent(txnName,componentName); }, /** * Flushes the data to site24x7 servers * */ flush : ()=>{ Site24x7Rn.flush(); }, /** * @param {object} err; * Captures the Exception, mostly used in catch block */ captureException : (err) => { if(utils.isEmpty(errorMonitor)){ throw new Error( "Initialize the error monitoring, to capture error/exception" //No I18N ); }else{ utils.addUserBreadCrumbs("Info", "Handled Exception"); //No I18N errorMonitor.parseErrorWithHandledExceptionCheck(err, true, true); } }, /** * * @param {string} event * @param {string} msg */ addBreadCrumb : (event, msg) => { if(utils.isEmptyString(event) || utils.isEmptyString(msg) || !utils.isString(event) || !utils.isString(msg)){ throw new Error( "addBreadCrumb 'event' 'msg' must be a string & not empty" //No I18N ); }else{ utils.addUserBreadCrumbs(event, msg); } }, ErrorBoundary : ErrorBoundary, /** * * @param {string[]} screensToBeIgnored */ excludeNativeScreens : (screensToBeIgnored) => { if(utils.isValidStringArray(screensToBeIgnored)){ Site24x7Rn.excludeNativeScreens(screensToBeIgnored); } }, /** * * @param {string} env */ setEnvironment : (env) => { if(utils.isEmpty(env) || !utils.isString(env)){ throw new Error( "environment must be a string & not empty" //No I18N ); }else{ Site24x7Rn.setEnvironment(env); } } }