EventTracker is an open-source Swift library that allows you to easily track and monitor multiple named events and their occurrences. The library provides a simple interface to register, update, reset, and evaluate events based on customizable conditions. It also includes support for logging and persistence through UserDefaults.
I use this in another library ReviewPrompter but it can be used to track any number of things when you want to perform some action when the user has done some event x number of times, e.g. Prompting them to purchase a premium subscription after they have used a feature three times.
- Track multiple named events with unique identifiers
- Set custom conditions to evaluate events
- Increase and reset event counts
- Evaluate whether an event has met its condition
- Check if an event is being tracked
- Retrieve event count and condition values
- Enable or disable logging
- Persist tracked events and their conditions using UserDefaults
The library is almost fully tested with 98.2% code coverage
To add EventTracker to your project using Swift Package Manager, add the following dependency in your Package.swift file:
dependencies: [
.package(url: "https://github.com/ChristianFox/EventTracker.git", from: "1.0.0")
]
Don't forget to add EventTracker to your target dependencies:
targets: [
.target(
name: "YourTarget",
dependencies: ["EventTracker"]),
]
Initialise an instance
import EventTracker
let eventTracker = EventTracker(isLoggingEnabled: true)
Begin tracking an event
eventTracker.trackEvent(forIdentifier: "event1", withCondition: 3)
Stop tracking an event
eventTracker.stopTrackingEvent(forIdentifier: "event1")
Change the condition for an event
do {
try eventTracker.changeCondition(5, forIdentifier: "event1")
} catch {
print("Error updating event condition: \(error)")
}
Increase the event count
do {
try eventTracker.increaseEventCount(forIdentifier: "event1")
} catch {
print("Error increasing event count: \(error)")
}
Reset the event count
do {
try eventTracker.resetEventCount(forIdentifier: "event1")
} catch {
print("Error resetting event count: \(error)")
}
Check if an event's condition has been met
if eventTracker.hasEventMetCondition(forIdentifier: "event1") {
print("Event met condition!")
}
Check an event is being tracked
if eventTracker.isTrackingEvent(forIdentifier: "event1") {
print("Event is being tracked!")
}
Check the number of times an event has occured
let eventCount = eventTracker.eventCount(forIdentifier: "event1")
print("Event count: \(eventCount)")
Check the current condition of an event
let eventCondition = eventTracker.condition(forIdentifier: "event1")
print("Event condition: \(eventCondition)")
Please open an issue for support.
Pull requests are welcome. I welcome developers of all skill levels to help improve the library, fix bugs, or add new features.
For major changes, please open an issue first to discuss what you would like to change.
Before submitting a pull request, please ensure that your code adheres to the existing code style and conventions, and that all tests pass. Additionally, if you're adding new functionality, please make sure to include unit tests to verify the behavior.
If you have any questions or need assistance, feel free to open an issue, and I'll do my best to help you out.
EventTracker is released under the MIT Licence. See the LICENSE file for more information.