Swift SDK
Tracklytic Swift SDK für iOS und macOS Anwendungen
Swift SDK
Das Tracklytic Swift SDK ist für iOS und macOS Anwendungen optimiert und bietet eine native Integration in Ihre Swift-Projekte.
Installation
Swift Package Manager
Fügen Sie das Swift Package zu Ihrem Projekt hinzu:
dependencies: [
.package(url: "https://github.com/scalerit/tracklytic-swift", from: "1.0.0")
]Xcode
- Öffnen Sie Ihr Xcode-Projekt
- Gehen Sie zu File → Add Package Dependencies
- Geben Sie die URL ein:
https://github.com/scalerit/tracklytic-swift - Wählen Sie die gewünschte Version aus
Grundlegende Konfiguration
import Tracklytic
let tracker = Tracklytic(
apiToken: "your_api_token_here",
projectId: "your_project_id"
)Erweiterte Konfiguration
let tracker = Tracklytic(
apiToken: "your_api_token_here",
projectId: "your_project_id",
batchSize: 10,
flushInterval: 5.0,
debug: false,
offlineStorage: true,
maxOfflineEvents: 1000
)Event Tracking
Einfaches Event
tracker.track(event: "app_opened", properties: [
"version": "1.0.0",
"platform": "iOS"
])Event mit Channel
tracker.track(event: "purchase", properties: [
"product_id": "prod_123",
"amount": 29.99
], channel: "mobile_commerce")Event mit erweiterten Optionen
tracker.track(event: "button_click", properties: [
"button_name": "signup"
], options: [
"notify": true,
"icon": "🎯",
"tags": [
"source": "mobile",
"campaign": "summer_sale"
]
])User Identification
Nutzer identifizieren
tracker.identify(userId: "user_123", properties: [
"name": "John Doe",
"email": "john@example.com",
"plan": "premium",
"signup_date": "2024-01-15"
])Profil aktualisieren
tracker.identify(userId: "user_123", properties: [
"last_login": "2024-01-21T09:15:00",
"total_sessions": 46
])Insights Management
Einfache Insights
// Numerische Metriken
tracker.insight(title: "Total Users", value: 1250, icon: "👥")
// String-basierte Metriken
tracker.insight(title: "System Status", value: "Operational", icon: "✅")Komplexe Insights
tracker.insight(title: "User Breakdown", value: [
"total": 1250,
"premium": 312,
"free": 938,
"conversion_rate": 24.96
], icon: "📊")Insights inkrementieren
tracker.incrementInsight(title: "Daily Active Users", by: 1)iOS Integration
SwiftUI
import SwiftUI
import Tracklytic
struct ContentView: View {
@StateObject private var tracker = Tracklytic(
apiToken: "your_api_token_here",
projectId: "your_project_id"
)
var body: some View {
VStack {
Button("Track Event") {
tracker.track(event: "button_click", properties: [
"button": "cta"
])
}
}
.onAppear {
tracker.track(event: "screen_view", properties: [
"screen": "ContentView"
])
}
}
}UIKit
import UIKit
import Tracklytic
class ViewController: UIViewController {
private let tracker = Tracklytic(
apiToken: "your_api_token_here",
projectId: "your_project_id"
)
override func viewDidLoad() {
super.viewDidLoad()
tracker.track(event: "screen_view", properties: [
"screen": "ViewController"
])
}
@IBAction func buttonTapped(_ sender: UIButton) {
tracker.track(event: "button_click", properties: [
"button": "action_button"
])
}
}macOS Integration
AppKit
import AppKit
import Tracklytic
class AppDelegate: NSObject, NSApplicationDelegate {
private let tracker = Tracklytic(
apiToken: "your_api_token_here",
projectId: "your_project_id"
)
func applicationDidFinishLaunching(_ notification: Notification) {
tracker.track(event: "app_launched", properties: [
"platform": "macOS"
])
}
}Best Practices
Event-Namen
// Gut - Beschreibend und konsistent
tracker.track(event: "user_signup_completed", properties: [:])
tracker.track(event: "product_added_to_cart", properties: [:])
tracker.track(event: "payment_processed", properties: [:])
// Schlecht - Vage und inkonsistent
tracker.track(event: "click", properties: [:])
tracker.track(event: "action", properties: [:])Properties strukturieren
tracker.track(event: "purchase", properties: [
"product": [
"id": "prod_123",
"name": "Premium Plan",
"category": "subscription",
"price": 29.99
],
"transaction": [
"amount": 29.99,
"currency": "EUR",
"method": "credit_card"
],
"user": [
"id": "user_123",
"segment": "premium"
]
])Fehlerbehandlung
do {
try tracker.track(event: "important_event", properties: [
"data": "value"
])
} catch {
print("Event tracking failed: \(error)")
// Fallback-Logik oder Retry-Mechanismus
}Memory Management
class TrackingManager {
private let tracker: Tracklytic
init() {
self.tracker = Tracklytic(
apiToken: "your_api_token_here",
projectId: "your_project_id"
)
}
deinit {
tracker.flush() // Alle Events vor dem Dealloc senden
}
}Nächste Schritte: Schauen Sie sich die JavaScript SDK an.