-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ios): add custom event tracking
- Loading branch information
Adwin Ronald Ross
committed
Dec 31, 2024
1 parent
cd54d5e
commit f4abe89
Showing
20 changed files
with
252 additions
and
7 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// | ||
// CustomEventCollector.swift | ||
// MeasureSDK | ||
// | ||
// Created by Adwin Ross on 24/12/24. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol CustomEventCollector { | ||
func enable() | ||
func disable() | ||
func trackEvent(name: String, timestamp: Number?) | ||
} | ||
|
||
final class BaseCustomEventCollector: CustomEventCollector { | ||
private let logger: Logger | ||
private let eventProcessor: EventProcessor | ||
private let timeProvider: TimeProvider | ||
private let configProvider: ConfigProvider | ||
private var isEnabled = false | ||
private lazy var customEventNameRegex: NSRegularExpression? = { | ||
try? NSRegularExpression(pattern: configProvider.customEventNameRegex) | ||
}() | ||
|
||
init(logger: Logger, eventProcessor: EventProcessor, timeProvider: TimeProvider, configProvider: ConfigProvider) { | ||
self.logger = logger | ||
self.eventProcessor = eventProcessor | ||
self.timeProvider = timeProvider | ||
self.configProvider = configProvider | ||
} | ||
|
||
func enable() { | ||
isEnabled = true | ||
} | ||
|
||
func disable() { | ||
isEnabled = false | ||
} | ||
|
||
func trackEvent(name: String, timestamp: Number?) { | ||
guard isEnabled else { return } | ||
guard validateName(name) else { return } | ||
|
||
let data = CustomEventData(name: name) | ||
eventProcessor.track(data: data, | ||
timestamp: timeProvider.now(), | ||
type: .custom, | ||
attributes: nil, | ||
sessionId: nil, | ||
attachments: nil) | ||
} | ||
|
||
private func validateName(_ name: String) -> Bool { | ||
if name.isEmpty { | ||
logger.log(level: .warning, message: "Event name is empty. This event will be dropped.", error: nil, data: nil) | ||
return false | ||
} | ||
|
||
if name.count > configProvider.maxEventNameLength { | ||
logger.log(level: .warning, message: "Event(\(name)) exceeded max allowed length. This event will be dropped.", error: nil, data: nil) | ||
return false | ||
} | ||
|
||
if let regex = customEventNameRegex, | ||
regex.firstMatch(in: name, options: [], range: NSRange(location: 0, length: name.count)) == nil { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
} |
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,12 @@ | ||
// | ||
// CustomEventData.swift | ||
// MeasureSDK | ||
// | ||
// Created by Adwin Ross on 23/12/24. | ||
// | ||
|
||
import Foundation | ||
|
||
struct CustomEventData: Codable { | ||
let name: String | ||
} |
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
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
Oops, something went wrong.