A package that provides a type-safe Analytics API that's backend agnostic
Such as a view or interaction:
public extension Analytics {
struct View: AnalyticsEvent {
public var name: String { "view" }
}
}
public extension AnalyticsEvent where Self == Analytics.View {
static var view: Self { .init() }
}
From using the Analytics.View
above you can simply do the following to log the event:
struct ContactListView: View {
@Environment(\.analytics) private var log
var body: some View {
List { /* content hidden */ }
.onAppear {
log(.view)
}
}
}
Parameters are defined very similarly to how you'd define a custom EnvironmentKey
in SwiftUI
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.
struct RootView: View {
var body: some View {
TabView { /* content hidden */ }
.analytics(\.source, "app-tabs")
}
}
Example
Button("Submit") {
log(.interaction)
}
/*
Prints:
interaction
- source: app-tabs
*/
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)