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
21 changes: 21 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 = "ab-testing-enrolled-experiments"

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

Expand All @@ -23,6 +24,12 @@ 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 @@ -81,6 +88,9 @@ import Cocoa
ttlDate.addTimeInterval(TimeInterval(assignments.ttl))
UserDefaults.standard.setValue(ttlDate, forKey: self.ttlDateKey)

let enrolledExperimentNames = assignments.variations.map({ $0.key })
UserDefaults.standard.setValue(enrolledExperimentNames, forKey: self.enrolledKey)

completion?()
}
}
Expand All @@ -101,6 +111,17 @@ import Cocoa
}
}

/// 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(in name: String) -> Bool {
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