A comprehensive Swift package for analytics tracking across iOS apps, supporting multiple providers with a clean, type-safe API.
- 🎯 Multi-Provider Support - Track to Firebase, Amplitude, and console simultaneously
- 🏗️ Type-Safe Events - Structured event and user property protocols
- 🌐 Global Properties - Set once, included in all events automatically
- 🔧 Result Builder Syntax - Clean, declarative setup
- 🐛 Debug Support - Console engine with emoji indicators for development
- 📱 UIKit Integration - Easy app lifecycle integration
- ✅ Well Tested - Comprehensive test suite with mock implementations
Add CHAnalytics to your project using Swift Package Manager:
dependencies: [
.package(url: "https://github.com/Chainless-Dev/CHAnalytics.git", from: "1.0.0")
]Or add it through Xcode:
- File → Add Package Dependencies
- Enter:
https://github.com/Chainless-Dev/CHAnalytics.git
import CHAnalytics
// Setup analytics with multiple providers
let analytics = Analytics {
FirebaseAnalyticsEngine()
AmplitudeAnalyticsEngine(apiKey: "your-amplitude-key")
ConsoleAnalyticsEngine() // For debugging
}
// Configure on app launch
analytics.configure(for: .applicationDidFinishLaunching(isDebug: true))// Screen tracking
analytics.track(event: Event.screenView(screen: "HomeScreen"))
// User actions
analytics.track(event: Event.buttonTap(buttonName: "SignUp", screen: "LoginScreen"))
// E-commerce
analytics.track(event: Event.purchase(amount: 9.99, currency: "USD", itemName: "Premium"))
// Custom events
analytics.track(event: Event(name: "custom_event", parameters: [
"category": "engagement",
"value": 42
]))// Set user properties
analytics.track(userProperty: UserProperty(properties: [
"subscription_tier": "premium",
"onboarding_completed": true
]))
// Set user identifier
analytics.setUser(identifier: "user-123")
// Set global properties (included in all subsequent events)
analytics.setGlobalProperty(key: "app_version", value: "1.0.0")Automatically handles Firebase parameter limitations and integrates with Crashlytics.
let firebaseEngine = FirebaseAnalyticsEngine()Uses the modern Amplitude-Swift SDK with full feature support.
let amplitudeEngine = AmplitudeAnalyticsEngine(apiKey: "your-api-key")
// Access Amplitude-specific features
let sessionId = amplitudeEngine.sessionIdPerfect for development - logs events to console with emoji indicators.
let consoleEngine = ConsoleAnalyticsEngine()
// Outputs: 🎯 EVENT: screen_view | Parameters: screen_name: HomeScreenConvenient extensions for app lifecycle tracking:
// In your AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
analytics.configure(application: application, launchOptions: launchOptions)
return true
}
func applicationDidBecomeActive(_ application: UIApplication) {
analytics.handleApplicationDidBecomeActive()
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
analytics.handleOpen(url: url)
return true
}CHAnalytics includes comprehensive testing support:
import XCTest
@testable import CHAnalytics
// Use the built-in MockAnalyticsEngine for testing
let mockEngine = MockAnalyticsEngine()
let analytics = Analytics(engines: [mockEngine])
analytics.track(event: Event(name: "test_event"))
XCTAssertEqual(mockEngine.trackedEvents.count, 1)
XCTAssertEqual(mockEngine.trackedEvents.first?.name, "test_event")- iOS 13.0+
- macOS 11.0+
- tvOS 13.0+
- watchOS 6.0+
- CHLogger - Internal logging (implementation only)
- Firebase iOS SDK - Firebase Analytics & Crashlytics
- Amplitude-Swift - Amplitude analytics
Main analytics coordinator that manages multiple engines.
Protocol for defining trackable events.
name: String- Event nameparameters: [String: Any]?- Event parameters
Protocol for user properties.
setOnce: Bool- Whether to set property only onceproperties: [String: Any]- Property key-value pairs
Protocol for analytics providers.
Built-in event implementation with convenience initializers:
screenView(screen:)buttonTap(buttonName:screen:)purchase(amount:currency:itemName:)login(method:)signUp(method:)search(query:)share(contentType:itemId:)
Built-in user property implementation.
We welcome contributions! Please feel free to submit a Pull Request.
This project is available under the MIT license. See the LICENSE file for more info.
For issues and questions, please use GitHub Issues.