Created
October 21, 2015 09:07
-
-
Save samuelbeek/594fb89d2331ccaadc17 to your computer and use it in GitHub Desktop.
Google Analytics Helper (Work In Progress)
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
// | |
// Analytics.swift | |
// Instant | |
// | |
// Created by Samuel Beek on 21/10/15. | |
// Copyright © 2015 Samue Beek. All rights reserved. | |
// | |
struct Analytics { | |
enum ActionType { | |
case ui, share, process | |
var description : String { | |
switch self { | |
case .ui: return "uiAction" | |
case .share: return "shareAction" | |
case .process: return "processAction" | |
} | |
} | |
} | |
/// Configures Google Analytics | |
static func configure() { | |
// Configure tracker from GoogleService-Info.plist. | |
var configureError:NSError? | |
GGLContext.sharedInstance().configureWithError(&configureError) | |
assert(configureError == nil, "Error configuring Google services: \(configureError)") | |
// Optional: configure GAI options. | |
let gai = GAI.sharedInstance() | |
gai.trackUncaughtExceptions = true // report uncaught exceptions | |
gai.logger.logLevel = GAILogLevel.Verbose // remove before app release | |
} | |
static func screen(name: String) { | |
let tracker = GAI.sharedInstance().defaultTracker | |
tracker.set(kGAIScreenName, value: name) | |
let builder = GAIDictionaryBuilder.createScreenView() | |
tracker.send(builder.build() as [NSObject : AnyObject]) | |
} | |
static func event(category: ActionType, action: String, label: String = "", value: NSNumber = 0) { | |
let tracker = GAI.sharedInstance().defaultTracker | |
tracker.send(GAIDictionaryBuilder.createEventWithCategory(category.description, action: action, label: label, value: value == 0 ? nil : value).build() as [NSObject : AnyObject]) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment