Analytics abstraction layer for Swift.
To run the example project, clone the repo, and run pod install
from the Example directory first.
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
Also you can install LHypothesis via the Swift Package Manager
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]
}
}
}
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])
- FirebaseProvider (Firebase/Analytics)
- AppsFlyerProvider (AppsFlyerFramework)
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)
}
}
LHypothesis is available under the MIT license. See the LICENSE file for more info.