Skip to content

Instantly share code, notes, and snippets.

@AaronPresley
Last active August 29, 2015 14:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AaronPresley/b2c557ad25343875b72b to your computer and use it in GitHub Desktop.
Save AaronPresley/b2c557ad25343875b72b to your computer and use it in GitHub Desktop.
// This script has been updated from Sam Soffes'
// original version located at
// https://gist.github.com/soffes/dda0f842d1aa0547293e
import Foundation
import UIKit
struct Mixpanel {
// MARK: - Types
typealias Completion = (success: Bool) -> ()
// MARK: - Properties
private var token: String
private var URLSession: NSURLSession
private let endpoint = "http://api.mixpanel.com/track/"
// MARK: - Initializers
init(token: String, URLSession: NSURLSession = NSURLSession.sharedSession()) {
self.token = token
self.URLSession = URLSession
}
// MARK: - Events
func track(event: String, parameters: [String: AnyObject]? = nil, time: NSDate = NSDate(), completion: Completion? = nil) {
var properties: [String: AnyObject] = parameters ?? [String: AnyObject]()
properties["token"] = token
properties["time"] = time.timeIntervalSince1970
properties["$os"] = "iPhone OS"
properties["$manufacturer"] = "Apple"
properties["$os_version"] = UIDevice.currentDevice().systemVersion;
properties["$model"] = UIDevice.currentDevice().modelName
properties["$app_version"] = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as? String
properties["$app_release"] = NSBundle.mainBundle().infoDictionary?["CFBundleVersion"] as? String
let json = [
"event": event,
"properties": properties
]
if let jsonData = NSJSONSerialization.dataWithJSONObject(json, options: nil, error: nil) {
let data = jsonData.base64EncodedStringWithOptions(nil).stringByReplacingOccurrencesOfString("\n", withString: "")
if let url = NSURL(string: "\(endpoint)?data=\(data)") {
let request = NSURLRequest(URL: url)
let task = URLSession.dataTaskWithRequest(request, completionHandler: { data, response, error in
if let completion = completion, string = NSString(data: data, encoding: NSUTF8StringEncoding) {
completion(success: string == "1")
}
})
task.resume()
}
}
}
}
let mpToken = "YOUR-TOKEN"
let mixpanel = Mixpanel(token: mpToken)
@ohad7
Copy link

ohad7 commented Jun 27, 2015

If anyone's interested in doing a/b testing in swift, here's an example :
https://gist.github.com/ohad7/8ce12a432773fe3b1bf3

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