Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RUMM-2354 Report Frustration Count in View #1009

Merged
merged 1 commit into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class RUMUserActionScope: RUMScope, RUMContextProvider {
private var activeResourcesCount: Int = 0

/// Callback called when a `RUMActionEvent` is submitted for storage.
private let onActionEventSent: () -> Void
private let onActionEventSent: (RUMActionEvent) -> Void

init(
parent: RUMContextProvider,
Expand All @@ -67,7 +67,7 @@ internal class RUMUserActionScope: RUMScope, RUMContextProvider {
startTime: Date,
serverTimeOffset: TimeInterval,
isContinuous: Bool,
onActionEventSent: @escaping () -> Void
onActionEventSent: @escaping (RUMActionEvent) -> Void
) {
self.parent = parent
self.dependencies = dependencies
Expand Down Expand Up @@ -184,7 +184,7 @@ internal class RUMUserActionScope: RUMScope, RUMContextProvider {

if let event = dependencies.eventBuilder.build(from: actionEvent) {
writer.write(value: event)
onActionEventSent()
onActionEventSent(event)
}
}

Expand Down
20 changes: 13 additions & 7 deletions Sources/Datadog/RUM/RUMMonitor/Scopes/RUMViewScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ internal class RUMViewScope: RUMScope, RUMContextProvider {
private var longTasksCount: Int64 = 0
/// Number of Frozen Frames tracked by this View.
private var frozenFramesCount: Int64 = 0
/// Number of Frustration tracked by this View.
private var frustrationCount: Int64 = 0

/// Current version of this View to use for RUM `documentVersion`.
private var version: UInt = 0
Expand Down Expand Up @@ -256,9 +258,8 @@ internal class RUMViewScope: RUMScope, RUMContextProvider {
startTime: command.time,
serverTimeOffset: serverTimeOffset,
isContinuous: true,
onActionEventSent: { [weak self] in
self?.actionsCount += 1
self?.needsViewUpdate = true
onActionEventSent: { [weak self] event in
self?.onActionEventSent(event)
}
)
}
Expand All @@ -273,13 +274,18 @@ internal class RUMViewScope: RUMScope, RUMContextProvider {
startTime: command.time,
serverTimeOffset: serverTimeOffset,
isContinuous: false,
onActionEventSent: { [weak self] in
self?.actionsCount += 1
self?.needsViewUpdate = true
onActionEventSent: { [weak self] event in
self?.onActionEventSent(event)
}
)
}

private func onActionEventSent(_ event: RUMActionEvent) {
actionsCount += 1
frustrationCount += event.action.frustration?.type.count.toInt64 ?? 0
needsViewUpdate = true
}

private func addDiscreteUserAction(on command: RUMAddUserActionCommand) {
userActionScope = createDiscreteUserActionScope(on: command)
}
Expand Down Expand Up @@ -429,7 +435,7 @@ internal class RUMViewScope: RUMScope, RUMContextProvider {
firstInputDelay: nil,
firstInputTime: nil,
frozenFrame: .init(count: frozenFramesCount),
frustration: nil,
frustration: .init(count: frustrationCount),
id: viewUUID.toRUMDataFormat,
inForegroundPeriods: nil,
isActive: isActive,
Expand Down
2 changes: 1 addition & 1 deletion Tests/DatadogTests/Datadog/Mocks/RUMFeatureMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ extension RUMUserActionScope {
startTime: Date = .mockAny(),
serverTimeOffset: TimeInterval = .zero,
isContinuous: Bool = .mockAny(),
onActionEventSent: @escaping () -> Void = {}
onActionEventSent: @escaping (RUMActionEvent) -> Void = { _ in }
) -> RUMUserActionScope {
return RUMUserActionScope(
parent: parent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ class RUMUserActionScopeTests: XCTestCase {
actionType: .tap,
startTime: currentTime,
isContinuous: false,
onActionEventSent: {
onActionEventSent: { _ in
callbackCalled = true
}
)
Expand Down Expand Up @@ -555,7 +555,7 @@ class RUMUserActionScopeTests: XCTestCase {
actionType: .tap,
startTime: currentTime,
isContinuous: false,
onActionEventSent: {
onActionEventSent: { _ in
callbackCalled = true
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,64 @@ class RUMViewScopeTests: XCTestCase {
XCTAssertEqual(firstActionEvent.os?.name, "device-os")
}

func testWhenDiscreteUserActionHasFrustration_itSendsFrustrationCount() throws {
// Given
var currentTime = Date()
let scope = RUMViewScope(
isInitialView: false,
parent: parent,
dependencies: .mockAny(),
identity: mockView,
path: .mockAny(),
name: .mockAny(),
attributes: [:],
customTimings: [:],
startTime: currentTime,
serverTimeOffset: .zero
)

XCTAssertTrue(
scope.process(
command: RUMStartViewCommand.mockWith(time: currentTime, identity: mockView),
context: context,
writer: writer
)
)

// When
(0..<5).forEach { i in
XCTAssertTrue(
scope.process(
command: RUMAddUserActionCommand.mockWith(time: currentTime, actionType: .tap),
context: context,
writer: writer
)
)

XCTAssertTrue(
scope.process(
command: RUMAddCurrentViewErrorCommand.mockRandom(),
context: context,
writer: writer
)
)

currentTime.addTimeInterval(RUMUserActionScope.Constants.discreteActionTimeoutDuration)
}

XCTAssertFalse(
scope.process(
command: RUMStopViewCommand.mockWith(time: currentTime, identity: mockView),
context: context,
writer: writer
)
)

// Then
let event = try XCTUnwrap(writer.events(ofType: RUMViewEvent.self).last)
XCTAssertEqual(event.view.frustration?.count, 5)
}

// MARK: - Error Tracking

func testWhenViewErrorIsAdded_itSendsErrorEventAndViewUpdateEvent() throws {
Expand Down