Skip to content

Instantly share code, notes, and snippets.

@fmo91
Created February 23, 2017 03:44
Show Gist options
  • Save fmo91/440e6a16c573e23de2f916add045db18 to your computer and use it in GitHub Desktop.
Save fmo91/440e6a16c573e23de2f916add045db18 to your computer and use it in GitHub Desktop.
Sample implementation of AnalyticsManager. See the blogpost in Medium
import Foundation
final class AnalyticsManager: AnalyticsService {
// MARK: - Properties -
// The list of services added to this class as observers.
internal private(set) var services = [AnalyticsService]()
// MARK: - Singleton -
static let instance = AnalyticsManager()
private init() {}
// MARK: - Methods -
/**
Adds a service as an observer.
*/
func add(service: AnalyticsService) {
self.services.append(service)
}
// MARK: - AnalyticsService -
func initialize(application: UIApplication, launchOptions: [UIApplicationLaunchOptionsKey: Any]?) {
// When AnalyticsManager is instantiated,
// it also instantiates each service that
// has been added to it.
for service in services {
service.initialize(
application : application,
launchOptions : launchOptions
)
}
}
func track(event: Event) {
// When an event is tracked by AnalyticsManager,
// it will notify all the services that has been added to it.
for service in services where service.shouldTrack(event: event) {
service.track(event: event)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment