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

Store and confirm what experiments a user is enrolled in #208

Merged
merged 9 commits into from
Aug 2, 2022
23 changes: 23 additions & 0 deletions Sources/Experiments/ExPlat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Cocoa

private let assignmentsKey = "ab-testing-assignments"
private let ttlDateKey = "ab-testing-ttl-date"
private let enrolledKey = "enrolled-experiments"
charliescheer marked this conversation as resolved.
Show resolved Hide resolved

private(set) var experimentNames: [String] = []

Expand All @@ -23,6 +24,13 @@ import Cocoa
return ttlDate.timeIntervalSinceReferenceDate - Date().timeIntervalSinceReferenceDate
}

/// Check to see if ExPlat has stored the names of enrolled experiments
///
public var enrolledArrayExists: Bool {
UserDefaults.standard.object(forKey: enrolledKey) != nil
}


public init(configuration: ExPlatConfiguration,
service: ExPlatService? = nil) {
self.service = service ?? ExPlatService(configuration: configuration)
Expand Down Expand Up @@ -80,6 +88,9 @@ import Cocoa
var ttlDate = Date()
ttlDate.addTimeInterval(TimeInterval(assignments.ttl))
UserDefaults.standard.setValue(ttlDate, forKey: self.ttlDateKey)

let enrolledExperimentNames = assignments.variations.map({ String($0.key) })
charliescheer marked this conversation as resolved.
Show resolved Hide resolved
UserDefaults.standard.setValue(enrolledExperimentNames, forKey: self.enrolledKey)

completion?()
}
Expand All @@ -100,6 +111,18 @@ import Cocoa
return .treatment(variation)
}
}

/// Checks if the experiment name is contained in the enrolled experiments array
/// returns false if there is no dictionary or the experiment is not registered
///
public func isEnrolled(_ name: String) -> Bool {
charliescheer marked this conversation as resolved.
Show resolved Hide resolved
guard let enrolled = UserDefaults.standard.object(forKey: enrolledKey) as? [String] else {
return false
}

return enrolled.contains(name)
}


/// Check if the app is entering background and/or foreground
/// and start/stop the timers
Expand Down