forked from TeamSobokSobok/Sobok-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TeamSobokSobok#388] Firebase Analytics Abstraction
- Loading branch information
1 parent
645498d
commit 4392cb2
Showing
5 changed files
with
162 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
63 changes: 63 additions & 0 deletions
63
SobokSobok/SobokSobok/Common/Analytics/AnalyticsProvider.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// | ||
// AnalyticsProvider.swift | ||
// SobokSobok | ||
// | ||
// Created by taekki on 2022/11/30. | ||
// | ||
|
||
import Foundation | ||
|
||
typealias AnalyticsEventParameters = [String: Any?] | ||
typealias AnalyticsEventUserProperties = [String: String?] | ||
|
||
protocol AnalyticsEvent { | ||
var name: String { get } | ||
var parameters: AnalyticsEventParameters { get } | ||
var userProperties: AnalyticsEventUserProperties { get } | ||
} | ||
|
||
protocol AnalyticsProvider { | ||
var name: String { get } | ||
|
||
func logEvent( | ||
_ event: AnalyticsEvent, | ||
_ parameters: [String: Any], | ||
_ userProperties: [String: String]) | ||
|
||
func setUserId(_ userId: String?) | ||
} | ||
|
||
enum Analytics { | ||
private static var providers: [AnalyticsProvider] = [] | ||
|
||
static func register(_ providers: [AnalyticsProvider]) { | ||
for provider in providers { | ||
Self.providers.append(provider) | ||
} | ||
} | ||
|
||
static func log(_ event: AnalyticsEvent) { | ||
let parameters: [String: Any] = event.parameters | ||
.reduce(into: [:], { $0[$1.key] = $1.value }) | ||
let userProperties: [String: String] = event.userProperties | ||
.reduce(into: [:], { $0[$1.key] = $1.value }) | ||
for provider in Self.providers { | ||
provider.logEvent(event, parameters, userProperties) | ||
} | ||
} | ||
|
||
static func setUserId(_ userId: String?) { | ||
for provider in Self.providers { | ||
provider.setUserId(userId) | ||
} | ||
} | ||
} | ||
|
||
extension AppDelegate { | ||
|
||
func registerAnalyticsProvider() { | ||
Analytics.register([ | ||
FirebaseAnalyticsProvider() | ||
]) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
SobokSobok/SobokSobok/Common/Analytics/FirebaseAnalyticsProvider.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// FirebaseAnalyticsProvider.swift | ||
// SobokSobok | ||
// | ||
// Created by taekki on 2022/11/30. | ||
// | ||
|
||
import FirebaseAnalytics | ||
|
||
final class FirebaseAnalyticsProvider: AnalyticsProvider { | ||
let name: String = "Firebase" | ||
|
||
func logEvent(_ event: AnalyticsEvent, _ parameters: [String : Any], _ userProperties: [String : String]) { | ||
for userProperty in userProperties { | ||
FirebaseAnalytics.Analytics.setUserProperty(userProperty.value, forName: userProperty.key) | ||
} | ||
|
||
FirebaseAnalytics.Analytics.logEvent(event.name, parameters: parameters) | ||
} | ||
|
||
func setUserId(_ userId: String?) { | ||
FirebaseAnalytics.Analytics.setUserID(userId) | ||
} | ||
} |