Skip to content

Commit 4163546

Browse files
Merge pull request #2348 from DataDog/mariedm/rum-9988-expand-swiftui-action-tracking
RUM-9988 Expand Action Tracking to other UI components
2 parents 149a1ac + 1c9889d commit 4163546

File tree

6 files changed

+4
-140
lines changed

6 files changed

+4
-140
lines changed

DatadogRUM/Sources/Instrumentation/Actions/SwiftUI/LegacySwiftUIComponentDetector.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,7 @@ internal final class LegacySwiftUIComponentDetector: SwiftUIComponentDetector {
4040
}
4141
}
4242

43-
// Special detection for SwiftUI Toogle
44-
return SwiftUIComponentHelpers.extractSwiftUIToggleAction(
45-
from: touch,
46-
predicate: predicate,
47-
dateProvider: dateProvider
48-
)
43+
return nil
4944
}
5045
}
5146

DatadogRUM/Sources/Instrumentation/Actions/SwiftUI/ModernSwiftUIComponentDetector.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,6 @@ internal final class ModernSwiftUIComponentDetector: SwiftUIComponentDetector {
4848
) {
4949
return command
5050
}
51-
52-
// Special detection for SwiftUI Toogle
53-
return SwiftUIComponentHelpers.extractSwiftUIToggleAction(
54-
from: touch,
55-
predicate: predicate,
56-
dateProvider: dateProvider
57-
)
5851
}
5952

6053
return nil

DatadogRUM/Sources/Instrumentation/Actions/SwiftUI/SwiftUIComponentDetector.swift

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import DatadogInternal
1111
internal enum SwiftUIComponentNames {
1212
static let button = "SwiftUI_Button"
1313
static let navigationLink = "SwiftUI_NavigationLink"
14-
static let toggle = "SwiftUI_Toggle"
1514
static let unidentified = "SwiftUI_Unidentified_Element"
1615
}
1716

@@ -90,43 +89,6 @@ internal class SwiftUIComponentHelpers {
9089

9190
return defaultName
9291
}
93-
94-
/// Extracts a SwiftUI Toggle action from a touch if applicable
95-
/// - Parameters:
96-
/// - touch: The `UITouch` to analyze
97-
/// - predicate: The predicate to use for determining if an action should be created
98-
/// - dateProvider: Provider for current time
99-
/// - Returns: A RUM action command if a toggle was detected, `nil` otherwise
100-
static func extractSwiftUIToggleAction(
101-
from touch: UITouch,
102-
predicate: SwiftUIRUMActionsPredicate?,
103-
dateProvider: DateProvider
104-
) -> RUMAddUserActionCommand? {
105-
guard let predicate = predicate,
106-
let view = touch.view else {
107-
return nil
108-
}
109-
110-
#if !os(tvOS)
111-
let uiSwitch = view.findInParentHierarchy { parent in
112-
// Note: we might want to extend to `UIControl` in the future.
113-
return parent is UISwitch
114-
}
115-
116-
if uiSwitch != nil,
117-
let rumAction = predicate.rumAction(with: SwiftUIComponentNames.toggle) {
118-
return RUMAddUserActionCommand(
119-
time: dateProvider.now,
120-
attributes: rumAction.attributes,
121-
instrumentation: .swiftuiAutomatic,
122-
actionType: .tap,
123-
name: rumAction.name
124-
)
125-
}
126-
#endif
127-
128-
return nil
129-
}
13092
}
13193

13294
/// Protocol defining interface for type description functionality

DatadogRUM/Sources/Instrumentation/Actions/UIKit/UIEventCommandFactory.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ internal final class UITouchCommandFactory: UIEventCommandFactory {
106106
let bestParent = view.findInParentHierarchy { parent in
107107
return parent is UITableViewCell
108108
|| parent is UICollectionViewCell
109+
|| parent is UIControl
109110
}
110111
return bestParent // best parent or `nil`
111112
}

DatadogRUM/Tests/Instrumentation/Actions/SwiftUIComponentDetectorTests.swift

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -116,40 +116,6 @@ class SwiftUIComponentDetectorTests: XCTestCase {
116116
XCTAssertNil(command)
117117
}
118118

