Skip to content

Commit 6664ae2

Browse files
committedMar 19, 2025
RUM-8416 - Add view tracking option for SwiftUI
1 parent 46a05d1 commit 6664ae2

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed
 

‎DatadogRUM/Sources/Instrumentation/RUMInstrumentation.swift

+4-7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ internal final class RUMInstrumentation: RUMCommandPublisher {
4848
featureScope: FeatureScope,
4949
uiKitRUMViewsPredicate: UIKitRUMViewsPredicate?,
5050
uiKitRUMActionsPredicate: UIKitRUMActionsPredicate?,
51+
swiftUIRUMViewsPredicate: SwiftUIRUMViewsPredicate?,
5152
longTaskThreshold: TimeInterval?,
5253
appHangThreshold: TimeInterval?,
5354
mainQueue: DispatchQueue,
@@ -59,20 +60,16 @@ internal final class RUMInstrumentation: RUMCommandPublisher {
5960
watchdogTermination: WatchdogTerminationMonitor?,
6061
memoryWarningMonitor: MemoryWarningMonitor
6162
) {
62-
// MARK: TODO: RUM-8416 - Remove after we add SwiftUI view instrumentation option
63-
// Always create views handler (we can't know if it will be used by SwiftUI instrumentation)
64-
// and only swizzle `UIViewController` if UIKit instrumentation is configured:
6563
let viewsHandler = RUMViewsHandler(
6664
dateProvider: dateProvider,
6765
uiKitPredicate: uiKitRUMViewsPredicate,
68-
swiftUIPredicate: nil,
69-
swiftUIViewNameExtractor: nil,
66+
swiftUIPredicate: swiftUIRUMViewsPredicate,
67+
swiftUIViewNameExtractor: SwiftUIReflectionBasedViewNameExtractor(),
7068
notificationCenter: notificationCenter
7169
)
7270
let viewControllerSwizzler: UIViewControllerSwizzler? = {
7371
do {
74-
// MARK: TODO: RUM-8416 - Check both predicates after we add SwiftUI view instrumentation option
75-
if uiKitRUMViewsPredicate != nil {
72+
if uiKitRUMViewsPredicate != nil || swiftUIRUMViewsPredicate != nil {
7673
return try UIViewControllerSwizzler(handler: viewsHandler)
7774
}
7875
} catch {

‎DatadogRUM/Sources/RUMConfiguration.swift

+18-2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ extension RUM {
8484
/// Default: `nil` - which means automatic RUM action tracking is not enabled by default.
8585
public var uiKitActionsPredicate: UIKitRUMActionsPredicate?
8686

87+
/// The predicate for automatically tracking `UIViewControllers` as RUM views.
88+
///
89+
/// RUM will query this predicate for each `UIViewController` presented in the app. The predicate implementation
90+
/// should return RUM view parameters if the given controller should start a view, or `nil` to ignore it.
91+
///
92+
/// You can use `DefaultSwiftUIRUMViewsPredicate` or create your own predicate by implementing `SwiftUIRUMViewsPredicate`.
93+
///
94+
/// Note: Automatic RUM views tracking involves swizzling the `UIViewController` lifecycle methods.
95+
///
96+
/// Default: `nil` - which means automatic RUM view tracking is not enabled by default.
97+
@available(*, message: "This API is experimental and may change in future releases")
98+
public var swiftUIViewsPredicate: SwiftUIRUMViewsPredicate?
99+
87100
/// The configuration for automatic RUM resources tracking.
88101
///
89102
/// RUM resources tracking requires enabling `URLSessionInstrumentation`. See
@@ -179,7 +192,7 @@ extension RUM {
179192
/// and do not make any assumptions on the thread used to run it.
180193
///
181194
/// Note: This mapper ensures that all views are sent by preventing the return of `nil`. To drop certain automatically
182-
/// collected RUM views, adjust the implementation of the view predicate (see the `uiKitViewsPredicate` option).
195+
/// collected RUM views, adjust the implementation of the view predicate (see the `uiKitViewsPredicate` and `swiftUIPredicate` options).
183196
///
184197
/// Default: `nil`.
185198
public var viewEventMapper: RUM.ViewEventMapper?
@@ -377,8 +390,9 @@ extension RUM.Configuration {
377390
/// - Parameters:
378391
/// - applicationID: The RUM application identifier.
379392
/// - sessionSampleRate: The sampling rate for RUM sessions. Must be a value between `0` and `100`. Default: `100`.
380-
/// - uiKitViewsPredicate: The predicate for automatically tracking `UIViewControllers` as RUM views. Default: `nil`.
393+
/// - uiKitViewsPredicate: The predicate for automatically tracking `UIViewControllers` in `UIKit` as RUM views. Default: `nil`.
381394
/// - uiKitActionsPredicate: The predicate for automatically tracking `UITouch` events as RUM actions. Default: `nil`.
395+
/// - swiftUIViewsPredicate: The predicate for automatically tracking `UIViewControllers` in `SwiftUI` as RUM views. Default: `nil`.
382396
/// - urlSessionTracking: The configuration for automatic RUM resources tracking. Default: `nil`.
383397
/// - trackFrustrations: Determines whether automatic tracking of user frustrations should be enabled. Default: `true`.
384398
/// - trackBackgroundEvents: Determines whether RUM events should be tracked when no view is active. Default: `false`.
@@ -404,6 +418,7 @@ extension RUM.Configuration {
404418
sessionSampleRate: SampleRate = .maxSampleRate,
405419
uiKitViewsPredicate: UIKitRUMViewsPredicate? = nil,
406420
uiKitActionsPredicate: UIKitRUMActionsPredicate? = nil,
421+
swiftUIViewsPredicate: SwiftUIRUMViewsPredicate? = nil,
407422
urlSessionTracking: URLSessionTracking? = nil,
408423
trackFrustrations: Bool = true,
409424
trackBackgroundEvents: Bool = false,
@@ -428,6 +443,7 @@ extension RUM.Configuration {
428443
self.sessionSampleRate = sessionSampleRate
429444
self.uiKitViewsPredicate = uiKitViewsPredicate
430445
self.uiKitActionsPredicate = uiKitActionsPredicate
446+
self.swiftUIViewsPredicate = swiftUIViewsPredicate
431447
self.urlSessionTracking = urlSessionTracking
432448
self.trackFrustrations = trackFrustrations
433449
self.trackBackgroundEvents = trackBackgroundEvents

0 commit comments

Comments
 (0)
Failed to load comments.