@datadog/mobile-react-native
Version:
A client-side React Native module to interact with Datadog
114 lines (110 loc) • 4.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.DdBabelInteractionTracking = void 0;
var _NativeDdSdk = _interopRequireDefault(require("../../../specs/NativeDdSdk"));
var _DefaultTimeProvider = require("../../../utils/time-provider/DefaultTimeProvider");
var _constants = require("../../constants");
var _types = require("../../types");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/
const StateErrors = {
ALREADY_INITIALIZED: 'Interaction Tracking singleton already initialized, please use `getInstance`.'
};
class DdBabelInteractionTracking {
static instance = null;
static config = {
trackInteractions: false,
useAccessibilityLabel: true
};
timeProvider = new _DefaultTimeProvider.DefaultTimeProvider();
telemetrySent = false;
ddRum = null;
isInitialized = false;
constructor(ddRum) {
if (DdBabelInteractionTracking.instance) {
throw new Error(StateErrors.ALREADY_INITIALIZED);
}
if (ddRum) {
this.ddRum = ddRum;
}
DdBabelInteractionTracking.instance = this;
}
static getInstance(ddRum) {
if (!DdBabelInteractionTracking.instance) {
DdBabelInteractionTracking.instance = new DdBabelInteractionTracking(ddRum);
}
return DdBabelInteractionTracking.instance;
}
static getTelemetryConfig() {
return {
babel_plugin: {
enabled: !!globalThis.__DD_RN_BABEL_PLUGIN_ENABLED__,
track_interactions: !!DdBabelInteractionTracking.config.trackInteractions
}
};
}
getTargetName(targetObject) {
const {
getContent,
options,
handlerArgs,
componentName,
'dd-action-name': actionName,
accessibilityLabel,
...attrs
} = targetObject;
const {
useAccessibilityLabel
} = DdBabelInteractionTracking.config;
const tryContent = () => {
const content = getContent?.();
if (content && content.length > 0) {
return content;
}
return null;
};
const getAccessibilityLabel = () => useAccessibilityLabel && accessibilityLabel ? accessibilityLabel : null;
const index = handlerArgs ? handlerArgs.find(x => typeof x === 'number') || 0 : 0;
// Order: content → actionName → actionNameAttribute → accessibilityLabel
const selectedContent = tryContent() || actionName || Object.values(attrs)[0] || getAccessibilityLabel();
if (!selectedContent) {
return componentName;
}
// Fail-safe in case the our 'index' value turns out to not be a real index
const output = index + 1 > selectedContent.length || index < 0 ? selectedContent[0] : selectedContent[index];
return options.useNamePrefix ? `${componentName} ("${output}")` : output;
}
wrapRumAction(func, action, targetObject) {
return (...args) => {
const result = func(...args);
if (!this.telemetrySent) {
_NativeDdSdk.default?.sendTelemetryLog(_constants.BABEL_PLUGIN_TELEMETRY, DdBabelInteractionTracking.getTelemetryConfig(), {
onlyOnce: true
});
this.telemetrySent = true;
}
const targetName = this.getTargetName(targetObject);
const {
trackInteractions
} = DdBabelInteractionTracking.config;
if (trackInteractions) {
this.ddRum?.addAction(action, targetName, {
'__dd.action_source': _types.ActionSource.BABEL
}, this.timeProvider.now()).catch(e => {
if (e instanceof Error) {
_NativeDdSdk.default?.telemetryError(e.message, e.stack || '', 'BabelActionTrack');
}
});
}
return result;
};
}
}
exports.DdBabelInteractionTracking = DdBabelInteractionTracking;
//# sourceMappingURL=DdBabelInteractionTracking.js.map