Created
April 13, 2016 06:12
-
-
Save syou007/17a5a0b58a9b8166b7309d1024c89346 to your computer and use it in GitHub Desktop.
SwiftでGoogleAnalyticsの設定とスクリーン名自動設定 ref: http://qiita.com/syou007/items/49fdc84be92acae19bb1
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
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool { | |
// GoogleAnalytice | |
GoogleAnalytics.setup() | |
} |
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
// スクリーン名 | |
GoogleAnalytics.trackScreen("ScreenName") | |
// イベント | |
GoogleAnalytics.trackEvent(.User, action: .Show, label: "USER-ID") |
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
class GoogleAnalytics: NSObject { | |
// トラッキングカテゴリー | |
enum Category:String { | |
case User = "User" | |
} | |
// トラッキングアクション | |
enum Action:String { | |
case Show = "Show" | |
case Tap = "Tap" | |
} | |
// セットアップ | |
class func setup() { | |
GAI.sharedInstance().trackUncaughtExceptions = true; | |
# 2分毎にgoogleへデータ送信 | |
GAI.sharedInstance().dispatchInterval = 120 | |
GAI.sharedInstance().logger.logLevel = .Warning //.Verbose | |
GAI.sharedInstance().trackerWithTrackingId("UA-XXXX-XX") | |
} | |
// トラッキングスクリーン | |
class func trackScreen(screenName: String) { | |
let build = GAIDictionaryBuilder.createScreenView().set(screenName, forKey: kGAIScreenName).build() as [NSObject: AnyObject] | |
GAI.sharedInstance().defaultTracker.send(build) | |
} | |
// トラックイベント | |
class func trackEvent(category:Category, action:Action, label:String?, value:NSNumber = 1) { | |
let build = GAIDictionaryBuilder.createEventWithCategory(category.rawValue, action: action.rawValue, label: label, value: value).build() as [NSObject: AnyObject] | |
GAI.sharedInstance().defaultTracker.send(build) | |
} | |
} |
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
pod 'GoogleAnalytics' |
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
extension UIViewController { | |
public override class func initialize() { | |
struct Static { | |
static var token: dispatch_once_t = 0 | |
} | |
// make sure this isn't a subclass | |
if self !== UIViewController.self { | |
return | |
} | |
dispatch_once(&Static.token) { | |
let originalSelector = #selector(UIViewController.viewDidAppear(_:)) | |
let swizzledSelector = #selector(UIViewController.swiftalytics_viewDidAppear(_:)) | |
let originalMethod = class_getInstanceMethod(self, originalSelector) | |
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector) | |
let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)) | |
if didAddMethod { | |
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)) | |
} else { | |
method_exchangeImplementations(originalMethod, swizzledMethod) | |
} | |
} | |
} | |
func swiftalytics_viewDidAppear(animated: Bool) { | |
swiftalytics_viewDidAppear(animated) | |
// 除外するクラスを指定 | |
if !(self is UINavigationController) { | |
// クラス名を取得 | |
var className = NSStringFromClass(self.dynamicType) | |
GoogleAnalytics.trackScreen(className) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment