EventAnalytics 0.1.0

EventAnalytics 0.1.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jun 2017
SwiftSwift Version 3.1
SPMSupports SPM

Maintained by Suyeol Jeon.


Downloads

Total38
Week0
Month38

Installs

Apps1
Test Targets1
powered by Segment

GitHub

Stars176
Watchers3
Forks4
Issues4
Contributors1
Pull Requests0

Code

Files1
LOCLines of Code 22


  • By
  • Suyeol Jeon

Analytics abstraction layer for Swift. Inspired by Moya.

Why?

There are many tools for mobile app analytics such as Firebase, Google Analytics, Fabric Answers, Flurry, Mixpanel, etc. You might use one or more of those in your application. But most of those SDKs have some problems: if you use multiple analytics tools, your code will be messed up. And the SDKs take event name as a string and parameters as a dictionary which is not guaranteed by Swift compiler. It means that if you change the event definition, you should find all related code by your hand. It has an opportunity that cause a human error. EventAnalytics uses Swift enums and the associated values to solve these problems.

Features

  • 💪 Taking advantages of Swift compiler by using an enum and associated values.
  • 🎯 Logging events to multiple analytics providers at once.
  • 🎨 Creating custom analytics providers.

At a Glance

Before 🤢

FIRAnalytics.logEvent(withName: kFIREventEcommercePurchase, parameters: [
  kFIRParameterCurrency: "USD" as NSObject,
  kFIRParameterValue: 9.99 as NSNumber,
  kFIRParameterTransactionID: "20170709123456" as NSObject,
])
Flurry.logEvent("purchase", withParameters: [
  "Currency": "USD",
  "Price": 9.99,
  "Transaction ID": "20170709123456"
])
MyCustomAnalytics.logEvent("purchase"withParameters: [
  "currency": "USD",
  "price": 9.99,
  "transaction_id": "20170709123456"
])

After 😊

let analytics = Analytics<MyAppEvent>()
analytics.register(provider: FirebaseProvider())
analytics.register(provider: FlurryProvider())
analytics.register(provider: MyCustomProvider())
analytics.log(.purchase(currency: "USD", price: 9.99, transactionID: "20170709123456"))

Getting Started

Defining Events

First of all, you should define all of your events in a single enum. Let's assume that we have three events that have associated parameters.

enum MyAppEvent {
  case signup(username: String)
  case viewContent(productID: Int)
  case purchase(productID: Int, price: Float)
}

Then make the enum to conform the protocol EventType. It requires two functions: name(for:) and parameters(for:).

extension MyAppEvent: EventType {
  /// An event name to be logged
  func name(for provider: ProviderType) -> String {
    switch self {
    case .signup: return "signup"
    case .viewContent: return "view_content"
    case .purchase: return "purchase"
    }
  }

  /// Parameters to be logged
  func parameters(for provider: ProviderType) -> [String: Any]? {
    switch self {
    case let .signup(username):
      return ["username": username]
    case let .viewContent(productID):
      return ["product_id": productID]
    case let .purchase(productID, price):
      return ["product_id": productID, "price": price]
    }
  }
}

You can even provide different event names and parameters by providers.

Adding Providers

A prodiver represents an actual analytics service provider. Firebase, Google Analytics and other services can become providers. It's easy to create a provider: just create a class and conform to the protocol ProviderType.

final class FirebaseProvider: ProviderType {
  func log(_ eventName: String, parameters: [String: Any]?) {
    FIRAnalytics.logEvent(withName: eventName, parameters: parameters)
  }
}

Note: At this time EventAnalytics doesn't provide default providers but it will provide popular provider classes in the future.

Creating Analytics

The last step is to create an instance of Analytics. You can define it anywhere but I'd recommended to create an instance as a global constant and register providers in application(_:didFinishLaunchingWithOptions:).

let analytics = Analytics<MyAppEvent>()
analytics.register(provider: FirebaseProvider())
analytics.register(provider: GoogleAnalyticsProvider())
analytics.register(provider: FlurryProvider())
analytics.register(provider: MyCustomProvider())

With this instance, you can log the events 🎉

analytics.log(.signup(username: "devxoul"))

Installation

  • Using CocoaPods:

    pod 'EventAnalytics'
  • Using Carthage:

    github "devxoul/EventAnalytics"
    

License

EventAnalytics is under MIT license. See the LICENSE file for more info.