in extensions/applicationinsights-react-native/src/ReactNativePlugin.ts [56:197]
constructor(config?: IReactNativePluginConfig) {
super();
let _device: INativeDevice = {};
let _config: IReactNativePluginConfig = config || _getDefaultConfig();
let _analyticsPlugin: IAppInsights;
let _defaultHandler;
dynamicProto(ReactNativePlugin, this, (_self, _base) => {
_self.initialize = (
config?: IReactNativePluginConfig | object, // need `| object` to coerce to interface
core?: IAppInsightsCore,
extensions?: IPlugin[]
) => {
if (!_self.isInitialized()) {
_base.initialize(config, core, extensions);
const inConfig = config || {};
const defaultConfig = _getDefaultConfig();
objForEachKey(defaultConfig, (option, value) => {
_config[option] = ConfigurationManager.getConfig(
inConfig as any,
option,
_self.identifier,
!isUndefined(_config[option]) ? _config[option] : value
);
});
if (!_config.disableDeviceCollection) {
_self._collectDeviceInfo();
}
if (extensions) {
arrForEach(extensions, ext => {
const identifier = (ext as ITelemetryPlugin).identifier;
if (identifier === AnalyticsPluginIdentifier) {
_analyticsPlugin = (ext as any) as IAppInsights;
}
});
}
if (!_config.disableExceptionCollection) {
_self._setExceptionHandler();
}
}
};
_self.processTelemetry = (item: ITelemetryItem, itemCtx?: IProcessTelemetryContext) => {
_applyDeviceContext(item);
_self.processNext(item, itemCtx);
};
_self.setDeviceId = (newId: string) => {
_device.id = newId;
};
_self.setDeviceModel = (newModel: string) => {
_device.model = newModel;
};
_self.setDeviceType = (newType: string) => {
_device.deviceClass = newType;
};
/**
* Automatically collects native device info for this device
*/
_self._collectDeviceInfo = () => {
try {
_device.deviceClass = DeviceInfo.getDeviceType();
_device.id = DeviceInfo.getUniqueId(); // Installation ID
_device.model = DeviceInfo.getModel();
} catch (e) {
_self.diagLog().warnToConsole("Failed to get DeviceInfo: " + getExceptionName(e) + " - " + dumpObj(e));
}
}
function _applyDeviceContext(item: ITelemetryItem) {
if (_device) {
item.ext = item.ext || {};
item.ext.device = item.ext.device || ({} as IDevice);
if (typeof _device.id === "string") {
item.ext.device.localId = _device.id;
}
if (typeof _device.model === "string") {
item.ext.device.model = _device.model;
}
if (typeof _device.deviceClass === "string") {
item.ext.device.deviceClass = _device.deviceClass;
}
}
}
function _getGlobal(): any {
if (typeof global !== strShimUndefined && global) {
return global as any;
}
return getGlobal() as any;
}
_self._setExceptionHandler = () => {
const _global = _getGlobal();
if (_global && _global.ErrorUtils) {
// intercept react-native error handling
_defaultHandler = (typeof _global.ErrorUtils.getGlobalHandler === "function" && _global.ErrorUtils.getGlobalHandler()) || _global.ErrorUtils._globalHandler;
_global.ErrorUtils.setGlobalHandler(_trackException.bind(this));
}
}
// default global error handler syntax: handleError(e, isFatal)
function _trackException(e, isFatal) {
const exception: IExceptionTelemetry = { exception: e, severityLevel: SeverityLevel.Error };
if (_analyticsPlugin) {
_analyticsPlugin.trackException(exception);
} else {
_self.diagLog().throwInternal(
LoggingSeverity.CRITICAL, _InternalMessageId.TelemetryInitializerFailed, "Analytics plugin is not available, ReactNative plugin telemetry will not be sent: ");
}
// call the _defaultHandler - react native also gets the error
if (_defaultHandler) {
_defaultHandler.call(global, e, isFatal);
}
}
// Test Hooks
(_self as any)._config = _config;
(_self as any)._getDbgPlgTargets = () => {
return [_device];
}
});
function _getDefaultConfig(): IReactNativePluginConfig {
return {
// enable auto collection by default
disableDeviceCollection: false,
disableExceptionCollection: false
};
}
}