Using GA from base controller to automatically create screen views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Google | |
public struct Analytics { | |
enum GAToken: String { | |
case QAKey = "UPDATE-WITH-QA-KEY" | |
case ProdKey = "UPDATE-WITH-PROD-KEY" | |
} | |
public static func createTracker(key: String) { | |
GAI.sharedInstance().trackerWithTrackingId(key) | |
GAI.sharedInstance().logger.logLevel = GAILogLevel.None | |
GAI.sharedInstance().trackUncaughtExceptions = true | |
} | |
private func getTracker() -> GAITracker { | |
return GAI.sharedInstance().defaultTracker | |
} | |
public func screenView(name: String) { | |
let tracker = getTracker() | |
tracker.set(kGAIScreenName, value: name) | |
let builder = GAIDictionaryBuilder.createScreenView() | |
tracker.send(builder.build() as [NSObject : AnyObject]) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class BaseViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
Analytics().screenView(self.controllerName) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIViewController { | |
var controllerName: String { | |
return NSStringFromClass(self.classForCoder).componentsSeparatedByString(".").last! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment