@datadog/mobile-react-native
Version:
A client-side React Native module to interact with Datadog
84 lines (83 loc) • 2.82 kB
JavaScript
/*
* 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.
*/
import { InternalLog } from '../../../InternalLog';
import { SdkVerbosity } from '../../../SdkVerbosity';
import DdNativeRum from '../../../specs/NativeDdRum';
import DdSdk from '../../../specs/NativeDdSdk';
import { getBabelTelemetryConfig } from '../../../utils/telemetry';
import { DefaultTimeProvider } from '../../../utils/time-provider/DefaultTimeProvider';
import { BABEL_PLUGIN_TELEMETRY } from '../../constants';
import { ActionSource } from '../../types';
const StateErrors = {
ALREADY_INITIALIZED: 'Interaction Tracking singleton already initialized, please use `getInstance`.'
};
export class DdBabelInteractionTracking {
static instance = null;
static config = {
trackInteractions: false,
useAccessibilityLabel: true
};
timeProvider = new DefaultTimeProvider();
telemetrySent = false;
isInitialized = false;
constructor() {
if (DdBabelInteractionTracking.instance) {
throw new Error(StateErrors.ALREADY_INITIALIZED);
}
DdBabelInteractionTracking.instance = this;
}
static getInstance() {
if (!DdBabelInteractionTracking.instance) {
DdBabelInteractionTracking.instance = new DdBabelInteractionTracking();
}
return DdBabelInteractionTracking.instance;
}
getTargetName(targetObject) {
const {
componentName,
'dd-action-name': actionName,
accessibilityLabel,
...attrs
} = targetObject;
const {
useAccessibilityLabel
} = DdBabelInteractionTracking.config;
if (actionName) {
return actionName;
}
const keys = Object.keys(attrs);
if (keys.length) {
return attrs[keys[0]];
}
if (useAccessibilityLabel && accessibilityLabel) {
return accessibilityLabel;
}
return componentName;
}
wrapRumAction(func, action, targetObject) {
return (...args) => {
const result = func(...args);
if (!this.telemetrySent) {
DdSdk?.sendTelemetryLog(BABEL_PLUGIN_TELEMETRY, getBabelTelemetryConfig(), {
onlyOnce: true
});
this.telemetrySent = true;
}
const targetName = this.getTargetName(targetObject);
const {
trackInteractions
} = DdBabelInteractionTracking.config;
if (trackInteractions) {
InternalLog.log(`Adding RUM Action “${targetName}” (${action}, auto)`, SdkVerbosity.DEBUG);
DdNativeRum?.addAction(action, targetName, {
'__dd.action_source': ActionSource.BABEL
}, this.timeProvider.now());
}
return result;
};
}
}
//# sourceMappingURL=DdBabelInteractionTracking.js.map