Analytics tracking
Analytics tracking gives an application one SDK path to record views and product events against an analytics property. Applications can send events directly with Analytics.createEvent or use Tracking to add app lifecycle, screen, install, update, and push telemetry while keeping tracking failures separate from the host app. This page is part of Application and integration services; Application management and installations covers app resources, while OAuth, analytics, and push integration covers the related service APIs.
Event submission
The Analytics service is the low-level event interface. It sends a required property, event name, and URL or screen identifier, plus optional context that helps analytics interpret the event.
The object form is the preferred integration shape:
await analytics.createEvent({
propertyId: 'property-id',
name: 'checkout_completed',
url: 'app://ios/checkout',
engagementTime: 12,
props: ['plan', 'pro'],
interactive: true,
});
The main inputs are:
| Input | Purpose |
|---|---|
propertyId | Selects the analytics property that receives the event. |
name | Identifies the event. pageview and screen_view count as views; other names count as custom events. |
url | Identifies the page or app screen. |
domain, referrer, screenWidth, sessionHash, scrollDepth | Adds page, device, session, or engagement context when available. |
engagementTime | Reports engagement time in seconds. |
props | Sends custom string values as alternating key and value entries. |
userId, ip, userAgent | Overrides derived visitor data. These fields require API-key authentication with the analytics.write scope. |
interactive | Marks whether the event is a user interaction that ends a bounce. |
propertyId, name, and url are required. The service returns a promise for the accepted event response. Missing required inputs or a rejected request raise an AppwriteException, so direct callers can handle the failure as part of their request flow.
References: src/…/analytics.ts• L5-45 src/…/analytics.ts• L175-272
Automatic tracking
The Tracking helper is the application-facing layer for common telemetry. It receives an Analytics service and a property ID, then supplies a base URL for events that do not provide their own URL.
start() enables app lifecycle tracking and checks install state. It reports first_open once for a new native install and reports app_updated when the saved app version changes. These install events are not sent on the web. A failed install or update event remains pending for a later app start, while a send failure is logged instead of being passed to the host app.
The default lifecycle events are:
| Event | Meaning |
|---|---|
app_foregrounded | The app becomes active after being in the background. |
app_backgrounded | The app enters the background. Its engagementTime reports foreground time since the last resume. |
first_open | The first recorded launch of a native installation. |
app_updated | The first launch after the recorded app version changes, with previousVersion and version. |
Lifecycle tracking is enabled once per Tracking instance. Applications can stop it with disableAutoLifecycleEvents() when the helper should no longer listen for app state changes.
References: src/…/analytics-tracking.ts• L105-157 src/…/analytics-tracking.ts• L159-249 src/…/analytics-tracking.ts• L251-298
Manual event tracking
Tracking.track() provides a best-effort API for custom product telemetry. It accepts an event name and optional properties, URL, engagement time, and interaction flag, then converts property values to the flat string format expected by the analytics event service.
The helper supplies a platform-safe default URL: the current web origin on the web, or an app:// URL on native platforms. A caller can set a different base URL when constructing Tracking or resolve a particular event URL relative to that base.
Tracking failures are swallowed after a warning. This keeps analytics loss from interrupting the user flow or surfacing an exception through the application event path.
References: src/…/analytics-tracking.ts• L28-42 src/…/analytics-tracking.ts• L301-340
Screen tracking
Screen tracking records a screen_view event with the screen name in both the event properties and the event URL path. screenView() supports an optional screen class and custom properties for applications that report screens directly.
React Navigation applications can use trackNavigation() to connect route changes to the same event stream. The helper reports the initial route and later reports only when the selected screen name changes. Applications can provide a screen-name mapper to skip or rename routes, and can keep their own navigation callbacks through the helper options.
References: src/…/analytics-tracking.ts• L44-47 src/…/analytics-tracking.ts• L342-355 src/…/analytics-tracking.ts• L384-425
Push event tracking
Push tracking links notification delivery with user action. Applications call pushReceived() from their push handler and call pushOpened() when the user taps the notification.
Both helpers can attach a message ID and campaign name. A received event is non-interactive, while an opened event is interactive. Delivery is best effort because the operating system may not run application code when a notification arrives while the app is closed.
References: src/…/analytics-tracking.ts• L49-53 src/…/analytics-tracking.ts• L357-380
Tracking flow
The tracking flow combines application events with shared context before the Analytics service submits one event request.
flowchart LR
A[Application action] --> B{Tracking helper}
B -->|lifecycle, install, screen, push, or custom event| C[Event name and context]
C --> D[Analytics.createEvent]
D --> E[Analytics property]
D -->|accepted| F[Resolved response]
D -->|rejected| G[Handled tracking failure]
Direct Analytics.createEvent calls expose the service response to the application. Tracking calls send in the background and treats failure as telemetry loss rather than an application error.
References: src/…/analytics.ts• L30-45 src/…/analytics-tracking.ts• L301-340
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the event property names and flat
propscontract aligned betweenTrackingandAnalytics.createEvent. - Preserve the distinction between interactive user events and non-interactive lifecycle, push-received, and background events.
- Keep install marker and pending-event behavior aligned with version changes so retries do not lose or duplicate the install state decision.
- Keep route-name filtering and callback order aligned with the React Navigation integration contract.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
propertyId, name, or url is missing | The Analytics service rejects the request with an AppwriteException. | Supply all three required event inputs. |
| A tracking request is rejected or the network fails | Direct callers receive a rejected promise; Tracking logs the failure and continues the app flow. | Handle direct service errors and use Tracking for best-effort telemetry. |
| A notification arrives while the app is not running | The app may not execute pushReceived(), so receipt data is absent. | Treat push receipt tracking as best effort and track pushOpened() when the app handles the tap. |
| A native app has no native cookie module | Native install and update events cannot use the app version or session state. | Use the native SDK module for install/update tracking; do not expect these events in web or Expo Go contexts. |
Key files
| File | Why to read it |
|---|---|
| src/…/analytics.ts | Defines the event contract, required inputs, optional telemetry context, and accepted response path. |
| src/…/analytics-tracking.ts | Defines automatic lifecycle, install, screen, push, and manual tracking behavior for applications. |