-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
experiment default metrics pixels (#1107)
Task/Issue URL: https://app.asana.com/0/1204186595873227/1208775176602791/f iOS PR: in prep macOS PR: duckduckgo/macos-browser#3622 What kind of version bump will this require?: N/A Tech Design URL: https://app.asana.com/0/1204186595873227/1208682592686299 **Description**: Implements PixelExperimentKit to provide Experiment metrics API to clients
- Loading branch information
1 parent
f0755fb
commit e5d390c
Showing
14 changed files
with
1,238 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// ExperimentEventTracker.swift | ||
// | ||
// 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 typealias ThresholdCheckResult = Bool | ||
public typealias ExprimentPixelNameAndParameters = String | ||
public typealias NumberOfActions = Int | ||
|
||
public protocol ExperimentActionPixelStore { | ||
func removeObject(forKey defaultName: String) | ||
func integer(forKey defaultName: String) -> Int | ||
func set(_ value: Int, forKey defaultName: String) | ||
} | ||
|
||
public protocol ExperimentEventTracking { | ||
/// Increments the count for a given event key and checks if the threshold has been exceeded. | ||
/// | ||
/// This method performs the following actions: | ||
/// 1. If the `isInWindow` parameter is `false`, it removes the stored count for the key and returns `false`. | ||
/// 2. If `isInWindow` is `true`, it increments the count for the key. | ||
/// 3. If the updated count meets or exceeds the specified `threshold`, the stored count is removed, and the method returns `true`. | ||
/// 4. If the updated count does not meet the threshold, it updates the count and returns `false`. | ||
/// | ||
/// - Parameters: | ||
/// - key: The key used to store and retrieve the count. | ||
/// - threshold: The count threshold that triggers a return of `true`. | ||
/// - isInWindow: A flag indicating if the count should be considered (e.g., within a time window). | ||
/// - Returns: `true` if the threshold is exceeded and the count is reset, otherwise `false`. | ||
func incrementAndCheckThreshold(forKey key: ExprimentPixelNameAndParameters, threshold: NumberOfActions, isInWindow: Bool) -> ThresholdCheckResult | ||
} | ||
|
||
public struct ExperimentEventTracker: ExperimentEventTracking { | ||
private let store: ExperimentActionPixelStore | ||
private let syncQueue = DispatchQueue(label: "com.pixelkit.experimentActionSyncQueue") | ||
|
||
public init(store: ExperimentActionPixelStore = UserDefaults.standard) { | ||
self.store = store | ||
} | ||
|
||
public func incrementAndCheckThreshold(forKey key: ExprimentPixelNameAndParameters, threshold: NumberOfActions, isInWindow: Bool) -> ThresholdCheckResult { | ||
syncQueue.sync { | ||
// Remove the key if is not in window | ||
guard isInWindow else { | ||
store.removeObject(forKey: key) | ||
return false | ||
} | ||
|
||
// Increment the current count | ||
let currentCount = store.integer(forKey: key) | ||
let newCount = currentCount + 1 | ||
store.set(newCount, forKey: key) | ||
|
||
// Check if the threshold is exceeded | ||
if newCount >= threshold { | ||
store.removeObject(forKey: key) | ||
return true | ||
} | ||
return false | ||
} | ||
} | ||
|
||
} | ||
|
||
extension UserDefaults: ExperimentActionPixelStore {} |
Oops, something went wrong.