119-
#if !os(tvOS)
120-
@available(iOS 18.0, tvOS 18.0, *)
121-
func testModernDetector_DetectsToggle() {
122-
// Given
123-
let detector = ModernSwiftUIComponentDetector()
124-
125-
// Create a view hierarchy simulating a toggle
126-
let switchView = UIView()
127-
let parentView = UIView()
128-
let grandparentView = UISwitch()
129-
parentView.addSubview(switchView)
130-
grandparentView.addSubview(parentView)
131-
132-
let mockTouch = MockUITouch(
133-
phase: .ended,
134-
view: switchView
135-
)
136-
137-
// When
138-
let command = detector.createActionCommand(
139-
from: mockTouch,
140-
predicate: defaultPredicate,
141-
dateProvider: dateProvider
142-
)
143-
144-
// Then
145-
XCTAssertNotNil(command)
146-
XCTAssertEqual(command?.name, "SwiftUI_Toggle")
147-
XCTAssertEqual(command?.actionType, .tap)
148-
XCTAssertEqual(command?.instrumentation, .swiftuiAutomatic)
149-
XCTAssertEqual(command?.time, .mockDecember15th2019At10AMUTC())
150-
}
151-
#endif
152-
153119
@available(iOS 18.0, tvOS 18.0, *)
154120
func testModernDetector_IgnoresNonSwiftUIViews() {
155121
// Given
@@ -344,40 +310,6 @@ class SwiftUIComponentDetectorTests: XCTestCase {
344310
XCTAssertEqual(command?.time, .mockDecember15th2019At10AMUTC())
345311
}
346312

347-
#if !os(tvOS)
348-
func testLegacyDetector_DetectsToggle() {
349-
// Given
350-
let detector = LegacySwiftUIComponentDetector()
351-
let defaultPredicate = DefaultSwiftUIRUMActionsPredicate(isLegacyDetectionEnabled: true)
352-
353-
// Create a view hierarchy simulating a toggle
354-
let switchView = UIView()
355-
let parentView = UIView()
356-
let grandparentView = UISwitch()
357-
parentView.addSubview(switchView)
358-
grandparentView.addSubview(parentView)
359-
360-
let mockTouch = MockUITouch(
361-
phase: .ended,
362-
view: switchView
363-
)
364-
365-
// When
366-
let command = detector.createActionCommand(
367-
from: mockTouch,
368-
predicate: defaultPredicate,
369-
dateProvider: dateProvider
370-
)
371-
372-
// Then
373-
XCTAssertNotNil(command)
374-
XCTAssertEqual(command?.name, "SwiftUI_Toggle")
375-
XCTAssertEqual(command?.actionType, .tap)
376-
XCTAssertEqual(command?.instrumentation, .swiftuiAutomatic)
377-
XCTAssertEqual(command?.time, .mockDecember15th2019At10AMUTC())
378-
}
379-
#endif
380-
381313
func testLegacyDetector_IgnoresContainerViews() {
382314
// Given
383315
let detector = LegacySwiftUIComponentDetector()
@@ -679,25 +611,6 @@ private class MockGestureRecognizer: UIGestureRecognizer {
679611
}
680612
}
681613

682-
private class MockToggleView: UIView {
683-
var overrideIsSafeForPrivacy: Bool?
684-
var mockTypeDescription: String?
685-
var overrideIsSwiftUIView: Bool?
686-
687-
override var typeDescription: String {
688-
return mockTypeDescription ?? "ToggleView"
689-
}
690-
691-
// Override extension properties
692-
@objc override var isSafeForPrivacy: Bool {
693-
return overrideIsSafeForPrivacy ?? true
694-
}
695-
696-
@objc override var isSwiftUIView: Bool {
697-
return false
698-
}
699-
}
700-
701614
private class SwiftUIViewMock: UIView {
702615
var mockTypeDescription: String?
703616
var overrideIsSwiftUIView: Bool?

IntegrationTests/IntegrationScenarios/Scenarios/RUM/RUMSwiftUIAutoInstrumentationTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class RUMSwiftUIAutoInstrumentationTests: IntegrationTests, RUMCommonAsserts {
215215
if #available(iOS 18.0, tvOS 18.0, visionOS 18.0, *) {
216216
XCTAssertEqual(mainView.actionEvents[0].action.target?.name, SwiftUIComponentNames.button)
217217
XCTAssertEqual(mainView.actionEvents[1].action.target?.name, SwiftUIComponentNames.navigationLink)
218-
XCTAssertEqual(mainView.actionEvents[2].action.target?.name, "SwiftUI_Toggle")
218+
XCTAssertEqual(mainView.actionEvents[2].action.target?.name, "UISwitch")
219219
XCTAssertEqual(mainView.actionEvents[3].action.target?.name, "UISlider(slider)")
220220
XCTAssertEqual(mainView.actionEvents[4].action.target?.name, "UIStepper(stepper)")
221221
XCTAssertEqual(mainView.actionEvents[5].action.target?.name, "UISegmentedControl")
@@ -225,7 +225,7 @@ class RUMSwiftUIAutoInstrumentationTests: IntegrationTests, RUMCommonAsserts {
225225
} else {
226226
XCTAssertEqual(mainView.actionEvents[0].action.target?.name, SwiftUIComponentNames.unidentified)
227227
XCTAssertEqual(mainView.actionEvents[1].action.target?.name, SwiftUIComponentNames.unidentified)
228-
XCTAssertEqual(mainView.actionEvents[2].action.target?.name, "SwiftUI_Toggle")
228+
XCTAssertEqual(mainView.actionEvents[2].action.target?.name, "UISwitch")
229229
XCTAssertEqual(mainView.actionEvents[3].action.target?.name, "UISlider(slider)")
230230
XCTAssertEqual(mainView.actionEvents[4].action.target?.name, "UIStepper(stepper)")
231231
XCTAssertEqual(mainView.actionEvents[5].action.target?.name, "UISegmentedControl")

0 commit comments

Comments
 (0)