Last active
February 24, 2025 20:45
-
-
Save sohamb1390/0a012581423c24376f746eb6318987b4 to your computer and use it in GitHub Desktop.
Complete code gist of Analytics Service and Analytics Manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
@MainActor | |
final class AnalyticsManager { | |
static let manager: AnalyticsManager = AnalyticsManager() | |
private var services: [any AnalyticsService] = [] | |
private init() {} | |
func initialize(with services: [any AnalyticsService], application: UIApplication, launchOptions: [UIApplication.LaunchOptionsKey : Any]?) { | |
self.services = services | |
for service in services { | |
service.initialise(with: application, launchOptions: launchOptions) | |
} | |
} | |
func trackEvent(_ event: Event) { | |
for service in services { | |
service.track(event) | |
} | |
} | |
func trackEvents(_ events: [Event], for type: AnalyticsType) { | |
if let service = services.first(where: { $0.analyticsType as! AnalyticsType == type }) { | |
for event in events { | |
service.track(event) | |
} | |
} | |
} | |
func unsetEvent(_ event: Event) { | |
for service in services { | |
service.unset(event) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
/* THIS PART CAN BE SEPARATED IN A FRAMEWORK */ | |
/// A struct that represents an event | |
public struct Event { | |
/// Name of the event | |
let name: String | |
/// Properties of the event | |
let properties: [String: Any] | |
} | |
public protocol AnalyticsService: Sendable { | |
associatedtype T | |
/// An unique type to recognise a particular analytics service | |
var analyticsType: T { get } | |
/// A method to track a single `Event` | |
func track(_ event: Event) | |
/// A method to unset an event. This can be utilised when we want to discard any tracking event for a particular action | |
func unset(_ event: Event) | |
/// Initialises analytics service with `UIApplication` | |
func initialise(with application: UIApplication, launchOptions: [UIApplication.LaunchOptionsKey: Any]?) | |
} | |
/* THIS PART CAN BE SEPARATED IN A FRAMEWORK */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Various AnalyticsType | |
enum AnalyticsType { | |
case mixpanel | |
case firebase | |
case heap | |
// Few more analytics service type | |
// .. | |
// .. | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
/// Firebase Analytics Service | |
struct FirebaseAnalyticsService: AnalyticsService { | |
var analyticsType: AnalyticsType { | |
.firebase | |
} | |
func track(_ event: Event) { | |
// Track Event | |
} | |
func unset(_ event: Event) { | |
// Unset Event | |
} | |
func initialise(with application: UIApplication, launchOptions: [UIApplication.LaunchOptionsKey : Any]?) { | |
// Initialise Firebase | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
/// MixPanel Analytics Service | |
struct MixPanelAnalyticsService: AnalyticsService { | |
var analyticsType: AnalyticsType { | |
.mixpanel | |
} | |
func track(_ event: Event) { | |
// Track Event | |
} | |
func unset(_ event: Event) { | |
// Unset Event | |
} | |
func initialise(with application: UIApplication, launchOptions: [UIApplication.LaunchOptionsKey : Any]?) { | |
// Initialise Firebase | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment