|
| 1 | +/* |
| 2 | + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. |
| 3 | + * This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | + * Copyright 2016-Present Datadog, Inc. |
| 5 | + */ |
| 6 | + |
| 7 | +import { InternalLog } from '../../../InternalLog'; |
| 8 | +import { SdkVerbosity } from '../../../SdkVerbosity'; |
| 9 | +import DdNativeRum from '../../../specs/NativeDdRum'; |
| 10 | +import DdSdk from '../../../specs/NativeDdSdk'; |
| 11 | +import { getBabelTelemetryConfig } from '../../../utils/telemetry'; |
| 12 | +import { DefaultTimeProvider } from '../../../utils/time-provider/DefaultTimeProvider'; |
| 13 | +import type { TimeProvider } from '../../../utils/time-provider/TimeProvider'; |
| 14 | +import { BABEL_PLUGIN_TELEMETRY } from '../../constants'; |
| 15 | +import type { RumActionType } from '../../types'; |
| 16 | +import { ActionSource } from '../../types'; |
| 17 | + |
| 18 | +const StateErrors = { |
| 19 | + ALREADY_INITIALIZED: |
| 20 | + 'Interaction Tracking singleton already initialized, please use `getInstance`.' |
| 21 | +} as const; |
| 22 | + |
| 23 | +type BabelConfig = { |
| 24 | + trackInteractions: boolean; |
| 25 | + useAccessibilityLabel: boolean; |
| 26 | +}; |
| 27 | + |
| 28 | +type TargetObject = { |
| 29 | + compoenentName: string; |
| 30 | + 'dd-action-name': string; |
| 31 | + accessibilityLabel: string; |
| 32 | + [key: string]: string; |
| 33 | +}; |
| 34 | + |
| 35 | +export class DdBabelInteractionTracking { |
| 36 | + private static instance: DdBabelInteractionTracking | null = null; |
| 37 | + |
| 38 | + static config: BabelConfig = { |
| 39 | + trackInteractions: false, |
| 40 | + useAccessibilityLabel: true |
| 41 | + }; |
| 42 | + |
| 43 | + private timeProvider: TimeProvider = new DefaultTimeProvider(); |
| 44 | + |
| 45 | + private telemetrySent: boolean = false; |
| 46 | + |
| 47 | + isInitialized: boolean = false; |
| 48 | + |
| 49 | + private constructor() { |
| 50 | + if (DdBabelInteractionTracking.instance) { |
| 51 | + throw new Error(StateErrors.ALREADY_INITIALIZED); |
| 52 | + } |
| 53 | + |
| 54 | + DdBabelInteractionTracking.instance = this; |
| 55 | + } |
| 56 | + |
| 57 | + static getInstance() { |
| 58 | + if (!DdBabelInteractionTracking.instance) { |
| 59 | + DdBabelInteractionTracking.instance = new DdBabelInteractionTracking(); |
| 60 | + } |
| 61 | + |
| 62 | + return DdBabelInteractionTracking.instance; |
| 63 | + } |
| 64 | + |
| 65 | + private getTargetName(targetObject: TargetObject) { |
| 66 | + const { |
| 67 | + componentName, |
| 68 | + 'dd-action-name': actionName, |
| 69 | + accessibilityLabel, |
| 70 | + ...attrs |
| 71 | + } = targetObject; |
| 72 | + |
| 73 | + const { useAccessibilityLabel } = DdBabelInteractionTracking.config; |
| 74 | + |
| 75 | + if (actionName) { |
| 76 | + return actionName; |
| 77 | + } |
| 78 | + |
| 79 | + const keys = Object.keys(attrs); |
| 80 | + if (keys.length) { |
| 81 | + return attrs[keys[0]]; |
| 82 | + } |
| 83 | + |
| 84 | + if (useAccessibilityLabel && accessibilityLabel) { |
| 85 | + return accessibilityLabel; |
| 86 | + } |
| 87 | + |
| 88 | + return componentName; |
| 89 | + } |
| 90 | + |
| 91 | + wrapRumAction( |
| 92 | + func: (...args: any[]) => any, |
| 93 | + action: RumActionType, |
| 94 | + targetObject: TargetObject |
| 95 | + ): (...args: any[]) => any { |
| 96 | + return (...args: any[]) => { |
| 97 | + const result = func(...args); |
| 98 | + |
| 99 | + if (!this.telemetrySent) { |
| 100 | + DdSdk?.sendTelemetryLog( |
| 101 | + BABEL_PLUGIN_TELEMETRY, |
| 102 | + getBabelTelemetryConfig(), |
| 103 | + { onlyOnce: true } |
| 104 | + ); |
| 105 | + |
| 106 | + this.telemetrySent = true; |
| 107 | + } |
| 108 | + |
| 109 | + const targetName = this.getTargetName(targetObject); |
| 110 | + |
| 111 | + const { trackInteractions } = DdBabelInteractionTracking.config; |
| 112 | + |
| 113 | + if (trackInteractions) { |
| 114 | + InternalLog.log( |
| 115 | + `Adding RUM Action “${targetName}” (${action}, auto)`, |
| 116 | + SdkVerbosity.DEBUG |
| 117 | + ); |
| 118 | + |
| 119 | + DdNativeRum?.addAction( |
| 120 | + action, |
| 121 | + targetName, |
| 122 | + { '__dd.action_source': ActionSource.BABEL }, |
| 123 | + this.timeProvider.now() |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + return result; |
| 128 | + }; |
| 129 | + } |
| 130 | +} |
0 commit comments