Skip to content

Instantly share code, notes, and snippets.

@samuelbeek
Created October 21, 2015 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuelbeek/594fb89d2331ccaadc17 to your computer and use it in GitHub Desktop.
Save samuelbeek/594fb89d2331ccaadc17 to your computer and use it in GitHub Desktop.
Google Analytics Helper (Work In Progress)
//
// 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