Skip to content

Latest commit

 

History

History
137 lines (99 loc) · 3.88 KB

README.md

File metadata and controls

137 lines (99 loc) · 3.88 KB

LHypothesis

Build Status codecov Version License Platform

Analytics abstraction layer for Swift.

Table of Contents

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

CocoaPods

LHypothesis is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'LHypothesis'
pod 'LHypothesis/Firebase' # using with built-in FirebaseProvider
pod 'LHypothesis/AppsFlyer' # using with built-in AppsFlyerProvider

Swift Package Manager

Also you can install LHypothesis via the Swift Package Manager

Getting started

Defining Events

First of all, you should define all of your events in a enums.

enum AuthEvent {
  case signup(username: String)
  case signin(username: String)
  case resetPassword(username: String)
}

enum PurchaseEvents {
  case purchase(productID: Int, price: Float)
  case requestPrice(productID: Int)
}

Then make the enum to conform the protocol AnalyticsEvent. It requires three variables: name, parameters and userProperties.

extension PurchaseEvents: AnalyticsEvent {
  /// An event name to be logged
  var name: String {
    switch self {
    case .purchase: return "purchase"
    case .requestPrice: return "request_price"
    }
  }

  /// Parameters to be logged
  var parameters: AnalyticsEventParameters? {
    switch self {
    case let .purchase(productID, price):
      return ["product_id": productID, "price": price]
    case .requestPrice(let productID): 
      return ["product_id": productID]
    }
  }

}

Usage

First of all, you should register providers. A prodiver is a wrapper for an actual analytics service such as Firebase and Fabric Answers. It's recommended to register providers in application(_:didFinishLaunchingWithOptions:).

Analytics.register([FirebaseProvider(), AppsFlyerProvider()])

Then you can log the events:

Analytics.log(event: .purchase(providerID: 1, price: 12.0))

This method log events for all providers. If you want to log an events for specific providers, use the providersFilter

Analytics.log(event: .purchase(providerID: 1, price: 12.0), providersFilter: [FirebaseProvider.self])

Built-in Providers

Custom Providers

If there's no built-in provider for the serivce you're using, you can also create your own. It's easy to create a provider: just create a class and conform to the protocol AnalyticsProvider.

class MyAnalyticsProvider: AnalyticsProvider {
  
  func logEvent(_ event: AnalyticsEvent) {
    Analytics.logEvent(event.name, parameters: event.parameters)
  }
  
  public func setUserProperty(_ property: String?, forName name: String) {
    Analytics.setUserProperty(property, forName: name)
  }
  
  func setUserId(_ userId: String?) {
    Analytics.setUserID(userId)
  }
}

License

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