$npx -y skills add rshankras/claude-code-apple-skills --skill analytics-setupGenerates protocol-based analytics infrastructure with swappable providers (TelemetryDeck, Firebase, Mixpanel). Use when user wants to add analytics, track events, or set up telemetry.
| 1 | # Analytics Setup Generator |
| 2 | |
| 3 | Generate a protocol-based analytics infrastructure that makes it easy to swap providers without changing app code. |
| 4 | |
| 5 | ## When This Skill Activates |
| 6 | |
| 7 | Use this skill when the user: |
| 8 | - Asks to "add analytics" or "set up analytics" |
| 9 | - Mentions "TelemetryDeck", "Firebase Analytics", "Mixpanel" |
| 10 | - Wants to "track events" or "add telemetry" |
| 11 | - Asks about "privacy-friendly analytics" |
| 12 | - Wants to swap analytics providers |
| 13 | |
| 14 | ## Key Feature: Swappable Providers |
| 15 | |
| 16 | The generated code uses a protocol-based architecture: |
| 17 | |
| 18 | ```swift |
| 19 | // Your app uses the protocol |
| 20 | analytics.track(.buttonTapped("subscribe")) |
| 21 | |
| 22 | // Swap providers by changing ONE line: |
| 23 | let analytics: AnalyticsService = TelemetryDeckAnalytics() // or FirebaseAnalytics() |
| 24 | ``` |
| 25 | |
| 26 | ## Pre-Generation Checks |
| 27 | |
| 28 | ### 1. Project Context Detection |
| 29 | - [ ] Check for existing analytics implementations |
| 30 | - [ ] Look for TelemetryDeck/Firebase/Mixpanel in Package.swift or Podfile |
| 31 | - [ ] Identify source file locations |
| 32 | |
| 33 | ### 2. Conflict Detection |
| 34 | Search for existing analytics: |
| 35 | ``` |
| 36 | Glob: **/*Analytics*.swift, **/*Telemetry*.swift |
| 37 | Grep: "protocol.*Analytics" or "TelemetryDeck" or "Firebase" |
| 38 | ``` |
| 39 | |
| 40 | If found, ask user: |
| 41 | - Extend existing analytics? |
| 42 | - Replace with new implementation? |
| 43 | - Add new provider to existing setup? |
| 44 | |
| 45 | ## Configuration Questions |
| 46 | |
| 47 | Ask user via AskUserQuestion: |
| 48 | |
| 49 | 1. **Which provider(s)?** |
| 50 | - TelemetryDeck (privacy-friendly, recommended) |
| 51 | - Firebase Analytics |
| 52 | - Mixpanel |
| 53 | - None (NoOp for now, add later) |
| 54 | |
| 55 | 2. **What events to track?** |
| 56 | - App lifecycle (launch, background, foreground) |
| 57 | - Screen views |
| 58 | - User actions (buttons, features used) |
| 59 | - Errors |
| 60 | - Custom events |
| 61 | |
| 62 | 3. **User properties?** |
| 63 | - App version |
| 64 | - Subscription status |
| 65 | - Custom properties |
| 66 | |
| 67 | ## Generation Process |
| 68 | |
| 69 | ### Step 1: Create Core Files |
| 70 | |
| 71 | Always generate these files: |
| 72 | 1. `AnalyticsService.swift` - Protocol (never changes) |
| 73 | 2. `AnalyticsEvent.swift` - Event definitions (app-specific) |
| 74 | 3. `NoOpAnalytics.swift` - For testing/privacy mode |
| 75 | |
| 76 | ### Step 2: Create Selected Provider(s) |
| 77 | |
| 78 | Based on user selection: |
| 79 | - `TelemetryDeckAnalytics.swift` |
| 80 | - `FirebaseAnalytics.swift` |
| 81 | - `MixpanelAnalytics.swift` |
| 82 | |
| 83 | ### Step 3: Create Environment Integration |
| 84 | |
| 85 | For SwiftUI apps: |
| 86 | - `AnalyticsServiceKey.swift` - Environment key for dependency injection |
| 87 | |
| 88 | ### Step 4: Determine File Location |
| 89 | |
| 90 | Check project structure: |
| 91 | - If `Sources/` exists → `Sources/Analytics/` |
| 92 | - If `App/` exists → `App/Analytics/` |
| 93 | - Otherwise → `Analytics/` |
| 94 | |
| 95 | ## Output Format |
| 96 | |
| 97 | After generation, provide: |
| 98 | |
| 99 | ### Files Created |
| 100 | ``` |
| 101 | Sources/Analytics/ |
| 102 | ├── AnalyticsService.swift # Protocol (stable interface) |
| 103 | ├── AnalyticsEvent.swift # Your app's events |
| 104 | ├── Providers/ |
| 105 | │ ├── NoOpAnalytics.swift # Testing/privacy |
| 106 | │ └── [Provider]Analytics.swift # Selected provider(s) |
| 107 | └── AnalyticsServiceKey.swift # SwiftUI Environment (optional) |
| 108 | ``` |
| 109 | |
| 110 | ### Integration Steps |
| 111 | |
| 112 | **App Entry Point:** |
| 113 | ```swift |
| 114 | @main |
| 115 | struct MyApp: App { |
| 116 | // Choose your provider |
| 117 | private let analytics: AnalyticsService = TelemetryDeckAnalytics(appID: "YOUR-APP-ID") |
| 118 | |
| 119 | init() { |
| 120 | analytics.configure() |
| 121 | } |
| 122 | |
| 123 | var body: some Scene { |
| 124 | WindowGroup { |
| 125 | ContentView() |
| 126 | .environment(\.analytics, analytics) |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | ``` |
| 131 | |
| 132 | **Tracking Events:** |
| 133 | ```swift |
| 134 | struct ContentView: View { |
| 135 | @Environment(\.analytics) private var analytics |
| 136 | |
| 137 | var body: some View { |
| 138 | Button("Subscribe") { |
| 139 | analytics.track(.buttonTapped("subscribe")) |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | ``` |
| 144 | |
| 145 | ### Required Dependencies |
| 146 | |
| 147 | **TelemetryDeck:** |
| 148 | ```swift |
| 149 | // Package.swift |
| 150 | .package(url: "https://github.com/TelemetryDeck/SwiftClient", from: "1.0.0") |
| 151 | ``` |
| 152 | |
| 153 | **Firebase:** |
| 154 | ```swift |
| 155 | // Package.swift |
| 156 | .package(url: "https://github.com/firebase/firebase-ios-sdk", from: "10.0.0") |
| 157 | // Also requires GoogleService-Info.plist |
| 158 | ``` |
| 159 | |
| 160 | ### Swapping Providers Later |
| 161 | |
| 162 | To switch providers: |
| 163 | 1. Add new provider file (or generate with this skill) |
| 164 | 2. Change ONE line in App.swift: |
| 165 | ```swift |
| 166 | // Before |
| 167 | private let analytics: AnalyticsService = TelemetryDeckAnalytics(...) |
| 168 | |
| 169 | // After |
| 170 | private let analytics: AnalyticsService = FirebaseAnalytics() |
| 171 | ``` |
| 172 | |
| 173 | ### Testing |
| 174 | - Use `NoOpAnalytics()` in tests and previews |
| 175 | - All tracking calls become no-ops |
| 176 | - No external dependencies in tests |
| 177 | |
| 178 | ## References |
| 179 | |
| 180 | - **analytics-patterns.md** - Protocol architecture and best practices |
| 181 | - **templates/** - All template files |