|
| 1 | +#Sentry Addon for Analytiks |
| 2 | + |
| 3 | +Integration of[Sentry](https://sentry.io/) error tracking and performance monitoring with the Analytiks library. |
| 4 | + |
| 5 | +##Features |
| 6 | + |
| 7 | +- ✅**Event Tracking**: Track custom events as Sentry breadcrumbs |
| 8 | +- ✅**User Identification**: Associate events with specific users |
| 9 | +- ✅**User Properties**: Set user email, username, IP address, and custom data |
| 10 | +- ✅**Screen Tracking**: Track screen views |
| 11 | +- ✅**Error Context**: Enrich error reports with analytics events |
| 12 | +- ✅**Performance Monitoring**: Leverage Sentry's performance tracking capabilities |
| 13 | + |
| 14 | +##Installation |
| 15 | + |
| 16 | +###Using BOM (Recommended) |
| 17 | + |
| 18 | +```gradle |
| 19 | +dependencies { |
| 20 | + implementation platform('io.github.aminekarimii:analytiks-bom:1.4.0') |
| 21 | + implementation 'io.github.aminekarimii:analytiks-addon-sentry' |
| 22 | +
|
| 23 | + // Sentry Android SDK (if not already included) |
| 24 | + implementation 'io.sentry:sentry-android:7.0.0' |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +###Manual Version |
| 29 | + |
| 30 | +```gradle |
| 31 | +dependencies { |
| 32 | + implementation 'io.github.aminekarimii:analytiks-core:1.4.0' |
| 33 | + implementation 'io.github.aminekarimii:analytiks:1.4.0' |
| 34 | + implementation 'io.github.aminekarimii:analytiks-addon-sentry:1.4.0' |
| 35 | +
|
| 36 | + // Sentry Android SDK |
| 37 | + implementation 'io.sentry:sentry-android:7.0.0' |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +##Setup |
| 42 | + |
| 43 | +###1. Initialize Sentry |
| 44 | + |
| 45 | +First, initialize Sentry in your`Application` class or`AndroidManifest.xml`: |
| 46 | + |
| 47 | +####Option A: In Application Class |
| 48 | + |
| 49 | +```kotlin |
| 50 | +classMyApplication :Application() { |
| 51 | +overridefunonCreate() { |
| 52 | +super.onCreate() |
| 53 | + |
| 54 | +SentryAndroid.init(this) { options-> |
| 55 | + options.dsn="YOUR_SENTRY_DSN" |
| 56 | + options.environment=if (BuildConfig.DEBUG)"development"else"production" |
| 57 | + options.tracesSampleRate=1.0 |
| 58 | + options.isEnableAutoSessionTracking=true |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +####Option B: In AndroidManifest.xml |
| 65 | + |
| 66 | +```xml |
| 67 | +<application> |
| 68 | + <meta-data |
| 69 | +android:name="io.sentry.dsn" |
| 70 | +android:value="YOUR_SENTRY_DSN" /> |
| 71 | + <meta-data |
| 72 | +android:name="io.sentry.environment" |
| 73 | +android:value="production" /> |
| 74 | +</application> |
| 75 | +``` |
| 76 | + |
| 77 | +###2. Add to Analytiks |
| 78 | + |
| 79 | +```kotlin |
| 80 | +classMainActivity :AppCompatActivity() { |
| 81 | +privatelateinitvar analytiks:Analytiks |
| 82 | + |
| 83 | +overridefunonCreate(savedInstanceState:Bundle?) { |
| 84 | +super.onCreate(savedInstanceState) |
| 85 | + |
| 86 | + analytiks=Analytiks.Builder() |
| 87 | + .addClient(SentryClient(context= applicationContext)) |
| 88 | + .addClient(/* other analytics clients*/) |
| 89 | + .build() |
| 90 | + |
| 91 | + analytiks.initialize(applicationContext) |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +##Usage |
| 97 | + |
| 98 | +###Track Events |
| 99 | + |
| 100 | +Events are tracked as Sentry breadcrumbs with INFO level: |
| 101 | + |
| 102 | +```kotlin |
| 103 | +// Simple event |
| 104 | +analytiks.logEvent("button_clicked") |
| 105 | + |
| 106 | +// Event with properties |
| 107 | +analytiks.logEvent( |
| 108 | + eventName="purchase_completed", |
| 109 | + properties=mapOf( |
| 110 | +"product_id" to"12345", |
| 111 | +"price" to29.99, |
| 112 | +"currency" to"USD" |
| 113 | + ) |
| 114 | +) |
| 115 | +``` |
| 116 | + |
| 117 | +###Identify Users |
| 118 | + |
| 119 | +```kotlin |
| 120 | +// Identify user |
| 121 | +analytiks.identify(userId="user_12345") |
| 122 | +``` |
| 123 | + |
| 124 | +###Set User Properties |
| 125 | + |
| 126 | +```kotlin |
| 127 | +// Set email |
| 128 | +analytiks.setUserProperty("email","user@example.com") |
| 129 | + |
| 130 | +// Set username |
| 131 | +analytiks.setUserProperty("username","john_doe") |
| 132 | + |
| 133 | +// Set IP address |
| 134 | +analytiks.setUserProperty("ip_address","192.168.1.1") |
| 135 | + |
| 136 | +// Set custom properties |
| 137 | +analytiks.setUserProperty("subscription_tier","premium") |
| 138 | +analytiks.setUserProperty("account_age_days",365) |
| 139 | +``` |
| 140 | + |
| 141 | +###Track Screens |
| 142 | + |
| 143 | +```kotlin |
| 144 | +// Track screen view |
| 145 | +analytiks.trackScreen( |
| 146 | + screenName="HomeScreen", |
| 147 | + properties=mapOf( |
| 148 | +"section" to"main", |
| 149 | +"tab" to"featured" |
| 150 | + ) |
| 151 | +) |
| 152 | +``` |
| 153 | + |
| 154 | +###Reset User Data |
| 155 | + |
| 156 | +```kotlin |
| 157 | +// Clear user data and breadcrumbs |
| 158 | +analytiks.reset() |
| 159 | +``` |
| 160 | + |
| 161 | +##How It Works |
| 162 | + |
| 163 | +###Event Tracking |
| 164 | +- Events are sent to Sentry as**breadcrumbs** with level`INFO` |
| 165 | +- Event properties are included in the breadcrumb data |
| 166 | +- Breadcrumbs provide context for errors and crashes |
| 167 | + |
| 168 | +###User Management |
| 169 | +- User ID is set using Sentry's user identification |
| 170 | +- Special properties are mapped to Sentry's User object: |
| 171 | +-`email` → User.email |
| 172 | +-`username` → User.username |
| 173 | +-`ip_address` /`ipaddress` → User.ipAddress |
| 174 | +- Custom properties are stored in User.data |
| 175 | + |
| 176 | +###Screen Tracking |
| 177 | +- Screen views are tracked as breadcrumbs with category "navigation" |
| 178 | +- Screen properties are included in the breadcrumb data |
| 179 | + |
| 180 | +##Benefits of Sentry Integration |
| 181 | + |
| 182 | +1.**Error Context**: Analytics events appear as breadcrumbs in error reports, helping you understand what users were doing before an error occurred |
| 183 | + |
| 184 | +2.**User Tracking**: Associate errors with specific users and their properties |
| 185 | + |
| 186 | +3.**Performance Monitoring**: Combine analytics with Sentry's performance tracking |
| 187 | + |
| 188 | +4.**Release Tracking**: Track analytics across different app versions |
| 189 | + |
| 190 | +5.**Environment Separation**: Separate development and production analytics |
| 191 | + |
| 192 | +##Example: Complete Setup |
| 193 | + |
| 194 | +```kotlin |
| 195 | +classMyApplication :Application() { |
| 196 | +privatelateinitvar analytiks:Analytiks |
| 197 | + |
| 198 | +overridefunonCreate() { |
| 199 | +super.onCreate() |
| 200 | + |
| 201 | +// Initialize Sentry |
| 202 | +SentryAndroid.init(this) { options-> |
| 203 | + options.dsn="YOUR_SENTRY_DSN" |
| 204 | + options.environment=if (BuildConfig.DEBUG)"development"else"production" |
| 205 | + options.tracesSampleRate=1.0 |
| 206 | + options.isEnableAutoSessionTracking=true |
| 207 | + options.isEnableUserInteractionTracing=true |
| 208 | + } |
| 209 | + |
| 210 | +// Initialize Analytiks with Sentry |
| 211 | + analytiks=Analytiks.Builder() |
| 212 | + .addClient(SentryClient(context=this)) |
| 213 | + .addClient(TimberAnalyticsClient())// For debug logging |
| 214 | + .build() |
| 215 | + |
| 216 | + analytiks.initialize(this) |
| 217 | + |
| 218 | +// Identify user |
| 219 | + analytiks.identify("user_12345") |
| 220 | + analytiks.setUserProperty("email","user@example.com") |
| 221 | + analytiks.setUserProperty("plan","premium") |
| 222 | + } |
| 223 | +} |
| 224 | +``` |
| 225 | + |
| 226 | +##Advanced Configuration |
| 227 | + |
| 228 | +###Custom Sentry Options |
| 229 | + |
| 230 | +You can configure Sentry with additional options before initializing Analytiks: |
| 231 | + |
| 232 | +```kotlin |
| 233 | +SentryAndroid.init(this) { options-> |
| 234 | + options.dsn="YOUR_SENTRY_DSN" |
| 235 | + options.environment="production" |
| 236 | + options.release="my-app@1.0.0" |
| 237 | + options.dist="1" |
| 238 | + |
| 239 | +// Performance monitoring |
| 240 | + options.tracesSampleRate=1.0 |
| 241 | + options.profilesSampleRate=1.0 |
| 242 | + |
| 243 | +// Session tracking |
| 244 | + options.isEnableAutoSessionTracking=true |
| 245 | + options.sessionTrackingIntervalMillis=30000 |
| 246 | + |
| 247 | +// User interaction tracking |
| 248 | + options.isEnableUserInteractionTracing=true |
| 249 | + options.isEnableUserInteractionBreadcrumbs=true |
| 250 | + |
| 251 | +// Network tracking |
| 252 | + options.isEnableNetworkEventBreadcrumbs=true |
| 253 | + |
| 254 | +// Before send callback |
| 255 | + options.beforeSend=SentryOptions.BeforeSendCallback { event, hint-> |
| 256 | +// Modify or filter events before sending |
| 257 | + event |
| 258 | + } |
| 259 | +} |
| 260 | +``` |
| 261 | + |
| 262 | +##Resources |
| 263 | + |
| 264 | +-[Sentry Android Documentation](https://docs.sentry.io/platforms/android/) |
| 265 | +-[Sentry Breadcrumbs](https://docs.sentry.io/platforms/android/enriching-events/breadcrumbs/) |
| 266 | +-[Sentry User Context](https://docs.sentry.io/platforms/android/enriching-events/identify-user/) |
| 267 | +-[Analytiks Core Documentation](../../analytiks-core/README.md) |
| 268 | + |
| 269 | +##Support |
| 270 | + |
| 271 | +For issues specific to the Sentry addon, please[open an issue](https://github.com/aminekarimii/analytiks/issues) on GitHub. |
| 272 | + |
| 273 | +For Sentry-specific questions, refer to the[Sentry documentation](https://docs.sentry.io/) or[Sentry support](https://sentry.io/support/). |
| 274 | + |