UNPKG

@datadog/mobile-react-native

Version:

A client-side React Native module to interact with Datadog

106 lines (103 loc) 3.66 kB
/* * 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 DdSdk from '../../../specs/NativeDdSdk'; 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; 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) { DdSdk?.sendTelemetryLog(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': ActionSource.BABEL }, this.timeProvider.now()).catch(e => { if (e instanceof Error) { DdSdk?.telemetryError(e.message, e.stack || '', 'BabelActionTrack'); } }); } return result; }; } } //# sourceMappingURL=DdBabelInteractionTracking.js.map