|
| 1 | +# Analytics Developer (Smart Router) |
| 2 | + |
| 3 | +## Purpose |
| 4 | +Context-aware routing to the Anytype iOS analytics system. Helps you add analytics events, track user routes, and maintain consistent analytics patterns. |
| 5 | + |
| 6 | +## When Auto-Activated |
| 7 | +- Working with analytics events or AnytypeAnalytics |
| 8 | +- Adding route tracking or event properties |
| 9 | +- Modifying `AnalyticsConstants.swift` or `AnytypeAnalytics+Events.swift` |
| 10 | +- Keywords: analytics, route tracking, logEvent, AnytypeAnalytics, AnalyticsConstants |
| 11 | + |
| 12 | +## 🚨 CRITICAL RULES (NEVER VIOLATE) |
| 13 | + |
| 14 | +1. **ALWAYS define route enums in AnalyticsConstants.swift** - Never hardcode route strings |
| 15 | +2. **ALWAYS use existing property keys** - Use `AnalyticsEventsPropertiesKey.*` |
| 16 | +3. **NEVER use string literals for routes** - Use typed enums with `.rawValue` |
| 17 | +4. **ALWAYS use `.compactMapValues { $0 }` for optional properties** - Removes nil values |
| 18 | +5. **ALWAYS track route context** - Know how users reached a feature |
| 19 | + |
| 20 | +## 📋 Quick Workflow |
| 21 | + |
| 22 | +### Adding New Analytics Event |
| 23 | + |
| 24 | +1. **Define route enum** (if needed): Add to `AnalyticsConstants.swift` |
| 25 | +2. **Add event method**: Add to `AnytypeAnalytics+Events.swift` |
| 26 | +3. **Use in code**: Call `AnytypeAnalytics.instance().logYourEvent(...)` |
| 27 | + |
| 28 | +### Adding Route Tracking to Existing Screen |
| 29 | + |
| 30 | +1. **Define route enum**: Add `YourFeatureRoute` to `AnalyticsConstants.swift` |
| 31 | +2. **Update data model**: Add `route: YourFeatureRoute?` parameter |
| 32 | +3. **Pass through hierarchy**: Coordinator → View → ViewModel |
| 33 | +4. **Update analytics call**: Pass route to existing log method |
| 34 | + |
| 35 | +## 🎯 Common Patterns |
| 36 | + |
| 37 | +### Pattern 1: Screen Analytics (onAppear) |
| 38 | + |
| 39 | +```swift |
| 40 | +// ViewModel |
| 41 | +final class HomeWidgetsViewModel { |
| 42 | + let route: HomeWidgetRoute? |
| 43 | + |
| 44 | + func onAppear() { |
| 45 | + AnytypeAnalytics.instance().logScreenWidget(route: route) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// Analytics method (AnytypeAnalytics+Events.swift) |
| 50 | +func logScreenWidget(route: HomeWidgetRoute?) { |
| 51 | + logEvent("ScreenWidget", withEventProperties: [ |
| 52 | + AnalyticsEventsPropertiesKey.route: route?.rawValue |
| 53 | + ].compactMapValues { $0 }) |
| 54 | +} |
| 55 | + |
| 56 | +// Route enum (AnalyticsConstants.swift) |
| 57 | +enum HomeWidgetRoute: String, Hashable, Codable { |
| 58 | + case home = "Home" |
| 59 | + case space = "Space" |
| 60 | + case appLaunch = "AppLaunch" |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +### Pattern 2: Button Click Analytics |
| 65 | + |
| 66 | +```swift |
| 67 | +// ViewModel |
| 68 | +func onTapShare() { |
| 69 | + AnytypeAnalytics.instance().logClickShare(type: .link, route: .settings) |
| 70 | + output?.onShareSelected() |
| 71 | +} |
| 72 | + |
| 73 | +// Analytics method |
| 74 | +func logClickShare(type: ShareType, route: ShareRoute) { |
| 75 | + logEvent("ClickShare", withEventProperties: [ |
| 76 | + AnalyticsEventsPropertiesKey.type: type.rawValue, |
| 77 | + AnalyticsEventsPropertiesKey.route: route.rawValue |
| 78 | + ]) |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### Pattern 3: Multi-Space Events |
| 83 | + |
| 84 | +```swift |
| 85 | +func logCreateObject(objectType: AnalyticsObjectType, spaceId: String, route: AnalyticsEventsRouteKind) { |
| 86 | + logEvent("CreateObject", spaceId: spaceId, withEventProperties: [ |
| 87 | + AnalyticsEventsPropertiesKey.objectType: objectType.analyticsId, |
| 88 | + AnalyticsEventsPropertiesKey.route: route.rawValue |
| 89 | + ]) |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +## 🗂️ Analytics File Structure |
| 94 | + |
| 95 | +### Key Files |
| 96 | +- **AnalyticsConstants.swift** - All route enums, property keys (400+ lines) |
| 97 | +- **AnytypeAnalytics+Events.swift** - All event methods (1,500+ lines) |
| 98 | +- **Converters/** - Domain type → analytics value converters |
| 99 | + |
| 100 | +### Route Enum Location |
| 101 | + |
| 102 | +**Always add to AnalyticsConstants.swift**, grouped by feature: |
| 103 | + |
| 104 | +```swift |
| 105 | +// Widget-related routes |
| 106 | +enum AnalyticsWidgetRoute: String { |
| 107 | + case addWidget = "AddWidget" |
| 108 | + case inner = "Inner" |
| 109 | +} |
| 110 | + |
| 111 | +enum HomeWidgetRoute: String, Hashable, Codable { |
| 112 | + case home = "Home" |
| 113 | + case space = "Space" |
| 114 | + case appLaunch = "AppLaunch" |
| 115 | +} |
| 116 | + |
| 117 | +// Screen navigation routes |
| 118 | +enum SettingsSpaceShareRoute: String { |
| 119 | + case settings = "Settings" |
| 120 | + case navigation = "Navigation" |
| 121 | + case chat = "Chat" |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +## 📈 Route Tracking Implementation |
| 126 | + |
| 127 | +### Step-by-Step: Adding Route to Screen |
| 128 | + |
| 129 | +**Example: Adding route tracking to ScreenWidget** |
| 130 | + |
| 131 | +1. **Define route enum** (AnalyticsConstants.swift): |
| 132 | +```swift |
| 133 | +enum HomeWidgetRoute: String, Hashable, Codable { |
| 134 | + case home = "Home" // Home button click |
| 135 | + case space = "Space" // Space selection |
| 136 | + case appLaunch = "AppLaunch" // App launch |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +2. **Update data model**: |
| 141 | +```swift |
| 142 | +struct HomeWidgetData: Hashable { |
| 143 | + let spaceId: String |
| 144 | + let route: HomeWidgetRoute? // Add route parameter |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +3. **Pass through view hierarchy**: |
| 149 | +```swift |
| 150 | +// Coordinator |
| 151 | +HomeWidgetsView(info: info, output: model, route: data.route) |
| 152 | + |
| 153 | +// View |
| 154 | +HomeWidgetsViewModel(info: info, output: output, route: route) |
| 155 | +``` |
| 156 | + |
| 157 | +4. **Update analytics call**: |
| 158 | +```swift |
| 159 | +// ViewModel |
| 160 | +func onAppear() { |
| 161 | + AnytypeAnalytics.instance().logScreenWidget(route: route) |
| 162 | +} |
| 163 | + |
| 164 | +// Analytics method |
| 165 | +func logScreenWidget(route: HomeWidgetRoute?) { |
| 166 | + logEvent("ScreenWidget", withEventProperties: [ |
| 167 | + AnalyticsEventsPropertiesKey.route: route?.rawValue |
| 168 | + ].compactMapValues { $0 }) |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +5. **Set route at navigation points**: |
| 173 | +```swift |
| 174 | +// Home button |
| 175 | +let data = HomeWidgetData(spaceId: spaceId, route: .home) |
| 176 | + |
| 177 | +// Space selection |
| 178 | +let data = HomeWidgetData(spaceId: spaceId, route: .space) |
| 179 | + |
| 180 | +// App launch |
| 181 | +let data = HomeWidgetData(spaceId: spaceId, route: .appLaunch) |
| 182 | +``` |
| 183 | + |
| 184 | +## 🔧 Common Property Keys |
| 185 | + |
| 186 | +Located in `AnalyticsEventsPropertiesKey`: |
| 187 | + |
| 188 | +| Key | Usage | Example | |
| 189 | +|-----|-------|---------| |
| 190 | +| `route` | Navigation context | `.home`, `.navigation`, `.widget` | |
| 191 | +| `type` | Primary classification | `.image`, `.video`, `.file` | |
| 192 | +| `objectType` | Object type ID | `type.analyticsType.analyticsId` | |
| 193 | +| `spaceId` | Space identifier | `document.spaceId` | |
| 194 | +| `count` | Quantity values | Number of items | |
| 195 | +| `format` | Data format | File format, date format | |
| 196 | + |
| 197 | +## ⚠️ Common Mistakes |
| 198 | + |
| 199 | +### ❌ Hardcoded Route Strings |
| 200 | +```swift |
| 201 | +// WRONG |
| 202 | +AnytypeAnalytics.instance().logScreenWidget(route: "Home") |
| 203 | + |
| 204 | +// CORRECT |
| 205 | +AnytypeAnalytics.instance().logScreenWidget(route: .home) |
| 206 | +``` |
| 207 | + |
| 208 | +### ❌ Route Enum in Wrong File |
| 209 | +```swift |
| 210 | +// WRONG - defined in feature file |
| 211 | +enum HomeWidgetRoute: String { |
| 212 | + case home = "Home" |
| 213 | +} |
| 214 | + |
| 215 | +// CORRECT - defined in AnalyticsConstants.swift |
| 216 | +``` |
| 217 | + |
| 218 | +### ❌ Missing compactMapValues |
| 219 | +```swift |
| 220 | +// WRONG - will include nil values as NSNull |
| 221 | +[AnalyticsEventsPropertiesKey.route: route?.rawValue] |
| 222 | + |
| 223 | +// CORRECT - removes nil values |
| 224 | +[AnalyticsEventsPropertiesKey.route: route?.rawValue].compactMapValues { $0 } |
| 225 | +``` |
| 226 | + |
| 227 | +### ❌ Using String Literals for Property Keys |
| 228 | +```swift |
| 229 | +// WRONG |
| 230 | +["route": route.rawValue] |
| 231 | + |
| 232 | +// CORRECT |
| 233 | +[AnalyticsEventsPropertiesKey.route: route.rawValue] |
| 234 | +``` |
| 235 | + |
| 236 | +## 🔍 Finding Existing Patterns |
| 237 | + |
| 238 | +```bash |
| 239 | +# Search for existing events |
| 240 | +rg "logEvent.*EventName" Anytype/Sources/Analytics/ |
| 241 | + |
| 242 | +# Find route enums |
| 243 | +rg "enum.*Route.*String" Anytype/Sources/Analytics/AnalyticsConstants.swift |
| 244 | + |
| 245 | +# Find property usage |
| 246 | +rg "AnalyticsEventsPropertiesKey\." Anytype/Sources/Analytics/ |
| 247 | + |
| 248 | +# Find screen analytics |
| 249 | +rg "func logScreen" Anytype/Sources/Analytics/AnytypeAnalytics+Events.swift |
| 250 | +``` |
| 251 | + |
| 252 | +## 📚 Complete Documentation |
| 253 | + |
| 254 | +**Full Guide**: `Anytype/Sources/PresentationLayer/Common/Analytics/ANALYTICS_PATTERNS.md` |
| 255 | + |
| 256 | +For comprehensive coverage of: |
| 257 | +- Analytics system architecture |
| 258 | +- All analytics patterns (user actions, screen navigation, content creation) |
| 259 | +- Route context tracking best practices |
| 260 | +- Platform-specific considerations |
| 261 | +- Testing analytics events |
| 262 | +- Migration patterns for adding parameters |
| 263 | +- Complete examples and code snippets |
| 264 | + |
| 265 | +## ✅ Workflow Checklist |
| 266 | + |
| 267 | +When adding analytics: |
| 268 | + |
| 269 | +- [ ] Route enum added to `AnalyticsConstants.swift` |
| 270 | +- [ ] Event method added to `AnytypeAnalytics+Events.swift` |
| 271 | +- [ ] Used existing property keys (`AnalyticsEventsPropertiesKey.*`) |
| 272 | +- [ ] Optional properties use `.compactMapValues { $0 }` |
| 273 | +- [ ] Route passed through view hierarchy (if tracking navigation) |
| 274 | +- [ ] No hardcoded strings for routes or property keys |
| 275 | +- [ ] Followed existing naming conventions (PascalCase events, camelCase properties) |
| 276 | + |
| 277 | +## 🔗 Related Skills & Docs |
| 278 | + |
| 279 | +- **ios-dev-guidelines** → `IOS_DEVELOPMENT_GUIDE.md` - MVVM/Coordinator patterns for passing analytics data |
| 280 | +- **code-generation-developer** → Understanding the build system |
| 281 | +- **design-system-developer** → Tracking UI interactions |
| 282 | + |
| 283 | +--- |
| 284 | + |
| 285 | +**Navigation**: This is a smart router. For deep patterns and examples, always refer to `ANALYTICS_PATTERNS.md`. |
0 commit comments