CDPGuidesSupport
SDKsClient

Android SDK (client)

Install the Ours Privacy Android SDK to capture privacy-first analytics in Android apps with event tracking and user identification.

The Ours Privacy Android SDK brings privacy-first analytics to your Android applications. It delivers a comprehensive feature set for event tracking, user profiles, and compliance without compromising performance.

Key features include:

  • Event Tracking (track): Record custom events with optional properties.
  • User Identification (identify): Associate events with a specific user ID.
  • Flush (flush): Send the queued events immediately instead of waiting for the next batch.
  • Reset (reset): Clear the queue and rotate the visitor ID, for example on logout.
  • Default Properties: Attach event, user, and consent properties to the custom events you track.
  • Opt In and Opt Out (optInTracking, optOutTracking): Gate sending on the consent you collected in your own UI.
  • Deep-link Attribution (trackDeepLink): Read UTM parameters and ad-platform click IDs off an inbound link.
  • Setting the Visitor ID (setVisitorId): Stitch an app user to the profile holding their web history.
  • Configuration: Project token, ingest URL, automatic lifecycle events, and an opt-out default.

Installation

Add the dependency to your app's build.gradle (use the latest version from Maven Central):

dependencies {
    implementation 'com.oursprivacy:oursprivacy-android:<latest>'
}

Make sure mavenCentral() is listed in your repositories block.

Required Permissions

Add these permissions to your AndroidManifest.xml:

<!-- Required for sending events -->
<uses-permission android:name="android.permission.INTERNET" />

<!-- Optional but recommended for network state awareness -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<!-- Optional for Bluetooth state tracking -->
<uses-permission android:name="android.permission.BLUETOOTH" />

Mobile lifecycle and screens

Automatic lifecycle tracking is off by default. Enable it when your app is ready to collect lifecycle events. The SDK reads your app version and build from its package metadata.

import com.oursprivacy.android.opmetrics.OursPrivacyAPI;
import com.oursprivacy.android.opmetrics.OursPrivacyInitOptions;

OursPrivacyAPI op = new OursPrivacyAPI(getApplicationContext());
op.initialize(
    "YOUR_API_TOKEN",
    OursPrivacyInitOptions.builder()
        .trackAutomaticEvents(true)
        .build()
);
EventWhen it is sentEvent-specific properties
$mobile_first_openFirst tracked app open for this installation and sourceNone
$mobile_app_openEach tracked foreground entryNone
$mobile_session_startFirst tracked open in a new sessionNone
$mobile_session_engagementA foreground-time checkpoint, screen change, or background transitionengagement_duration_ms as a positive integer; screen_name when known
$mobile_session_endWhen an expired session is observed, when possibleNone
$mobile_app_updateFirst tracked open after a known app version or build changesprevious_app_version and previous_app_build when known
$mobile_screen_viewYour app calls trackScreen() for a new visible screenscreen_name

The SDK keeps a session across a foreground return within 30 minutes and starts a new one at the timeout. A session is engaged after 10 accumulated foreground seconds. Session-end delivery is best effort because an app can terminate without another callback. The session ID sid is the session start in decimal milliseconds. Mobile Analytics metadata includes mobile_contract_version: 1, mobile_occurred_at, and available app_version and app_build event properties. Platform, OS, device, and SDK details are attached automatically.

An event's properties, including registered default event properties, can contain 200 keys and must stay below 1 MB. Keep it to 196 of your own keys and leave room in its size for all four mobile metadata fields. If adding metadata would exceed either limit, the SDK keeps your properties and sends the event without Mobile Analytics metadata. That event does not enter Mobile Analytics.

Call trackScreen() when your navigation code shows a stable destination:

op.trackScreen("Schedule");

Screen views are explicit. Activity transitions do not cover destinations in Compose or custom navigation, and this SDK version does not collect screen views automatically. Choose a fixed label such as Schedule, never a URL, route parameter, patient name, or screen content. trackScreen() accepts labels up to 80 characters that start with an ASCII letter and contain only ASCII letters, numbers, spaces, underscores, or hyphens. Leading or trailing whitespace is rejected. A parameterized label such as Patient 123 can pass the character check, so choose labels from a fixed list in your app.

Manual track() and trackScreen() work when automatic lifecycle tracking is off. A full optOutTracking() stops both and clears unsent events; the next opt-in starts a new session. Send standard healthcare events with the same names and properties as your other sources.

An enabled Android SDK also emits legacy $app_open, $ae_first_open, $ae_session, and $ae_updated events. These have different meanings from the $mobile_* events and do not supply screen views. $mobile_* events contain SDK lifecycle and device fields plus your stable screen label. They omit your registered default properties and deep-link attribution; manually tracked healthcare events keep those properties. Keep patient details and advertising IDs out of event names and screen labels.

Crash capture has its own trackAutomaticCrashes option and is off by default. To send $ae_crashed events with an exception reason, add .trackAutomaticCrashes(true) to your initialization builder; it works whether lifecycle tracking is on or off. Review exception text before enabling it because the reason may contain patient details. Full optOutTracking() and the DisableExceptionHandler manifest setting suppress crash capture.

Web-to-app Identity

When your app is opened by a link carrying ours_visitor_id, the SDK sets that visitor ID for subsequent events so they can connect to the same visitor profile as web activity. The same call reads supported UTM parameters and ad-platform click IDs off the link. Manual events tracked after the link can carry those attribution values; earlier events keep their own UTM context.

Hand the URL over with op.trackDeepLink(url), or pass it as initialURL in the init options and let the SDK parse it at startup. If your app already holds the visitor ID, from a WebView or a native bridge, set it directly with op.setVisitorId("550e8400-e29b-41d4-a716-446655440000").

Forward the opening URL. The SDK hooks nothing in the operating system, so your activity has to hand over the URL it was opened with, on first launch and while the app is already running:

// MainActivity
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    intent?.data?.let { op.trackDeepLink(it.toString()) }
}

override fun onNewIntent(intent: Intent) {
    super.onNewIntent(intent)
    intent.data?.let { op.trackDeepLink(it.toString()) }
}

Your activity also needs an <intent-filter> in AndroidManifest.xml with the VIEW action and the BROWSABLE category, or Android never delivers the link to your app at all:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="https" android:host="links.example.com" />
</intent-filter>

On Android, parsed UTM parameters and click IDs survive a full app restart. The visitor ID connects activity across sources, but each mobile event keeps the UTM context available when it was tracked. A later link does not change an earlier event's attribution.

Use the supported attribution fields on later custom events, and keep patient details out of the incoming link.

Mobile app attribution covers how app attribution works end to end, and when you need a mobile measurement partner alongside it.

Migrating from an earlier version

$deep_link_opened no longer includes the full URL as an event property. Update any downstream mapping that reads that URL.

Read More

For the full API, setup options, and release notes, see the Android SDK README on GitHub.

Compatibility

  • Android API 23+ (Android 6.0 and above) and Java 17
  • Kotlin 1.5+
  • Gradle 7.0+

Explore the API

Want to see what you can send? Build and send a test payload from the Track Events API reference.

Getting Help

For support or questions, contact support@oursprivacy.com.

Next Steps

How is this guide?

On this page