{"id":16482801,"url":"https://github.com/shaps80/analytics","last_synced_at":"2025-10-19T00:11:24.356Z","repository":{"id":69909685,"uuid":"354419587","full_name":"shaps80/Analytics","owner":"shaps80","description":null,"archived":false,"fork":false,"pushed_at":"2022-10-11T16:38:19.000Z","size":58,"stargazers_count":3,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-01T20:37:24.627Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shaps80.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"shaps80","custom":"paypal.me/shapsuk"}},"created_at":"2021-04-04T00:01:39.000Z","updated_at":"2023-08-17T09:54:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"a2828b19-4642-4dc0-8dc4-54b891d94967","html_url":"https://github.com/shaps80/Analytics","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/shaps80/Analytics","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shaps80%2FAnalytics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shaps80%2FAnalytics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shaps80%2FAnalytics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shaps80%2FAnalytics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shaps80","download_url":"https://codeload.github.com/shaps80/Analytics/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shaps80%2FAnalytics/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268294020,"owners_count":24227595,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-01T02:00:08.611Z","response_time":67,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T13:12:04.723Z","updated_at":"2025-10-19T00:11:24.264Z","avatar_url":"https://github.com/shaps80.png","language":"Swift","funding_links":["https://github.com/sponsors/shaps80","paypal.me/shapsuk"],"categories":[],"sub_categories":[],"readme":"# Analytics\n\nA package that provides a type-safe Analytics API that's backend agnostic \n\n## Make an `AnalyticsEvent`\n\nSuch as a view or interaction:\n\n```swift\npublic extension Analytics {\n    struct View: AnalyticsEvent {\n        public var name: String { \"view\" }\n    }\n}\n\npublic extension AnalyticsEvent where Self == Analytics.View {\n    static var view: Self { .init() }\n}\n```\n\n## Track an event\n\nFrom using the `Analytics.View` above you can simply do the following to log the event:\n\n```swift\nstruct ContactListView: View {\n    @Environment(\\.analytics) private var log\n    var body: some View {\n        List { /* content hidden */ }\n            .onAppear {\n                log(.view)\n            }\n    }\n}\n```\n\n## Define Parameters\n\nParameters are defined very similarly to how you'd define a custom `EnvironmentKey` in SwiftUI\n\n```swift\nprivate struct SourceAnalyticsKey: AnalyticsKey {\n    typealias Value = String\n    static var key: String { \"source\" }\n}\n\nextension AnalyticsValues {\n    // note: values must be `Optional`\n    var source: String? {\n        get { self[SourceAnalyticsKey.self] }\n        set { self[SourceAnalyticsKey.self] = newValue }\n    }\n}\n```\n\nOnce 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.\n\n```swift\nstruct RootView: View {\n    var body: some View {\n        TabView { /* content hidden */ }\n            .analytics(\\.source, \"app-tabs\")\n    }\n}\n```\n\n**Example**\n\n```swift\nButton(\"Submit\") {\n    log(.interaction)\n}\n\n/*\nPrints:\n\ninteraction\n- source: app-tabs\n*/\n```\n\n## Append or replace params without `Environment` propagation\n```swift \nvar values = AnalyticsValues()\nvalues[keyPath: \\.source] = .contactList\n\n// You can append values to specific logs, while retaining any inherited values\n// note: any existing values with matching keys will have their values overwritten\nlog(.view, appending: values)\n\n// Or you can replace the inherited values entirely for a specific log\nlog(.view, replacing: values)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshaps80%2Fanalytics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshaps80%2Fanalytics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshaps80%2Fanalytics/lists"}