{"id":30719862,"url":"https://github.com/dankinsoid/swift-analytics","last_synced_at":"2026-02-26T16:13:22.762Z","repository":{"id":238362488,"uuid":"796309510","full_name":"dankinsoid/swift-analytics","owner":"dankinsoid","description":"An analytics API for Swift","archived":false,"fork":false,"pushed_at":"2025-05-31T21:14:14.000Z","size":107,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-29T09:30:12.100Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dankinsoid.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2024-05-05T15:11:05.000Z","updated_at":"2025-05-31T21:14:09.000Z","dependencies_parsed_at":"2024-06-04T22:24:43.299Z","dependency_job_id":null,"html_url":"https://github.com/dankinsoid/swift-analytics","commit_stats":null,"previous_names":["dankinsoid/swift-analytics"],"tags_count":15,"template":false,"template_full_name":"dankinsoid/iOSLibraryTemplate","purl":"pkg:github/dankinsoid/swift-analytics","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dankinsoid%2Fswift-analytics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dankinsoid%2Fswift-analytics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dankinsoid%2Fswift-analytics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dankinsoid%2Fswift-analytics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dankinsoid","download_url":"https://codeload.github.com/dankinsoid/swift-analytics/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dankinsoid%2Fswift-analytics/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273431361,"owners_count":25104491,"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-09-03T02:00:09.631Z","response_time":76,"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":"2025-09-03T10:42:30.375Z","updated_at":"2026-02-26T16:13:22.732Z","avatar_url":"https://github.com/dankinsoid.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SwiftAnalytics\nSwiftAnalytics is an API package which tries to establish a common API the ecosystem can use.\nTo make analytics really work for real-world workloads, we need SwiftAnalytics-compatible analytics backends which send events to Firebase, Amplitude, DataDog, etc.\n\n## Getting Started\n\n### Adding the dependency\nTo depend on the analytics API package, you need to declare your dependency in your Package.swift:\n```swift\n.package(url: \"https://github.com/dankinsoid/swift-analytics.git\", from: \"1.9.0\"),\n```\nand to your application/library target, add \"SwiftAnalytics\" to your dependencies, e.g. like this:\n```swift\n.target(name: \"BestExampleApp\", dependencies: [\n    .product(name: \"SwiftAnalytics\", package: \"swift-analytics\")\n],\n```\n### Let's send events\n1. let's import the SwiftAnalytics API package\n```swift\nimport SwiftAnalytics\n```\n\n2. we need to bootstrap the analytics system with a default analytics handler, which is usually a custom implementation of `AnalyticsHandler` protocol. For example you can use FirebaseAnalyticsHandler from the [swift-firebase-tools](https://github.com/dankinsoid/swift-firebase-tools) package, or you can implement your own analytics handler, it's a very simple protocol.\n\n```swift\nAnalyticsSystem.bootstrap(FirebaseAnalyticsHandler())\n```\n\n3. we need to create a Analytics\n```swift\nlet analytics = Analytics()\n```\n\n4. we're now ready to use it\n```swift\nanalytics.send(\"hello world\")\n```\n\n## The core concepts\n\n### Analytics\n`Analytics` are used to send events and therefore the most important type in SwiftAnalytics, so their use should be as simple as possible.\n\n### Analytics.Event\n`Analytics.Event` is a type that represents an event that should be sent. It has a name and a dictionary of parameters. Example:\n```swift\nlet event = Analytics.Event(\"hello world\", parameters: [\"foo\": \"bar\"])\n// or\nlet event = Analytics.Event(\"hello world\").with(\"foo\", \"bar\")\n```\n\n### Analytics parameters\n`Analytics` has a parameters that can be shared across all events sent by the same instance of `Analytics`. Example:\n```swift\nvar analytics = Analytics()\nanalytics.parameters[\"user-id\"] = \"\\(UUID())\"\nanalytics.send(\"hello world\")\n```\nThere are some helper functions to set parameters:\n```swift\nlet analytics2 = analytics1\n\t.with(\"user-id\", UUID())\n  .with(\"user-name\", \"Alice\")\n\nlet analytics3 = analytics2\n    .with([\"session-id\": UUID()])\n```\n\n## Codable Support\n\nSwiftAnalytics provides excellent support for Swift's `Codable` protocol, making it easy to work with structured data in your analytics events.\n\n### Using Encodable Types as Parameters\n\nYou can directly use any `Encodable` type as parameters:\n\n```swift\nstruct UserInfo: Codable {\n    let id: String\n    let name: String\n    let age: Int\n}\n\nlet userInfo = UserInfo(id: \"123\", name: \"Alice\", age: 30)\n\n// Create an event with Encodable parameters\nlet event = try Analytics.Event(\"user_profile_viewed\", parameters: userInfo)\n\n// Or add Encodable parameters to existing analytics\nlet analytics = Analytics()\n    .with(\"user\", userInfo)\n    .with(\"session_id\", UUID())\n```\n\n### Adding Individual Encodable Parameters\n\nYou can add individual parameters using any `Encodable` type:\n\n```swift\nlet analytics = Analytics().with(\"user_info\", userInfo)\n\n// or directly with a dictionary\nlet analytics = try Analytics().with(userInfo)\n```\n\n### JSON String Representation\n\nAll `Analytics.ParametersValue` instances provide a `jsonString` property for easy serialization:\n\n```swift\nlet parameters: Analytics.Parameters = [\n    \"user_id\": \"123\",\n    \"preferences\": [\"theme\": \"dark\", \"notifications\": true],\n    \"scores\": [95, 87, 92]\n]\n\nfor (key, value) in parameters {\n    print(\"\\(key): \\(value.jsonString)\")\n}\n// Output:\n// user_id: \"123\"\n// preferences: {\"notifications\":true,\"theme\":\"dark\"}\n// scores: [95,87,92]\n```\n\n## On the implementation of a analytics backend (a AnalyticsHandler)\nNote: If you don't want to implement a custom analytics backend, everything in this section is probably not very relevant, so please feel free to skip.\n\nTo become a compatible analytics backend that all SwiftAnalytics consumers can use, you need to do two things: \n1. Implement a type (usually a struct) that implements AnalyticsHandler, a protocol provided by SwiftAnalytics\n2. Instruct SwiftAnalytics to use your analytics backend implementation.\n\nan AnalyticsHandler or analytics backend implementation is anything that conforms to the following protocol\n```swift\npublic protocol AnalyticsHandler {\n    \n    var parameters: Analytics.Parameters { get set }\n    func send(event: Analytics.Event, file: String, function: String, line: UInt)\n}\n```\nWhere `parameters` is a dictionary of parameters that can be shared across all events sent by the same instance of `AnalyticsHandler`, and `send(event:file:function:line:)` is a function that sends an event.\n\nInstructing SwiftAnalytics to use your analytics backend as the one the whole application (including all libraries) should use is very simple:\n\n```swift\nAnalyticsSystem.bootstrap(MyAnalyticsHandler())\n```\n\n## Installation\n\n1. [Swift Package Manager](https://github.com/apple/swift-package-manager)\n\nCreate a `Package.swift` file.\n```swift\n// swift-tools-version:5.7\nimport PackageDescription\n\nlet package = Package(\n  name: \"SomeProject\",\n  dependencies: [\n    .package(url: \"https://github.com/dankinsoid/swift-analytics.git\", from: \"1.9.0\")\n  ],\n  targets: [\n    .target(name: \"SomeProject\", dependencies: [\"SwiftAnalytics\"])\n  ]\n)\n```\n```ruby\n$ swift build\n```\n\n## Author\n\ndankinsoid, voidilov@gmail.com\n\n## License\n\nswift-analytics is available under the MIT license. See the LICENSE file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdankinsoid%2Fswift-analytics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdankinsoid%2Fswift-analytics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdankinsoid%2Fswift-analytics/lists"}