Android
MetaRouter Android SDK
Capture behavioral data directly from your Android app and route it through MetaRouter to any destination — without juggling multiple vendor SDKs.
Why use the Mobile SDK?
One integration, unlimited destinations
Stop embedding separate SDKs for analytics, marketing, and data warehouses. Collect once, route everywhere through MetaRouter.
Update routing without app releases
Add or remove destinations server-side. No code changes, no app store review cycles.
Reliable delivery, even offline
Events are queued locally and delivered automatically when connectivity returns. Built-in retry logic handles transient failures.
Automatic identity resolution
Track users from first app open through sign-up and beyond. The SDK manages anonymous IDs, persists identity across sessions, and connects pre-login activity to known users.
Privacy-ready by design
Built-in support for advertising ID consent, easy opt-out handling, and clean user data reset for GDPR/CCPA compliance.
Lightweight footprint
No bloated dependencies. Minimal impact on app size and startup time.
Quick start
1) Add the SDK
Add JitPack to your project's settings.gradle.kts:
dependencyResolutionManagement {
repositories {
maven { url = uri("https://jitpack.io") }
}
}Then add the dependency in your module’s build.gradle.kts:
dependencies {
implementation("com.github.metarouterio:android-sdk:1.0.1")
}2) Initialize
Add this to your Application.onCreate():
val analytics = MetaRouter.Analytics.initialize(
context = applicationContext,
options = InitOptions(
writeKey = "YOUR_WRITE_KEY",
ingestionHost = "https://YOUR_CLUSTER.YOUR_SITE.com"
)
)Your writeKey and ingestionHost are available in the MetaRouter dashboard.
3) Start tracking
// Track user actions
analytics.track(
"Product Viewed",
"sku" to "SHOE-123",
"price" to 89.99
)
// Identify known users
analytics.identify(
"user-456",
"email" to "[email protected]",
"plan" to "premium"
)
// Track screen views
analytics.screen(
"Product Detail",
"category" to "Footwear"
)Events are batched and delivered automatically. No additional setup required.
Core capabilities
Event tracking
| Method | Purpose |
|---|---|
track(event, properties) | Capture user actions — purchases, clicks, feature usage |
screen(name, properties) | Record screen views for navigation analytics |
page(name, properties) | Record page views (web semantics, if applicable) |
User identity
| Method | Purpose |
|---|---|
identify(userId, traits) | Associate events with a known user |
group(groupId, traits) | Associate users with a company, team, or account |
alias(newUserId) | Link anonymous activity to a newly identified user |
getAnonymousId() | Returns the anonymous ID the SDK assigned to this device |
Lifecycle & privacy
| Method | Purpose |
|---|---|
flush() | Send queued events immediately |
reset() | Clear all user data (use on logout) |
setAdvertisingId(id) | Set GAID for attribution (with user consent) |
clearAdvertisingId() | Remove GAID when user opts out |
Identity that just works
The SDK automatically handles the complexity of user identity:
Before login — Users are assigned a stable anonymous ID
On login — Call identify() to attach a known user ID
Across sessions — Identity persists through app restarts
On logout — Call reset() to clear everything and start fresh
Connect anonymous browsing to authenticated users with alias():
// User browses anonymously, then signs up
analytics.track("Product Viewed", "sku" to "ABC") // tracked as anonymous
// User creates account
analytics.alias("new-user-789") // links anonymous → known
analytics.identify("new-user-789", "email" to "[email protected]")
// Full journey is now connected in your downstream toolsConfiguration Options
Use InitOptions to configure delivery cadence, logging, in-memory buffering, and disk-backed persistence.
val analytics = MetaRouter.Analytics.initialize(
context = applicationContext,
options = InitOptions(
writeKey = "YOUR_WRITE_KEY", // Required
ingestionHost = "https://YOUR_CLUSTER.mr-in.com", // Required
flushIntervalSeconds = 10, // Optional (default: 10)
debug = false, // Optional (default: false)
maxQueueEvents = 2000, // Optional (default: 2000)
maxDiskEvents = 10000 // Optional (default: 10000)
)
)| Option | Required | Default | Description |
|---|---|---|---|
writeKey | Yes | — | Your MetaRouter write key. Must not be empty. |
ingestionHost | Yes | — | Your MetaRouter ingestion endpoint. Must be a valid http/https URL with no trailing slash. |
flushIntervalSeconds | No | 10 | How often the SDK attempts to send queued events. Must be greater than 0 (throws IllegalArgumentException otherwise). |
debug | No | false | Enables verbose MetaRouter logcat output. Can also be toggled at runtime via analytics.enableDebugLogging(). |
maxQueueEvents | No | 2000 | Maximum events held in the in-memory queue. Must be greater than 0. Queue is also bounded by a 5 MB byte cap. |
maxDiskEvents | No | 10000 | Maximum unsent events retained on disk for crash safety and offline recovery. Set to 0 to disable disk persistence. Negative values are rejected. |
Notes
- Disabling disk persistence —
maxDiskEvents = 0puts the queue in pure in-memory ring buffer mode. Oldest events are dropped whenmaxQueueEvents(or the 5 MB byte cap) is hit, and events will not survive process kill. Use for privacy-constrained or ephemeral environments. - Snapshot location — Disk snapshots are stored under
noBackupFilesDir(excluded from Android Auto Backup) and are cleared onreset()or after successful rehydration on next launch.
Advertising & attribution
For ad attribution, you can include the Google Advertising ID (GAID):
// Only after obtaining user consent
analytics.setAdvertisingId(advertisingId)
// When user opts out
analytics.clearAdvertisingId()Privacy note: Always obtain explicit consent before collecting advertising IDs. See the GitHub docs for implementation details:
https://github.com/metarouterio/android-sdk#advertising-id-gaid
Requirements
| Component | Version |
|---|---|
| Android | API 23+ (6.0 Marshmallow) |
| Kotlin | 2.0+ |
Updated 3 days ago