This commit is contained in:
@@ -234,7 +234,7 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
sections: [sectionOne, sectionTwo, sectionThree],
|
||||
tags: ["ios", "iosonly"],
|
||||
isStatic: false,
|
||||
visibility: .public
|
||||
visibility: .internal
|
||||
)
|
||||
|
||||
// Expect Analytics
|
||||
@@ -243,29 +243,32 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
|
||||
import Foundation
|
||||
import FirebaseAnalytics
|
||||
import os
|
||||
|
||||
\(protocolString(visibility: .public))
|
||||
\(protocolString(visibility: .internal))
|
||||
|
||||
\(firebaseString())
|
||||
|
||||
// MARK: - Traker Type
|
||||
|
||||
public enum TrackerType: CaseIterable {
|
||||
internal enum TrackerType: CaseIterable {
|
||||
|
||||
case firebase
|
||||
}
|
||||
|
||||
// MARK: - Manager
|
||||
|
||||
public class AnalyticsManager {
|
||||
internal class AnalyticsManager {
|
||||
|
||||
public static var shared = AnalyticsManager()
|
||||
internal static var shared = AnalyticsManager()
|
||||
|
||||
private init() {}
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
var managers: [TrackerType: AnalyticsManagerProtocol] = [:]
|
||||
|
||||
private var isDebugMode: Bool = false
|
||||
|
||||
private var isEnabled: Bool {
|
||||
if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" {
|
||||
@@ -274,6 +277,8 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
private let logger = Logger(subsystem: "resgen", category: "analytics")
|
||||
|
||||
// MARK: - Enable Methods
|
||||
|
||||
@@ -287,28 +292,55 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public func enableAnalytics(_ analytics: [TrackerType] = TrackerType.allCases) {
|
||||
internal func enableAnalytics(_ analytics: [TrackerType] = TrackerType.allCases) {
|
||||
setAnalytics(enable: true, analytics)
|
||||
}
|
||||
|
||||
public func disableAnalytics(_ analytics: [TrackerType] = TrackerType.allCases) {
|
||||
internal func disableAnalytics(_ analytics: [TrackerType] = TrackerType.allCases) {
|
||||
setAnalytics(enable: false, analytics)
|
||||
}
|
||||
|
||||
public func configure() {
|
||||
internal func configure(isDebugMode: Bool = false) {
|
||||
self.isDebugMode = isDebugMode
|
||||
managers[TrackerType.firebase] = FirebaseAnalyticsManager()
|
||||
}
|
||||
|
||||
// MARK: - Private Log Methods
|
||||
|
||||
private func formattedParams(_ params: [String: Any]?) -> String {
|
||||
guard let params = params, !params.isEmpty else { return "-" }
|
||||
|
||||
let formattedParams = params.map { key, value in
|
||||
" \\(key): \\(value)"
|
||||
}
|
||||
.joined(separator: "\\n")
|
||||
|
||||
return "\\n" + formattedParams
|
||||
}
|
||||
|
||||
private func logScreen(
|
||||
name: String,
|
||||
path: String,
|
||||
params: [String: Any]?
|
||||
) {
|
||||
guard isEnabled else { return }
|
||||
guard isEnabled else {
|
||||
if isDebugMode {
|
||||
logger.log("Analytics disabled")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
managers.values.forEach { manager in
|
||||
|
||||
if isDebugMode {
|
||||
logger.debug(\"""
|
||||
🖥️ Screen:
|
||||
Name: \\(name, privacy: .public)
|
||||
Path: \\(path, privacy: .public)
|
||||
Params: \\(self.formattedParams(params), privacy: .public)
|
||||
\""")
|
||||
}
|
||||
|
||||
manager.logScreen(
|
||||
name: name,
|
||||
path: path,
|
||||
@@ -323,9 +355,25 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
category: String,
|
||||
params: [String: Any]?
|
||||
) {
|
||||
guard isEnabled else { return }
|
||||
guard isEnabled else {
|
||||
if isDebugMode {
|
||||
logger.log("Analytics disabled")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
managers.values.forEach { manager in
|
||||
|
||||
if isDebugMode {
|
||||
logger.debug(\"""
|
||||
📊 Event:
|
||||
Name: \\(name, privacy: .public)
|
||||
Action: \\(action.isEmpty ? "-" : action, privacy: .public)
|
||||
Category: \\(category.isEmpty ? "-" : category, privacy: .public)
|
||||
Params: \\(self.formattedParams(params), privacy: .public)
|
||||
\""")
|
||||
}
|
||||
|
||||
manager.logEvent(
|
||||
name: name,
|
||||
action: action,
|
||||
@@ -337,7 +385,7 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
|
||||
// MARK: - section_one
|
||||
|
||||
public func logScreenS1DefOne() {
|
||||
internal func logScreenS1DefOne() {
|
||||
logScreen(
|
||||
name: "s1 def one",
|
||||
path: "",
|
||||
@@ -345,7 +393,7 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
)
|
||||
}
|
||||
|
||||
public func logEventS1DefTwo() {
|
||||
internal func logEventS1DefTwo() {
|
||||
logEvent(
|
||||
name: "s1 def two",
|
||||
action: "",
|
||||
@@ -356,7 +404,7 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
|
||||
// MARK: - section_two
|
||||
|
||||
public func logScreenS2DefOne() {
|
||||
internal func logScreenS2DefOne() {
|
||||
logScreen(
|
||||
name: "s2 def one",
|
||||
path: "",
|
||||
@@ -399,7 +447,7 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
sections: [sectionOne, sectionTwo, sectionThree],
|
||||
tags: ["ios", "iosonly"],
|
||||
isStatic: false,
|
||||
visibility: .package
|
||||
visibility: .internal
|
||||
)
|
||||
// Expect Analytics
|
||||
let expect = """
|
||||
@@ -407,29 +455,32 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
|
||||
import Foundation
|
||||
import MatomoTracker
|
||||
import os
|
||||
|
||||
\(protocolString(visibility: .package))
|
||||
\(protocolString(visibility: .internal))
|
||||
|
||||
\(matomoString())
|
||||
|
||||
// MARK: - Traker Type
|
||||
|
||||
package enum TrackerType: CaseIterable {
|
||||
internal enum TrackerType: CaseIterable {
|
||||
|
||||
case matomo
|
||||
}
|
||||
|
||||
// MARK: - Manager
|
||||
|
||||
package class AnalyticsManager {
|
||||
internal class AnalyticsManager {
|
||||
|
||||
package static var shared = AnalyticsManager()
|
||||
internal static var shared = AnalyticsManager()
|
||||
|
||||
private init() {}
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
var managers: [TrackerType: AnalyticsManagerProtocol] = [:]
|
||||
|
||||
private var isDebugMode: Bool = false
|
||||
|
||||
private var isEnabled: Bool {
|
||||
if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" {
|
||||
@@ -438,6 +489,8 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
private let logger = Logger(subsystem: "resgen", category: "analytics")
|
||||
|
||||
// MARK: - Enable Methods
|
||||
|
||||
@@ -451,15 +504,16 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
package func enableAnalytics(_ analytics: [TrackerType] = TrackerType.allCases) {
|
||||
internal func enableAnalytics(_ analytics: [TrackerType] = TrackerType.allCases) {
|
||||
setAnalytics(enable: true, analytics)
|
||||
}
|
||||
|
||||
package func disableAnalytics(_ analytics: [TrackerType] = TrackerType.allCases) {
|
||||
internal func disableAnalytics(_ analytics: [TrackerType] = TrackerType.allCases) {
|
||||
setAnalytics(enable: false, analytics)
|
||||
}
|
||||
|
||||
package func configure(siteId: String, url: String) {
|
||||
internal func configure(siteId: String, url: String, isDebugMode: Bool = false) {
|
||||
self.isDebugMode = isDebugMode
|
||||
managers[TrackerType.matomo] = MatomoAnalyticsManager(
|
||||
siteId: siteId,
|
||||
url: url
|
||||
@@ -468,14 +522,40 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
|
||||
// MARK: - Private Log Methods
|
||||
|
||||
private func formattedParams(_ params: [String: Any]?) -> String {
|
||||
guard let params = params, !params.isEmpty else { return "-" }
|
||||
|
||||
let formattedParams = params.map { key, value in
|
||||
" \\(key): \\(value)"
|
||||
}
|
||||
.joined(separator: "\\n")
|
||||
|
||||
return "\\n" + formattedParams
|
||||
}
|
||||
|
||||
private func logScreen(
|
||||
name: String,
|
||||
path: String,
|
||||
params: [String: Any]?
|
||||
) {
|
||||
guard isEnabled else { return }
|
||||
|
||||
guard isEnabled else {
|
||||
if isDebugMode {
|
||||
logger.log("Analytics disabled")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
managers.values.forEach { manager in
|
||||
|
||||
if isDebugMode {
|
||||
logger.debug(\"""
|
||||
🖥️ Screen:
|
||||
Name: \\(name, privacy: .public)
|
||||
Path: \\(path, privacy: .public)
|
||||
Params: \\(self.formattedParams(params), privacy: .public)
|
||||
\""")
|
||||
}
|
||||
|
||||
manager.logScreen(
|
||||
name: name,
|
||||
path: path,
|
||||
@@ -483,16 +563,32 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private func logEvent(
|
||||
name: String,
|
||||
action: String,
|
||||
category: String,
|
||||
params: [String: Any]?
|
||||
) {
|
||||
guard isEnabled else { return }
|
||||
|
||||
guard isEnabled else {
|
||||
if isDebugMode {
|
||||
logger.log("Analytics disabled")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
managers.values.forEach { manager in
|
||||
|
||||
if isDebugMode {
|
||||
logger.debug(\"""
|
||||
📊 Event:
|
||||
Name: \\(name, privacy: .public)
|
||||
Action: \\(action.isEmpty ? "-" : action, privacy: .public)
|
||||
Category: \\(category.isEmpty ? "-" : category, privacy: .public)
|
||||
Params: \\(self.formattedParams(params), privacy: .public)
|
||||
\""")
|
||||
}
|
||||
|
||||
manager.logEvent(
|
||||
name: name,
|
||||
action: action,
|
||||
@@ -501,18 +597,18 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - section_one
|
||||
|
||||
package func logScreenS1DefOne() {
|
||||
|
||||
internal func logScreenS1DefOne() {
|
||||
logScreen(
|
||||
name: "s1 def one",
|
||||
path: "s1_def_one/",
|
||||
params: nil
|
||||
)
|
||||
}
|
||||
|
||||
package func logEventS1DefTwo() {
|
||||
|
||||
internal func logEventS1DefTwo() {
|
||||
logEvent(
|
||||
name: "s1 def two",
|
||||
action: "test",
|
||||
@@ -520,10 +616,10 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
params: nil
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
// MARK: - section_two
|
||||
|
||||
package func logScreenS2DefOne() {
|
||||
|
||||
internal func logScreenS2DefOne() {
|
||||
logScreen(
|
||||
name: "s2 def one",
|
||||
path: "s2_def_one/",
|
||||
@@ -576,6 +672,7 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
import Foundation
|
||||
import MatomoTracker
|
||||
import FirebaseAnalytics
|
||||
import os
|
||||
|
||||
\(protocolString(visibility: .internal))
|
||||
|
||||
@@ -602,6 +699,8 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
// MARK: - Properties
|
||||
|
||||
var managers: [TrackerType: AnalyticsManagerProtocol] = [:]
|
||||
|
||||
private var isDebugMode: Bool = false
|
||||
|
||||
private var isEnabled: Bool {
|
||||
if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" {
|
||||
@@ -610,6 +709,8 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
private let logger = Logger(subsystem: "resgen", category: "analytics")
|
||||
|
||||
// MARK: - Enable Methods
|
||||
|
||||
@@ -631,7 +732,8 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
setAnalytics(enable: false, analytics)
|
||||
}
|
||||
|
||||
internal func configure(siteId: String, url: String) {
|
||||
internal func configure(siteId: String, url: String, isDebugMode: Bool = false) {
|
||||
self.isDebugMode = isDebugMode
|
||||
managers[TrackerType.matomo] = MatomoAnalyticsManager(
|
||||
siteId: siteId,
|
||||
url: url
|
||||
@@ -640,15 +742,41 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
}
|
||||
|
||||
// MARK: - Private Log Methods
|
||||
|
||||
private func formattedParams(_ params: [String: Any]?) -> String {
|
||||
guard let params = params, !params.isEmpty else { return "-" }
|
||||
|
||||
let formattedParams = params.map { key, value in
|
||||
" \\(key): \\(value)"
|
||||
}
|
||||
.joined(separator: "\\n")
|
||||
|
||||
return "\\n" + formattedParams
|
||||
}
|
||||
|
||||
private func logScreen(
|
||||
name: String,
|
||||
path: String,
|
||||
params: [String: Any]?
|
||||
) {
|
||||
guard isEnabled else { return }
|
||||
|
||||
guard isEnabled else {
|
||||
if isDebugMode {
|
||||
logger.log("Analytics disabled")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
managers.values.forEach { manager in
|
||||
|
||||
if isDebugMode {
|
||||
logger.debug(\"""
|
||||
🖥️ Screen:
|
||||
Name: \\(name, privacy: .public)
|
||||
Path: \\(path, privacy: .public)
|
||||
Params: \\(self.formattedParams(params), privacy: .public)
|
||||
\""")
|
||||
}
|
||||
|
||||
manager.logScreen(
|
||||
name: name,
|
||||
path: path,
|
||||
@@ -656,16 +784,32 @@ final class AnalyticsGeneratorTests: XCTestCase {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private func logEvent(
|
||||
name: String,
|
||||
action: String,
|
||||
category: String,
|
||||
params: [String: Any]?
|
||||
) {
|
||||
guard isEnabled else { return }
|
||||
|
||||
guard isEnabled else {
|
||||
if isDebugMode {
|
||||
logger.log("Analytics disabled")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
managers.values.forEach { manager in
|
||||
|
||||
if isDebugMode {
|
||||
logger.debug(\"""
|
||||
📊 Event:
|
||||
Name: \\(name, privacy: .public)
|
||||
Action: \\(action.isEmpty ? "-" : action, privacy: .public)
|
||||
Category: \\(category.isEmpty ? "-" : category, privacy: .public)
|
||||
Params: \\(self.formattedParams(params), privacy: .public)
|
||||
\""")
|
||||
}
|
||||
|
||||
manager.logEvent(
|
||||
name: name,
|
||||
action: action,
|
||||
|
||||
Reference in New Issue
Block a user