EventDispatcherKit

用于将事件分发到多个分析服务的库。

EventDispatcher (事件分发器)

处理来自您的应用程序的事件,将其编码为 JSON,然后将其分发到多个处理器。

func send<B: Encodable>(_ name: EventName, body: B)
func send<E: Event>(_ event: E)
func send<E: TechnicalEvent>(_ event: E)
func send<E: CustomizableEvent>(_ event: E)

EventProcessor (事件处理器)

分析服务的包装器,它将处理事件并将事件发送到该服务。

var name: EventProcessorName { get }
typealias Properties = [String: Any]
func send(_ name: EventName, properties: Properties)

Event (事件)

事件的简单协议,将被编码并分发到处理器。

struct CloseApp: Event {
    static let name: EventName = "app_close"
    let timestamp: Date
}

CustomizableEvent (可定制事件)

事件的协议,可以为每个处理器独立定制。

struct LogIn: CustomizableEvent, Encodable {
    func customized(for name: EventProcessorName) -> CustomizedEvent? {
        switch name {
        case .firebase:
            return .init(name: Firebase.AnalyticsEventLogin,
                         body: self)
        case .console:
            return .init(name: "login",
                         body: self)
        default:
            return nil
        }
    }
}

TechnicalEvent (技术事件)

技术事件的特殊协议,仅会被分发到 'isTechnical' 处理器。 仅用于开发者目的。

struct CloseApp: TechnicalEvent {
    static let name: EventName = "app_close"
}

ConsoleEventProcessor (控制台事件处理器)

处理器只是将事件打印到控制台,或者如果您愿意,也可以打印到您的自定义日志记录器。

import os

let logger = Logger(subsystem: "com.example", category: "EventDispatcherKit")
let processor = ConsoleEventProcessor(logger: { [logger] message in
    logger.log(level: .info, "\(message)")
})