Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using GA from base controller to automatically create screen views
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])
}
}
import UIKit
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Analytics().screenView(self.controllerName)
}
}
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