Skip to content

Instantly share code, notes, and snippets.

@ohad7
Last active August 22, 2019 11:49
Show Gist options
  • Save ohad7/8ce12a432773fe3b1bf3 to your computer and use it in GitHub Desktop.
Save ohad7/8ce12a432773fe3b1bf3 to your computer and use it in GitHub Desktop.
Mixpanel A/B Testing in Swift
//
// Copyright (c) 2015 Nexar Inc. - All Rights Reserved. Proprietary and confidential.
//
// Unauthorized copying of this file, via any medium is strictly prohibited.
import Foundation
import Mixpanel
public class Tweaks : NSObject, MPTweakObserver {
public static let sharedInstance = Tweaks(tweaks: [ ExecuteDiskCleanup])
public static let ExecuteDiskCleanup = Tweak.Booln(name: "Cleanup-Disk", defaultValue:false)
private var values = [String:AnyObject]()
private let tweaks: [Tweak]
public init(tweaks: [Tweak]) {
self.tweaks = tweaks
let defaults = NSUserDefaults.standardUserDefaults()
for tweak in tweaks {
if let value: AnyObject = defaults.objectForKey(Tweaks.generateKey(tweak.name)) {
self.values[tweak.name] = value
println("Initialized Tweak \(tweak.name) with value: \(value)")
}
}
}
public func setup() {
tweaks.foreach(){ tweak in
let theTweak = MPTweak(name: tweak.name, andEncoding: "UTF-8")
theTweak.defaultValue = tweak.defaultValue
theTweak.addObserver(self)
MPTweakStore.sharedInstance().addTweak(theTweak)
}
Mixpanel.sharedInstance().checkForVariantsOnActive = true
Mixpanel.sharedInstance().joinExperiments()
}
public func get(tweaks: [Tweak]) -> [String:AnyObject] {
var result = [String:AnyObject]()
synced(values) {
tweaks.foreach(){ tweak in
result[tweak.name] = self.values[tweak.name]
}
}
return result
}
public func tweakDidChange(tweak: MPTweak) {
println("tweakDidChange Tweak \(tweak.name) current value :\(tweak.currentValue)")
synced(self.values) {
let localTweak = self.tweaks.filter{ $0.name == tweak.name}.first
if let localTweak = localTweak{
switch (localTweak) {
case .Booln:
self.values[tweak.name] = Bool(tweak.currentValue as? Int == 1)
default:
self.values[tweak.name] = tweak.currentValue
}
println("Tweak \(localTweak.name) -> \(self.values[localTweak.name])")
NSUserDefaults.standardUserDefaults().setObject(self.values[localTweak.name], forKey: Tweaks.generateKey(localTweak.name))
NSUserDefaults.standardUserDefaults().synchronize()
}
}
}
private class func generateKey(key: String) -> String {
return "mp_tweak_\(key)"
}
}
public enum Tweak {
case Str(name: String, defaultValue: String)
case Booln(name: String, defaultValue: Bool)
case Integer(name: String, defaultValue: Int)
case Flt(name: String, defaultValue: Float)
var name : String {
switch (self) {
case let .Str(n, d): return n
case let .Booln(n, d): return n
case let .Integer(n, d): return n
case let .Flt(n, d): return n
}
}
var defaultValue : AnyObject {
switch (self) {
case let .Str(n, d): return d
case let .Booln(n, d): return d
case let .Integer(n, d): return d
case let .Flt(n, d): return d
}
}
public func get<T>() -> T? {
return get(self.defaultValue as? T)
}
public func get<T>(defaultValue: T?) -> T? {
var value = synced(Tweaks.sharedInstance.values){
return Tweaks.sharedInstance.values[self.name] as? T
}
return value != nil ? value : defaultValue
}
}
// Examples :
// public static let SomeIntegerZero = Tweak.Integer(name: "SomeIntegerZero", defaultValue:0)
// public static let SomeIntegerNonZero = Tweak.Integer(name: "SomeIntegerNonZero", defaultValue:6666)
// public static let SomeBooleanFalse = Tweak.Booln(name: "SomeBooleanFalse", defaultValue:false)
// public static let SomeBooleanTrue = Tweak.Booln(name: "SomeBooleanTrue", defaultValue:true)
// public static let SomeStringEmpty = Tweak.Str(name: "SomeEmptyString", defaultValue:"")
// public static let SomeStringFull = Tweak.Str(name: "SomeFullString", defaultValue:"full")
/*** Utilities - sync methods ***/
func synced<T>(lock: AnyObject, closure: () -> T) -> T {
objc_sync_enter(lock)
let result = closure()
objc_sync_exit(lock)
return result
}
func synced(lock: AnyObject, closure: () -> ()) {
objc_sync_enter(lock)
closure()
objc_sync_exit(lock)
}
@xManson
Copy link

xManson commented Nov 25, 2016

great ,  but how to use in my func?

func foo() {
 Tweaks.setup()
  let tw =  Tweaks.ExecuteDiskCleanup
 }

or other way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment