diff --git a/Example/TVSwiftMeasurementProtocol.xcodeproj/project.pbxproj b/Example/TVSwiftMeasurementProtocol.xcodeproj/project.pbxproj index d5e3c9b..cf78f35 100644 --- a/Example/TVSwiftMeasurementProtocol.xcodeproj/project.pbxproj +++ b/Example/TVSwiftMeasurementProtocol.xcodeproj/project.pbxproj @@ -196,14 +196,16 @@ TargetAttributes = { 63FDDF041C043AA400A6E364 = { CreatedOnToolsVersion = 7.1; - DevelopmentTeam = LRYVNX57AP; + LastSwiftMigration = 0830; }; 63FDDF151C043AA400A6E364 = { CreatedOnToolsVersion = 7.1; + LastSwiftMigration = 0830; TestTargetID = 63FDDF041C043AA400A6E364; }; 63FDDF201C043AA400A6E364 = { CreatedOnToolsVersion = 7.1; + LastSwiftMigration = 0830; TestTargetID = 63FDDF041C043AA400A6E364; }; }; @@ -348,6 +350,7 @@ ONLY_ACTIVE_ARCH = YES; SDKROOT = appletvos; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = 3; TVOS_DEPLOYMENT_TARGET = 9.0; }; @@ -384,6 +387,7 @@ GCC_WARN_UNUSED_VARIABLE = YES; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = appletvos; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = 3; TVOS_DEPLOYMENT_TARGET = 9.0; VALIDATE_PRODUCT = YES; @@ -396,6 +400,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = "App Icon & Top Shelf Image"; ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; CODE_SIGN_IDENTITY = "iPhone Developer"; + DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = TVSwiftMeasurementProtocol/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.analyticspros.TVSwiftMeasurementProtocol; @@ -409,6 +414,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = "App Icon & Top Shelf Image"; ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; CODE_SIGN_IDENTITY = "iPhone Developer"; + DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = TVSwiftMeasurementProtocol/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.analyticspros.TVSwiftMeasurementProtocol; diff --git a/Example/TVSwiftMeasurementProtocol/AppDelegate.swift b/Example/TVSwiftMeasurementProtocol/AppDelegate.swift index 5e4fb6d..0067801 100644 --- a/Example/TVSwiftMeasurementProtocol/AppDelegate.swift +++ b/Example/TVSwiftMeasurementProtocol/AppDelegate.swift @@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { - GATracker.setup("UA-1234567-89") + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { + _ = GATracker.setup("UA-1234567-89") return true } - func applicationWillResignActive(application: UIApplication) { + func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - func applicationDidEnterBackground(application: UIApplication) { + func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - func applicationWillEnterForeground(application: UIApplication) { + func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - func applicationDidBecomeActive(application: UIApplication) { + func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - func applicationWillTerminate(application: UIApplication) { + func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } diff --git a/Example/TVSwiftMeasurementProtocol/GATracker.swift b/Example/TVSwiftMeasurementProtocol/GATracker.swift index f24d5d3..6a972f2 100644 --- a/Example/TVSwiftMeasurementProtocol/GATracker.swift +++ b/Example/TVSwiftMeasurementProtocol/GATracker.swift @@ -8,7 +8,7 @@ import Foundation -private var _analyticsTracker: GATracker! +private var _analyticsTracker: GATracker! = nil class GATracker { /* @@ -20,7 +20,7 @@ class GATracker { @MPVersion = Measurement Protocol version @ua = User Agent string */ - private var tid : String + fileprivate var tid : String var cid: String var appName : String var appVersion : String @@ -29,11 +29,8 @@ class GATracker { var ul : String //Set up singleton object for the tracker - class func setup(tid: String) -> GATracker { - struct Static { - static var onceToken: dispatch_once_t = 0 - } - dispatch_once(&Static.onceToken) { + class func setup(_ tid: String) -> GATracker { + if _analyticsTracker == nil { _analyticsTracker = GATracker(tid: tid) } return _analyticsTracker @@ -58,29 +55,28 @@ class GATracker { #endif self.tid = tid - self.appName = NSBundle.mainBundle().infoDictionary!["CFBundleName"] as! String - let nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] + self.appName = Bundle.main.infoDictionary!["CFBundleName"] as! String + let nsObject: AnyObject? = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as AnyObject self.appVersion = nsObject as! String self.ua = "Mozilla/5.0 (Apple TV; CPU iPhone OS 9_0 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13T534YI" self.MPVersion = "1" - let defaults = NSUserDefaults.standardUserDefaults() - if let cid = defaults.stringForKey("cid") { + let defaults = UserDefaults.standard + if let cid = defaults.string(forKey: "cid") { self.cid = cid } else { - self.cid = NSUUID().UUIDString - defaults.setObject(self.cid, forKey: "cid") + self.cid = UUID().uuidString + defaults.set(self.cid, forKey: "cid") } - let language = NSLocale.preferredLanguages().first - if language?.characters.count > 0 { - self.ul = language! + if let language = Locale.preferredLanguages.first, language.characters.count > 0 { + self.ul = language } else { self.ul = "(not set)" } } - func send(type: String, params: Dictionary) { + func send(_ type: String, params: Dictionary) { /* Generic hit sender to Measurement Protocol Consists out of hit type and a dictionary of other parameters @@ -93,16 +89,16 @@ class GATracker { } //Encoding all the parameters - if let paramEndcode = parameters.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLPathAllowedCharacterSet()){ + if let paramEndcode = parameters.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlPathAllowed){ let urlString = endpoint + paramEndcode; - let url = NSURL(string: urlString); + let url = URL(string: urlString); #if DEBUG print(urlString) #endif - let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) -> Void in - if let httpReponse = response as? NSHTTPURLResponse { + let task = URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) -> Void in + if let httpReponse = response as? HTTPURLResponse { let statusCode = httpReponse.statusCode #if DEBUG print(statusCode) @@ -115,12 +111,12 @@ class GATracker { #endif } } - } + }) task.resume() } } - func screenView(screenName: String, customParameters: Dictionary?) { + func screenView(_ screenName: String, customParameters: Dictionary?) { /* A screenview hit, use screenname */ @@ -133,7 +129,8 @@ class GATracker { self.send("screenview", params: params) } - func event(category: String, action: String, var label: String?, customParameters: Dictionary?) { + func event(_ category: String, action: String, label: String?, customParameters: Dictionary?) { + var label = label /* An event hit with category, action, label */ @@ -151,7 +148,7 @@ class GATracker { self.send("event", params: params) } - func exception(description: String, isFatal:Bool, customParameters: Dictionary?) { + func exception(_ description: String, isFatal:Bool, customParameters: Dictionary?) { /* An exception hit with exception description (exd) and "fatality" (Crashed or not) (exf) */ @@ -169,4 +166,4 @@ class GATracker { self.send("exception", params: params) } -} \ No newline at end of file +} diff --git a/Example/TVSwiftMeasurementProtocol/SecondViewController.swift b/Example/TVSwiftMeasurementProtocol/SecondViewController.swift index 945f4b9..73d8f8b 100644 --- a/Example/TVSwiftMeasurementProtocol/SecondViewController.swift +++ b/Example/TVSwiftMeasurementProtocol/SecondViewController.swift @@ -15,12 +15,12 @@ class SecondViewController: UIViewController { } - override func viewDidAppear(animated: Bool) { + override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) GATracker.sharedInstance.screenView("SecondScreen", customParameters: ["cd1": "Seattle", "cm2": "2"]) } - @IBAction func exceptionFIred(sender: AnyObject) { + @IBAction func exceptionFIred(_ sender: AnyObject) { print("Exception Fired") GATracker.sharedInstance.send("transaction", params: ["tid":"10001", "tr":"425,00", "cu":"USD"]) GATracker.sharedInstance.exception("This test failed", isFatal: true, customParameters: nil) diff --git a/Example/TVSwiftMeasurementProtocol/ViewController.swift b/Example/TVSwiftMeasurementProtocol/ViewController.swift index de64cb8..2bb887a 100644 --- a/Example/TVSwiftMeasurementProtocol/ViewController.swift +++ b/Example/TVSwiftMeasurementProtocol/ViewController.swift @@ -14,18 +14,18 @@ class ViewController: UIViewController { } - override func viewDidAppear(animated: Bool) { + override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) GATracker.sharedInstance.screenView("FirstScreen", customParameters: nil) } - @IBAction func fireEvent(sender: AnyObject) { + @IBAction func fireEvent(_ sender: AnyObject) { print("Fire Event") GATracker.sharedInstance.event("a", action: "b", label: nil, customParameters: nil) } - @IBAction func nextScreenPressed(sender: AnyObject) { - self.performSegueWithIdentifier("toSecondViewController", sender: nil) + @IBAction func nextScreenPressed(_ sender: AnyObject) { + self.performSegue(withIdentifier: "toSecondViewController", sender: nil) } } diff --git a/Example/TVSwiftMeasurementProtocolTests/TVSwiftMeasurementProtocolTests.swift b/Example/TVSwiftMeasurementProtocolTests/TVSwiftMeasurementProtocolTests.swift index a174680..240d83c 100644 --- a/Example/TVSwiftMeasurementProtocolTests/TVSwiftMeasurementProtocolTests.swift +++ b/Example/TVSwiftMeasurementProtocolTests/TVSwiftMeasurementProtocolTests.swift @@ -28,7 +28,7 @@ class TVSwiftMeasurementProtocolTests: XCTestCase { func testPerformanceExample() { // This is an example of a performance test case. - self.measureBlock { + self.measure { // Put the code you want to measure the time of here. } } diff --git a/GATracker.swift b/GATracker.swift index a5f2feb..6a972f2 100644 --- a/GATracker.swift +++ b/GATracker.swift @@ -8,7 +8,7 @@ import Foundation -private var _analyticsTracker: GATracker! +private var _analyticsTracker: GATracker! = nil class GATracker { /* @@ -20,7 +20,7 @@ class GATracker { @MPVersion = Measurement Protocol version @ua = User Agent string */ - private var tid : String + fileprivate var tid : String var cid: String var appName : String var appVersion : String @@ -29,11 +29,8 @@ class GATracker { var ul : String //Set up singleton object for the tracker - class func setup(tid: String) -> GATracker { - struct Static { - static var onceToken: dispatch_once_t = 0 - } - dispatch_once(&Static.onceToken) { + class func setup(_ tid: String) -> GATracker { + if _analyticsTracker == nil { _analyticsTracker = GATracker(tid: tid) } return _analyticsTracker @@ -58,29 +55,28 @@ class GATracker { #endif self.tid = tid - self.appName = NSBundle.mainBundle().infoDictionary!["CFBundleName"] as! String - let nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] + self.appName = Bundle.main.infoDictionary!["CFBundleName"] as! String + let nsObject: AnyObject? = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as AnyObject self.appVersion = nsObject as! String self.ua = "Mozilla/5.0 (Apple TV; CPU iPhone OS 9_0 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13T534YI" self.MPVersion = "1" - let defaults = NSUserDefaults.standardUserDefaults() - if let cid = defaults.stringForKey("cid") { + let defaults = UserDefaults.standard + if let cid = defaults.string(forKey: "cid") { self.cid = cid } else { - self.cid = NSUUID().UUIDString - defaults.setObject(self.cid, forKey: "cid") + self.cid = UUID().uuidString + defaults.set(self.cid, forKey: "cid") } - let language = NSLocale.preferredLanguages().first - if language?.characters.count > 0 { - self.ul = language! + if let language = Locale.preferredLanguages.first, language.characters.count > 0 { + self.ul = language } else { self.ul = "(not set)" } } - func send(type: String, params: Dictionary) { + func send(_ type: String, params: Dictionary) { /* Generic hit sender to Measurement Protocol Consists out of hit type and a dictionary of other parameters @@ -93,16 +89,16 @@ class GATracker { } //Encoding all the parameters - if let paramEndcode = parameters.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLPathAllowedCharacterSet()){ + if let paramEndcode = parameters.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlPathAllowed){ let urlString = endpoint + paramEndcode; - let url = NSURL(string: urlString); + let url = URL(string: urlString); #if DEBUG print(urlString) #endif - let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) -> Void in - if let httpReponse = response as? NSHTTPURLResponse { + let task = URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) -> Void in + if let httpReponse = response as? HTTPURLResponse { let statusCode = httpReponse.statusCode #if DEBUG print(statusCode) @@ -115,12 +111,12 @@ class GATracker { #endif } } - } + }) task.resume() } } - func screenView(screenName: String, customParameters: Dictionary?) { + func screenView(_ screenName: String, customParameters: Dictionary?) { /* A screenview hit, use screenname */ @@ -133,13 +129,17 @@ class GATracker { self.send("screenview", params: params) } - func event(category: String, action: String, label: String?, customParameters: Dictionary?) { + func event(_ category: String, action: String, label: String?, customParameters: Dictionary?) { + var label = label /* An event hit with category, action, label */ - + if label == nil { + label = "" + } + //event parameters category, action and label - var params = ["ec" : category, "ea" : action, "el" : label ?? ""] + var params = ["ec" : category, "ea" : action, "el" : label!] if (customParameters != nil) { for (key, value) in customParameters! { params.updateValue(value, forKey: key) @@ -148,7 +148,7 @@ class GATracker { self.send("event", params: params) } - func exception(description: String, isFatal:Bool, customParameters: Dictionary?) { + func exception(_ description: String, isFatal:Bool, customParameters: Dictionary?) { /* An exception hit with exception description (exd) and "fatality" (Crashed or not) (exf) */