-
Notifications
You must be signed in to change notification settings - Fork 121
[Woo POS] Modularization: refactor WaitingTimeTracker to remove Analytics dependency and move it to WooFoundation
#16169
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
Merged
jaclync
merged 2 commits into
trunk
from
feat/WOOMOB-935-refactor-WaitingTimeTracker-and-move-to-WooFoundation
Sep 30, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
Modules/Sources/WooFoundation/Utilities/WaitingTimeTracker.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import Foundation | ||
|
|
||
| /// Tracks the waiting time for a given scenario, allowing to evaluate as analytics | ||
| /// how much time in seconds it took between the init and `end` function call | ||
| /// | ||
| public class WaitingTimeTracker { | ||
| private let trackScenario: WooAnalyticsEvent.WaitingTime.Scenario | ||
| private let currentTimestampSeconds: () -> TimeInterval | ||
| private let waitingStartedTimestamp: TimeInterval | ||
|
|
||
| public enum TrackingUnit { | ||
| case seconds | ||
| case milliseconds | ||
| } | ||
|
|
||
| public init(trackScenario: WooAnalyticsEvent.WaitingTime.Scenario, | ||
| currentTimestampSeconds: @escaping () -> TimeInterval = { Date().timeIntervalSince1970 } | ||
| ) { | ||
| self.trackScenario = trackScenario | ||
| self.currentTimestampSeconds = currentTimestampSeconds | ||
| waitingStartedTimestamp = currentTimestampSeconds() | ||
| } | ||
|
|
||
| /// Default `end()` method to preserve interface compatibility. By default, tracks in `.seconds` | ||
| /// - Returns: The analytics event to be tracked. | ||
| /// | ||
| public func end() -> WooAnalyticsEvent { | ||
| end(using: .seconds) | ||
| } | ||
|
|
||
| /// End the waiting time by evaluating the elapsed time from the init, | ||
| /// and returning an analytics event for tracking. | ||
| /// | ||
| /// - Parameter trackingUnit: Defines whether the elapsed time should be tracked in `.seconds` or `.milliseconds` (default is `.seconds`). | ||
| /// - Returns: The analytics event to be tracked. | ||
| /// | ||
| public func end(using trackingUnit: TrackingUnit = .seconds) -> WooAnalyticsEvent { | ||
| let elapsedTime = calculateElapsedTime(in: trackingUnit) | ||
| return .WaitingTime.waitingFinished(scenario: trackScenario, elapsedTime: elapsedTime) | ||
| } | ||
|
|
||
| /// Calculates elapsed time in the specified tracking unit. | ||
| /// | ||
| private func calculateElapsedTime(in trackingUnit: TrackingUnit) -> TimeInterval { | ||
| let elapsedTime = currentTimestampSeconds() - waitingStartedTimestamp | ||
| return trackingUnit == .milliseconds ? elapsedTime * 1000 : elapsedTime | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Waiting Time measurement | ||
| // | ||
| public extension WooAnalyticsEvent { | ||
| enum WaitingTime { | ||
| /// Possible Waiting time scenarios | ||
| public enum Scenario { | ||
| case orderDetails | ||
| case dashboardTopPerformers | ||
| case dashboardMainStats | ||
| case analyticsHub | ||
| case appStartup | ||
| case pointOfSaleLoaded | ||
| } | ||
|
|
||
| private enum Keys { | ||
| static let waitingTime = "waiting_time" | ||
| static let millisecondsTimeElapsedInSplashScreen = "milliseconds_time_elapsed_in_splash_screen" | ||
| } | ||
|
|
||
| static func waitingFinished(scenario: Scenario, elapsedTime: TimeInterval) -> WooAnalyticsEvent { | ||
| switch scenario { | ||
| case .orderDetails: | ||
| return WooAnalyticsEvent(statName: .orderDetailWaitingTimeLoaded, properties: [Keys.waitingTime: elapsedTime]) | ||
| case .dashboardTopPerformers: | ||
| return WooAnalyticsEvent(statName: .dashboardTopPerformersWaitingTimeLoaded, properties: [Keys.waitingTime: elapsedTime]) | ||
| case .dashboardMainStats: | ||
| return WooAnalyticsEvent(statName: .dashboardMainStatsWaitingTimeLoaded, properties: [Keys.waitingTime: elapsedTime]) | ||
| case .analyticsHub: | ||
| return WooAnalyticsEvent(statName: .analyticsHubWaitingTimeLoaded, properties: [Keys.waitingTime: elapsedTime]) | ||
| case .appStartup: | ||
| return WooAnalyticsEvent(statName: .applicationOpenedWaitingTimeLoaded, properties: [Keys.waitingTime: elapsedTime]) | ||
| case .pointOfSaleLoaded: | ||
| return WooAnalyticsEvent(statName: .pointOfSaleLoaded, properties: [Keys.millisecondsTimeElapsedInSplashScreen: elapsedTime]) | ||
| } | ||
| } | ||
| } | ||
| } | ||
113 changes: 113 additions & 0 deletions
113
Modules/Tests/WooFoundationTests/Utilities/WaitingTimeTrackerTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import XCTest | ||
| @testable import WooFoundation | ||
|
|
||
| /// WaitingTimeTracker Unit Tests | ||
| /// | ||
| final class WaitingTimeTrackerTests: XCTestCase { | ||
| func testTimeElapsedEvaluationIsCorrect() { | ||
| var currentTimeCallCounter = 0.0 | ||
|
|
||
| // Given | ||
| let waitingTracker = WaitingTimeTracker(trackScenario: .orderDetails) { | ||
| currentTimeCallCounter += 1 | ||
| return currentTimeCallCounter * 10 | ||
| } | ||
|
|
||
| // When | ||
| let event = waitingTracker.end() | ||
|
|
||
| // Then | ||
| XCTAssertEqual(event.properties["waiting_time"] as? TimeInterval, 10.0) | ||
| } | ||
|
|
||
| func testOrderDetailsTrackScenarioTriggersExpectedAnalyticsStat() { | ||
| // Given | ||
| let waitingTracker = WaitingTimeTracker(trackScenario: .orderDetails, currentTimestampSeconds: { 0 }) | ||
|
|
||
| // When | ||
| let event = waitingTracker.end() | ||
|
|
||
| // Then | ||
| XCTAssertEqual(event.statName.rawValue, WooAnalyticsStat.orderDetailWaitingTimeLoaded.rawValue) | ||
| } | ||
|
|
||
| func testTopPerformersTrackScenarioTriggersExpectedAnalyticsStat() { | ||
| // Given | ||
| let waitingTracker = WaitingTimeTracker(trackScenario: .dashboardTopPerformers, | ||
| currentTimestampSeconds: { 0 } | ||
| ) | ||
|
|
||
| // When | ||
| let event = waitingTracker.end() | ||
|
|
||
| // Then | ||
| XCTAssertEqual(event.statName.rawValue, WooAnalyticsStat.dashboardTopPerformersWaitingTimeLoaded.rawValue) | ||
| } | ||
|
|
||
| func testMainStatsTrackScenarioTriggersExpectedAnalyticsStat() { | ||
| // Given | ||
| let waitingTracker = WaitingTimeTracker(trackScenario: .dashboardMainStats, | ||
| currentTimestampSeconds: { 0 } | ||
| ) | ||
|
|
||
| // When | ||
| let event = waitingTracker.end() | ||
|
|
||
| // Then | ||
| XCTAssertEqual(event.statName.rawValue, WooAnalyticsStat.dashboardMainStatsWaitingTimeLoaded.rawValue) | ||
| } | ||
|
|
||
| func test_analytics_hub_track_scenario_triggers_expected_analytics_stat() { | ||
| // Given | ||
| let waitingTracker = WaitingTimeTracker(trackScenario: .analyticsHub, | ||
| currentTimestampSeconds: { 0 } | ||
| ) | ||
|
|
||
| // When | ||
| let event = waitingTracker.end() | ||
|
|
||
| // Then | ||
| XCTAssertEqual(event.statName.rawValue, WooAnalyticsStat.analyticsHubWaitingTimeLoaded.rawValue) | ||
| } | ||
|
|
||
| func test_appStartup_track_scenario_triggers_expected_analytics_stat() { | ||
| // Given | ||
| let waitingTracker = WaitingTimeTracker(trackScenario: .appStartup, | ||
| currentTimestampSeconds: { 0 } | ||
| ) | ||
|
|
||
| // When | ||
| let event = waitingTracker.end() | ||
|
|
||
| // Then | ||
| XCTAssertEqual(event.statName.rawValue, WooAnalyticsStat.applicationOpenedWaitingTimeLoaded.rawValue) | ||
| } | ||
|
|
||
| func test_timeElapsed_evaluation_in_milliseconds_is_correct() { | ||
| // Given | ||
| var currentTimeCallCounter = 0.0 | ||
| let expectedReceivedWaitingTime = 10_000.0 // 10s * 1000 ms | ||
| let waitingTracker = WaitingTimeTracker(trackScenario: .orderDetails) { | ||
| currentTimeCallCounter += 1 | ||
| return currentTimeCallCounter * 10 | ||
| } | ||
|
|
||
| // When | ||
| let event = waitingTracker.end(using: .milliseconds) | ||
|
|
||
| // Then | ||
| XCTAssertEqual(event.properties["waiting_time"] as? TimeInterval, expectedReceivedWaitingTime) | ||
| } | ||
|
|
||
| func test_track_scenario_triggers_expected_analytics_stat_in_milliseconds() { | ||
| // Given | ||
| let waitingTracker = WaitingTimeTracker(trackScenario: .pointOfSaleLoaded, | ||
| currentTimestampSeconds: { 0 }) | ||
|
|
||
| // When | ||
| let event = waitingTracker.end(using: .milliseconds) | ||
|
|
||
| // Then | ||
| XCTAssertEqual(event.statName.rawValue, WooAnalyticsStat.pointOfSaleLoaded.rawValue) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's likely not related to this PR, but if you open the Woo app, and after some time change the site, the elapsed time is logged from the app launch up until the site change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested outside this PR, and confirmed the same behavior. It seems to be from the stats and onboarding tasks are always reloaded every app launch, and the completion of these reloads is required for logging the event:
woocommerce-ios/WooCommerce/Classes/Analytics/AppStartupWaitingTimeTracker.swift
Lines 40 to 43 in 83cfd68
I'm guessing this event was for performance measurement (Eagle worked on it) while the stats/onboarding were always reloaded (it was the case before), then the app changed to not always reload the stats/onboarding tasks.
AppStartupWaitingTimeTrackeralso doesn't get reset when switching stores, so the loading time could become inaccurately longer as the starting time was from app launch. But since this is pre-existing behavior, perhaps the team working on it could revisit this performance metric.