Skip to content

Commit

Permalink
feat: allow configuring whitelist events to handle (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
lengocduy authored Sep 29, 2021
1 parent bebf22e commit 2519a39
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 10 deletions.
Binary file modified ArchDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 31 additions & 4 deletions DLAnalytics/Impl/AnalyticsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ final class AnalyticsManager {

// MARK: - CombineEvent
public struct CombineEvent: AnalyticsEvent {
public private(set) var type: String
public var name: String
public var payload: [String: Any]
}
Expand Down Expand Up @@ -79,10 +80,36 @@ extension AnalyticsManager: AnalyticsService {
}

services.forEach {
var combinedPayload = userProperty
combinedPayload.merge(dict: event.payload)
let combinedEvent = CombineEvent(name: event.name, payload: combinedPayload)
$0.send(event: combinedEvent)
if $0.allowEvents.contains(event.type) {
let combineEvent = buildCombineEvent(event)
$0.send(event: combineEvent)
}
}
}

/// Reset all data related to the user e.g user logout
func send(event: AnalyticsEvent, from viewController: ViewController) {
var services = [AnalyticsService]()
readWriteLock.read {
services = self.analyticsServices
}

services.forEach {
if $0.allowEvents.isEmpty || $0.allowEvents.contains(event.type) {
let combineEvent = buildCombineEvent(event)
$0.send(event: combineEvent, from: viewController)
}
}
}

/// Combine event form share event with the current event
private func buildCombineEvent(_ event: AnalyticsEvent) -> CombineEvent {
var combinedPayload = userProperty
combinedPayload.merge(dict: event.payload)
return CombineEvent(
type: event.type,
name: event.name,
payload: combinedPayload
)
}
}
5 changes: 5 additions & 0 deletions DLAnalytics/Model/Analytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public enum Analytics {
public static func reset() {
AnalyticsManager.sharedInstance.reset()
}

/// Support some cases want to track from or present some screens inside specific consumer (Analytics services)
public static func send(event: AnalyticsEvent, from viewController: ViewController) {
AnalyticsManager.sharedInstance.send(event: event, from: viewController)
}
}
5 changes: 5 additions & 0 deletions DLAnalytics/Protocol/AnalyticsEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
import Foundation

public protocol AnalyticsEvent {
var type: String { get }
var name: String { get }
var payload: [String: Any] { get }
}

public extension AnalyticsEvent {
var type: String { "\(Self.self)" }
}
19 changes: 19 additions & 0 deletions DLAnalytics/Protocol/AnalyticsService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@
//

import Foundation
#if os(iOS)
import UIKit
public typealias ViewController = UIViewController
#elseif os(macOS)
import AppKit
public typealias ViewController = NSViewController
#endif

public protocol AnalyticsService {
/// Whitelist events handle by the service. Handle all events as default if it does not specify
var allowEvents: Set<String> { get }

/// To support identify the user we need to help set these properties as global properties
func setUserIdentifyProperty(_ property: [String: String])

Expand All @@ -17,4 +27,13 @@ public protocol AnalyticsService {

/// Send an event to Analytics
func send(event: AnalyticsEvent)

/// Send an event to Analytics from a ViewController
func send(event: AnalyticsEvent, from viewController: ViewController)
}

public extension AnalyticsService {
var allowEvents: Set<String> { Set<String>() }

func send(event: AnalyticsEvent, from viewController: ViewController) {}
}
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Platform](https://img.shields.io/cocoapods/p/DLAnalytics.svg?style=flat)](http://cocoapods.org/pods/DLAnalytics)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)

An abstract Analytics Framework supports:
Abstract Analytics Framework supports:

- Unify Analytics.
- Modularize, Centralize Analytics.
Expand All @@ -26,18 +26,27 @@ An abstract Analytics Framework supports:

```
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]) {
print("setUserIdentifyProperty: To support identify the user")
// To support identify the user"
}
func reset() {
print("reset: To reset all data related to the user e.g user logout")
// reset all data related to the user e.g user logout"
}
func send(event: AnalyticsEvent) {
// Here is the specific Analytics implementation e.g FireBaseAnalytics, MixPanel, etc.
// 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)" )
}
}
```

Expand Down Expand Up @@ -85,11 +94,11 @@ Analytics.registerAnalyticsService(analyticsService)
```
/// Simulate tracking event InputOTP success
Analytics.send(event: InputOTPEvent.inputOTPSuccess())
Analytics.send(event: CheckoutEvent.success)
Analytics.send(event: CheckoutEvent.success, from viewController: checkoutVC)
/// Output:
Send an event name: InputOTP, payload = ["OTPValid": "1"]
Send an event name: Checkout_Success, payload = [:]
Send an event name: Checkout_Success, controller = CheckoutViewController
```

## Installation
Expand Down

0 comments on commit 2519a39

Please sign in to comment.