-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple behavior monitoring to better address potential breakage i…
…ssues (#2521)
- Loading branch information
Showing
14 changed files
with
398 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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,32 @@ | ||
// | ||
// UserBehaviorEvent.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
public enum UserBehaviorEvent { | ||
|
||
case reloadTwice | ||
case reloadAndRestart | ||
case reloadAndFireButton | ||
case reloadAndOpenSettings | ||
case reloadAndTogglePrivacyControls | ||
case fireButtonAndRestart | ||
case fireButtonAndTogglePrivacyControls | ||
|
||
} |
This file contains 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,115 @@ | ||
// | ||
// UserBehaviorMonitor.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import Common | ||
import Core | ||
|
||
protocol UserBehaviorStoring { | ||
|
||
var didRefreshTimestamp: Date? { get set } | ||
var didBurnTimestamp: Date? { get set } | ||
|
||
} | ||
|
||
final class UserBehaviorStore: UserBehaviorStoring { | ||
|
||
@UserDefaultsWrapper(key: .didRefreshTimestamp, defaultValue: .distantPast) | ||
var didRefreshTimestamp: Date? | ||
|
||
@UserDefaultsWrapper(key: .didBurnTimestamp, defaultValue: .distantPast) | ||
var didBurnTimestamp: Date? | ||
|
||
} | ||
|
||
final class UserBehaviorMonitor { | ||
|
||
enum Action: Equatable { | ||
|
||
case refresh | ||
case burn | ||
case reopenApp | ||
case openSettings | ||
case toggleProtections | ||
|
||
} | ||
|
||
private let eventMapping: EventMapping<UserBehaviorEvent> | ||
private var store: UserBehaviorStoring | ||
|
||
init(eventMapping: EventMapping<UserBehaviorEvent> = AppUserBehaviorMonitor.eventMapping, | ||
store: UserBehaviorStoring = UserBehaviorStore()) { | ||
self.eventMapping = eventMapping | ||
self.store = store | ||
} | ||
|
||
var didRefreshTimestamp: Date? { | ||
get { store.didRefreshTimestamp } | ||
set { store.didRefreshTimestamp = newValue } | ||
} | ||
|
||
var didBurnTimestamp: Date? { | ||
get { store.didBurnTimestamp } | ||
set { store.didBurnTimestamp = newValue } | ||
} | ||
|
||
func handleAction(_ action: Action, date: Date = Date()) { | ||
switch action { | ||
case .refresh: | ||
fireEventIfActionOccurredRecently(since: didRefreshTimestamp, eventToFire: .reloadTwice, within: 10.0) | ||
didRefreshTimestamp = date | ||
case .burn: | ||
fireEventIfActionOccurredRecently(since: didRefreshTimestamp, eventToFire: .reloadAndFireButton) | ||
didBurnTimestamp = date | ||
case .reopenApp: | ||
fireEventIfActionOccurredRecently(since: didRefreshTimestamp, eventToFire: .reloadAndRestart) | ||
fireEventIfActionOccurredRecently(since: didBurnTimestamp, eventToFire: .fireButtonAndRestart) | ||
case .openSettings: | ||
fireEventIfActionOccurredRecently(since: didRefreshTimestamp, eventToFire: .reloadAndOpenSettings) | ||
case .toggleProtections: | ||
fireEventIfActionOccurredRecently(since: didRefreshTimestamp, eventToFire: .reloadAndTogglePrivacyControls) | ||
fireEventIfActionOccurredRecently(since: didBurnTimestamp, eventToFire: .fireButtonAndTogglePrivacyControls) | ||
} | ||
|
||
func fireEventIfActionOccurredRecently(since timestamp: Date?, eventToFire: UserBehaviorEvent, within interval: Double = 30.0) { | ||
if let timestamp = timestamp, date.timeIntervalSince(timestamp) < interval { | ||
eventMapping.fire(eventToFire) | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
final class AppUserBehaviorMonitor { | ||
|
||
static let eventMapping = EventMapping<UserBehaviorEvent> { event, _, _, _ in | ||
let domainEvent: Pixel.Event | ||
switch event { | ||
case .reloadTwice: domainEvent = .userBehaviorReloadTwice | ||
case .reloadAndRestart: domainEvent = .userBehaviorReloadAndRestart | ||
case .reloadAndFireButton: domainEvent = .userBehaviorReloadAndFireButton | ||
case .reloadAndOpenSettings: domainEvent = .userBehaviorReloadAndOpenSettings | ||
case .reloadAndTogglePrivacyControls: domainEvent = .userBehaviorReloadAndTogglePrivacyControls | ||
case .fireButtonAndRestart: domainEvent = .userBehaviorFireButtonAndRestart | ||
case .fireButtonAndTogglePrivacyControls: domainEvent = .userBehaviorFireButtonAndTogglePrivacyControls | ||
} | ||
Pixel.fire(pixel: domainEvent) | ||
} | ||
|
||
} |
This file contains 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 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.