---
layout: article
title: Track a mobile app
description: Add Appwrite Analytics to an Android, Apple, Flutter, or React Native app. Track installs, updates, screen views, push notifications, time in the app, and your own events.
---

The Android, Apple, Flutter, and React Native SDKs each include a `Tracking` class. One call to `start()` records installs, updates, and when the app goes to the background and comes back. Connect `Tracking` to your navigation to record screen views, and call it from your push handler to see which notifications people open.

# Before you start

![Create property page with the Mobile type and the Flutter SDK selected](/images/docs/analytics/mobile-create.avif)

[Create a **Mobile** property](/docs/products/analytics/properties#create) and copy its property ID, which the Console shows next to the property name.

Register the app in the same project. The SDK sends your app's platform and package name or bundle ID with every request, and Appwrite rejects events from a platform with no app in the project with a `403` `general_unknown_origin` error.

1. In your project, open **Apps** and click **Add app**.
2. Choose your platform, such as **Android**, **Apple**, **Flutter**, or **React Native**.
3. Enter the package name or bundle ID, and finish the wizard.

Register each target separately, such as Android and iOS. To accept events only from your own app, also add its origin to the property's [allowed origins](/docs/products/analytics/properties#allowed-origins).

# Install the SDK

**Flutter**

```sh
flutter pub add appwrite
```

**Android**

Add the SDK to `app/build.gradle.kts`.

```kotlin
dependencies {
    implementation("io.appwrite:sdk-for-android:31.4.0")
}
```

**Apple**

In Xcode, select **File** > **Add Package Dependencies**, enter the package URL, and add the **Appwrite** library to your app target.

```text
https://github.com/appwrite/sdk-for-apple
```

**React Native**

```sh
npx expo install react-native-appwrite expo-file-system react-native-url-polyfill
```

Import `react-native-url-polyfill/auto` once, before any other SDK code runs.

# Start tracking

![Install tracking screen confirming that a screen_view event was received from the app](/images/docs/analytics/mobile-first-event.avif)

Create one `Tracking` instance for the whole app with your endpoint, project ID, and property ID, and call `start()` when the app launches. On Android, create it in your `Application` class and register that class in `AndroidManifest.xml` with `android:name=".MyApp"` on the `<application>` element. In React Native, pass the package name or bundle ID registered under **Apps** to `setPlatform()`.

```client-flutter
import 'package:appwrite/appwrite.dart';

final client = Client()
    .setEndpoint('https://<REGION>.cloud.appwrite.io/v1')
    .setProject('<PROJECT_ID>');

final tracking = Tracking(Analytics(client), '<PROPERTY_ID>');

void main() {
  tracking.start();
  runApp(const MyApp());
}
```
```client-android-kotlin
import android.app.Application
import io.appwrite.Client
import io.appwrite.Tracking
import io.appwrite.services.Analytics

class MyApp : Application() {
    lateinit var tracking: Tracking

    override fun onCreate() {
        super.onCreate()

        val client = Client(this)
            .setEndpoint("https://<REGION>.cloud.appwrite.io/v1")
            .setProject("<PROJECT_ID>")

        tracking = Tracking(Analytics(client), "<PROPERTY_ID>")
        tracking.start()
    }
}
```
```client-apple
import Appwrite
import SwiftUI

let tracking: Tracking = {
    let client = Client()
        .setEndpoint("https://<REGION>.cloud.appwrite.io/v1")
        .setProject("<PROJECT_ID>")

    return Tracking(Analytics(client), propertyId: "<PROPERTY_ID>")
}()

@main
struct MyApp: App {
    init() {
        tracking.start()
    }

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
```
```client-react-native
import 'react-native-url-polyfill/auto';
import { Analytics, Client, Tracking } from 'react-native-appwrite';

const client = new Client()
    .setEndpoint('https://<REGION>.cloud.appwrite.io/v1')
    .setProject('<PROJECT_ID>')
    .setPlatform('<PACKAGE_NAME_OR_BUNDLE_ID>');

export const tracking = new Tracking(new Analytics(client), '<PROPERTY_ID>');

tracking.start();
```

`start()` sends `first_open` after an install and `app_updated` after an update, and turns on the lifecycle events that measure time in the app. See [Install, lifecycle, and screen events](#automatic-events). Run the app. Within a few seconds, the install page shows **Event received**, and the events appear on the property's dashboard.

# Track screen views

![Screens card on a Mobile property with the top screens, next to the Humans vs bots card](/images/docs/analytics/mobile-screens.avif)

Send a `screen_view` each time a screen opens. Connect `Tracking` to your navigation, as shown below, so new screens are tracked with no extra code.

```client-flutter
MaterialApp(
  navigatorObservers: [TrackingObserver(tracking)],
  routes: {
    '/': (context) => const HomeScreen(),
    '/product': (context) => const ProductScreen(),
  },
);
```
```client-android-kotlin
val navController = rememberNavController()

LaunchedEffect(navController) {
    navController.currentBackStackEntryFlow.collect { entry ->
        entry.destination.route?.let { tracking.screenView(it) }
    }
}
```
```client-apple
struct ProductView: View {
    var body: some View {
        Text("Canvas backpack")
            .onAppear { tracking.screenView("product") }
    }
}
```
```client-react-native
import { NavigationContainer, useNavigationContainerRef } from '@react-navigation/native';
import { trackNavigation } from 'react-native-appwrite';

export default function App() {
    const navigationRef = useNavigationContainerRef();

    return (
        <NavigationContainer ref={navigationRef} {...trackNavigation(tracking, navigationRef)}>
            {/* your navigators */}
        </NavigationContainer>
    );
}
```

Each screen view records the screen name as the page path, so the **Screens** card shows `product` as `/product`. A `screen_view` counts as a view, the same as a `pageview` on the web. Screen views add to **Screen views** and **Screens/session**, and the first and last views of a session set its entry and exit screens.

To track a screen that isn't part of your navigation, such as a tab or a sheet, call `screenView()` directly.

In a React Native app that uses Expo Router, call `tracking.screenView(pathname)` when the value of `usePathname()` changes.

## Flutter route names

`TrackingObserver` reads each route's `RouteSettings.name` and skips routes without a name, such as most dialogs and bottom sheets. Give each `MaterialPageRoute` a name.

```client-flutter
Navigator.push(
  context,
  MaterialPageRoute(
    settings: const RouteSettings(name: '/settings'),
    builder: (context) => const SettingsScreen(),
  ),
);
```

To name screens another way, pass a `nameExtractor` to `TrackingObserver`. Return `null` from it to skip a route.

# Track push notifications

![Push notifications card with pushes received and opened for each message, next to the Installs card](/images/docs/analytics/mobile-installs.avif)

Call `pushReceived()` when a notification arrives and `pushOpened()` when someone taps it. Pass the Appwrite Messaging message ID, and the **Push notifications** card lists each message with how many were received and opened.

```client-flutter
tracking.pushReceived(messageId: '<MESSAGE_ID>');
tracking.pushOpened(messageId: '<MESSAGE_ID>');
```
```client-android-kotlin
tracking.pushReceived(messageId = "<MESSAGE_ID>")
tracking.pushOpened(messageId = "<MESSAGE_ID>")
```
```client-apple
tracking.pushReceived(messageId: "<MESSAGE_ID>")
tracking.pushOpened(messageId: "<MESSAGE_ID>")
```
```client-react-native
tracking.pushReceived({ messageId: '<MESSAGE_ID>' });
tracking.pushOpened({ messageId: '<MESSAGE_ID>' });
```

For a push sent from another service, pass `campaign` instead of `messageId`.

`push_opened` starts a session, but `push_received` doesn't, because a push can arrive while the app is closed. On iOS, and on Android while the app isn't running, notification code may not run when a push arrives, so **Received** can be lower than the number of pushes delivered.

# Track custom events

Send your own events with the screen they belong to as `url`, so each event is recorded under that path.

```client-flutter
tracking.event(
  'add_to_cart',
  url: '/product',
  props: {'product': 'canvas-backpack'},
);
```
```client-android-kotlin
tracking.event(
    "add_to_cart",
    url = "/product",
    props = mapOf("product" to "canvas-backpack"),
)
```
```client-apple
tracking.event("add_to_cart", url: "/product", props: ["product": "canvas-backpack"])
```
```client-react-native
tracking.track('add_to_cart', {
    url: '/product',
    props: { product: 'canvas-backpack' },
});
```

Prop values can be any type and are stored as text. For naming and limits, see [Track custom events](/docs/products/analytics/custom-events).

# Install, lifecycle, and screen events

| Event | When it's sent | Extra data |
| --- | --- | --- |
| `first_open` | The first time the app runs after an install | None |
| `app_updated` | The first time the app runs after an update | `previousVersion` and `version` props |
| `app_backgrounded` | The app moves to the background | The seconds the app was in the foreground |
| `app_foregrounded` | The app returns to the foreground | None |
| `screen_view` | A screen opens, from your navigation or `screenView()` | `screen` prop, and an optional `screenClass` prop |

To spot installs and updates, each SDK keeps a small marker with the app's version on the device. It holds no identifier. Reinstalling the app, or clearing its data, counts as a new install.

# Event URLs

![Technology card on the Platforms tab, splitting traffic between iOS and Android](/images/docs/analytics/mobile-platforms.avif)

Analytics records every event against a URL. In an app, `Tracking` uses `app://` and the platform name, such as `app://android` or `app://ios`, so a screen view of `product` on Android is recorded as `app://android/product`. The platform becomes the hostname, which the **Platforms** tab of the **Technology** card uses to split your traffic. Events without a screen, such as `first_open` and the lifecycle events, are recorded under `/`.

To record events under your own base URL, pass `url` to the `Tracking` constructor. In React Native, pass it in the options object, such as `{ url: 'https://example.com' }`. On Flutter web and React Native web, `Tracking` uses the page's origin.

# Errors

`Tracking` never throws and never interrupts your app. When an event fails, it's dropped, except `first_open` and `app_updated`, which are sent again on the next launch. To check your setup, call `createEvent` on the `Analytics` service once and print the error.

A `403` `general_unknown_origin` error names the package or bundle ID that needs an app under **Apps**. A `404` `analytics_property_not_found` error means the property ID is wrong or **Tracking** is off in the property's settings.
