Skip to content

lengocduy/DLAnalytics

Repository files navigation

DLAnalytics

CI Version License Platform Carthage compatible

Abstract Analytics Framework supports:

  • Unify Analytics.
  • Modularize, Centralize Analytics.
  • Plugin the new Analytics easier.
  • Standadize tracking method and event.

Requirements

  • Xcode 12+
  • Swift 5.0+

How

Setup

  1. 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)" )
	}
}
  1. 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
    }
}
  1. Register your custom AnalyticsService.
let analyticsService = ClientAnalyticsImpl()
Analytics.registerAnalyticsService(analyticsService)

Use

/// 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

Installation

There are three ways to install DLAnalytics

CocoaPods

Just add to your project's Podfile:

pod 'DLAnalytics', '~> 2.1'

Carthage

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

Swift Package Manager

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"])
        ]
)

Architecture

Architecture

Interaction Flow

Interaction Flow

License

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