Abstract Analytics Framework supports:
- Unify Analytics.
- Modularize, Centralize Analytics.
- Plugin the new Analytics easier.
- Standadize tracking method and event.
- Xcode 12+
- Swift 5.0+
- Implement your AnalyticsService.
class ClientAnalyticsImpl: AnalyticsService {
// Specify whitelist events. Accept all events by default
var allowEvents: Set<String> {
Set<String>(arrayLiteral: "\(InputOTPEvent.self)", "\(CheckoutEvent.self)")
}
func setUserIdentifyProperty(_ property: [String : String]) {
// To support identify the user"
}
func reset() {
// reset all data related to the user e.g user logout"
}
func send(event: AnalyticsEvent) {
// Specific Analytics implementation e.g FireBaseAnalytics, MixPanel, etc.
print("### Send an event name: \(event.name), payload = \(event.payload)")
}
func send(event: AnalyticsEvent, from viewController: DLAnalytics.ViewController) {
print("### Send an event name: \(event.name), controller = \(ViewController.self)" )
}
}
- Declare your custom event.
// MARK: - Support dynamic configurable payload for an event
struct InputOTPEvent: AnalyticsEvent {
private(set) var payload: [String: String]
var name: String {
return "InputOTP"
}
static func inputOTPSuccess() -> InputOTPEvent {
return InputOTPEvent(payload: ["OTPValid": "1"])
}
}
// MARK: - Enum support static configurable payload for an event
@frozen
enum CheckoutEvent: String, AnalyticsEvent {
case success = "Checkout_Success"
case error = "Checkout_Error"
internal var payload: [String: Any] {
return [:]
}
var name: String {
return rawValue
}
}
- Register your custom AnalyticsService.
let analyticsService = ClientAnalyticsImpl()
Analytics.registerAnalyticsService(analyticsService)
/// Simulate tracking event InputOTP success
Analytics.send(event: InputOTPEvent.inputOTPSuccess())
Analytics.send(event: CheckoutEvent.success, from viewController: checkoutVC)
/// Output:
Send an event name: InputOTP, payload = ["OTPValid": "1"]
Send an event name: Checkout_Success, controller = CheckoutViewController
There are three ways to install DLAnalytics
Just add to your project's Podfile
:
pod 'DLAnalytics', '~> 2.1'
Add following to Cartfile
:
github "lengocduy/DLAnalytics" ~> 2.1
- To building platform-independent xcframeworks Xcode 12 and above here
- To migrating from framework bundles to xcframework here
Create a Package.swift
file:
// swift-tools-version:5.0
import PackageDescription
let package = Package(
name: "TestDLAnalytics",
dependencies: [
.package(url: "https://github.com/lengocduy/DLAnalytics.git", from: "2.1.0"),
],
targets: [
.target(
name: "TestDLAnalytics",
dependencies: ["DLAnalytics"])
]
)
DLAnalytics is available under the MIT license. See the LICENSE file for more info.