https://github.com/shaps80/analytics
https://github.com/shaps80/analytics
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/shaps80/analytics
- Owner: shaps80
- Created: 2021-04-04T00:01:39.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2022-10-11T16:38:19.000Z (over 3 years ago)
- Last Synced: 2025-05-01T20:37:24.627Z (about 1 year ago)
- Language: Swift
- Size: 56.6 KB
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Analytics
A package that provides a type-safe Analytics API that's backend agnostic
## Make an `AnalyticsEvent`
Such as a view or interaction:
```swift
public extension Analytics {
struct View: AnalyticsEvent {
public var name: String { "view" }
}
}
public extension AnalyticsEvent where Self == Analytics.View {
static var view: Self { .init() }
}
```
## Track an event
From using the `Analytics.View` above you can simply do the following to log the event:
```swift
struct ContactListView: View {
@Environment(\.analytics) private var log
var body: some View {
List { /* content hidden */ }
.onAppear {
log(.view)
}
}
}
```
## Define Parameters
Parameters are defined very similarly to how you'd define a custom `EnvironmentKey` in SwiftUI
```swift
private struct SourceAnalyticsKey: AnalyticsKey {
typealias Value = String
static var key: String { "source" }
}
extension AnalyticsValues {
// note: values must be `Optional`
var source: String? {
get { self[SourceAnalyticsKey.self] }
set { self[SourceAnalyticsKey.self] = newValue }
}
}
```
Once defined, injecting the parameter into a `View` is all that's needed for it to be automatically added to all `AnalyticsEvent`s events within that hierarchy.
```swift
struct RootView: View {
var body: some View {
TabView { /* content hidden */ }
.analytics(\.source, "app-tabs")
}
}
```
**Example**
```swift
Button("Submit") {
log(.interaction)
}
/*
Prints:
interaction
- source: app-tabs
*/
```
## Append or replace params without `Environment` propagation
```swift
var values = AnalyticsValues()
values[keyPath: \.source] = .contactList
// You can append values to specific logs, while retaining any inherited values
// note: any existing values with matching keys will have their values overwritten
log(.view, appending: values)
// Or you can replace the inherited values entirely for a specific log
log(.view, replacing: values)
```