# Tracking lifecycle

import { Mermaid } from "zudoku/mermaid";

## Three ways a tracking starts

| Kind | When | Starting status | `driverHasApp` |
| --- | --- | --- | --- |
| **Regular** | The phone number belongs to a Load Market driver app user | `request` - "Pending driver's approval" | `true` |
| **Pending installation** | The driver does not have the app yet | `request` - "Pending installation" | `false` |
| **Scheduled** | You sent `schedule` instead of `expDate` | `scheduled` - "Scheduled tracking" | depends on the phone |

- **Regular**: the driver gets a push notification and accepts (`active`) or rejects (`cancel`).
- **Pending installation**: the driver gets an SMS invitation to install the app. When they install
  it and sign in with that phone number, the tracking becomes regular (status `request`) with the
  **same `trackingId`**. Until then there is no position, no `publicLink` and no webhook. A pending
  installation tracking item the driver never accepts is removed by the server after its tracking
  time runs out, and it never appears in history.
- **Scheduled**: nothing happens until `schedule.start`. Then the driver is invited and the tracking
  continues as regular or pending installation. It expires at `schedule.end`.

The `status.value` of a pending installation tracking is `request`, the same as a regular one - tell
them apart with `driverHasApp` (`false` means pending installation), not with `status.description`.

`stop-tracking` does not apply to pending installation or scheduled tracking items (it answers
`TRACKING_NOT_FOUND`) - use `delete-tracking` to remove them.

A tracking can also follow the truck's ELD instead of, or together with, the driver's phone
(`trackingSource` `eld` or `hybrid`). It starts as `requestEld`, or `scheduledEld` with a `schedule`, and
waits for the carrier's approval - see [ELD and Hybrid tracking](/eld-tracking).

## Status values

`status` is always an object: `{ "value": "active", "description": "Active" }`. Branch on `value`.
The `description` is the text shown in the Load Market web app.

New status values may be added over time. Handle an unknown value gracefully and show
`status.description`.

| `value` | `description` | Meaning |
| --- | --- | --- |
| `request` | Pending driver's approval | The driver has the app and has not answered yet |
| `request` | Pending installation | The driver has not installed the app yet |
| `scheduled` | Scheduled tracking | Waiting for `schedule.start` |
| `active` | Active | The driver shares the location |
| `stop` | Paused by the driver | The driver paused sharing in the app |
| `cancel` | Rejected by driver | The driver declined the request |
| `done` | Tracking finished | Stopped with `stop-tracking` or in the Load Market web app, or expired |

### ELD and Hybrid statuses

ELD and Hybrid tracking items have statuses of their own. See
[ELD and Hybrid tracking](/eld-tracking#statuses) for how they change.

| `value` | `description` | Meaning |
| --- | --- | --- |
| `requestEld` | Pending ELD admin approval | Waiting for the carrier (a hybrid also for the driver) |
| `scheduledEld` | Scheduled ELD | Waiting for `schedule.start` |
| `activeEld` | Active ELD | The location is shared |
| `stopEld` | Paused by the driver (ELD continuing) | Hybrid: the driver paused the phone, the truck's ELD keeps reporting |
| `cancelEld` | Rejected by ELD admin | The carrier refused (a hybrid once the driver declined too) |
| `doneEld` | ELD tracking finished | Stopped with `stop-tracking` or in the Load Market web app, or expired |

### Derived statuses

These can replace `active` while the condition lasts and switch back to `active` on their own. Only
one status is reported at a time. They never replace `activeEld`.

| `value` | `description` | Appears when |
| --- | --- | --- |
| `notMoving` | Not moving | The tracking is active, but the truck has not been moving for a while |
| `waiting` | Waiting for an update | The tracking is active, but no new position has arrived for a while, for example because the phone is offline or has no coverage |
| `settings` | One of: "Driver's device is in Power Save mode, this might disable GPS tracking." / "Driver's location permission is not set to ALWAYS." / "Driver's location accuracy is not set to PRECISE." | The tracking is active, but a phone setting on the driver's device, such as power-save mode, location permission or precise location, may stop tracking. A location permission or precise-location issue is no longer reported once live positions from the driver's phone show that the setting was turned back on, until the phone reports its settings again |

These two can appear while the tracking is `request`, `active`, `stop` or `stopEld`:

| `value` | `description` | Appears when |
| --- | --- | --- |
| `noAccount` | The driver deleted account | The driver deleted the Load Market account |
| `uninstallAccount` | The driver uninstalled the app | The driver removed the app and has not reinstalled it |

After `noAccount` or `uninstallAccount` the tracking sends no more webhooks. Ask the driver to
reinstall and create a new tracking.

## Transitions

<Mermaid
  chart={`
stateDiagram-v2
    direction LR
    [*] --> scheduled: create with schedule
    [*] --> pending: create, driver has no app
    [*] --> request: create, driver has the app
    scheduled --> request: schedule.start
    scheduled --> pending: schedule.start, no app
    pending --> request: driver installs the app
    pending --> [*]: delete-tracking, or tracking time runs out
    request --> active: driver accepts
    request --> cancel: driver rejects
    active --> stop: driver pauses
    stop --> active: driver resumes
    active --> done: stop-tracking or expDate
    stop --> done: stop-tracking or expDate
    request --> done: stop-tracking or expDate
    cancel --> done: stop-tracking or expDate
    done --> [*]
`}
/>

`pending` in the diagram is status `request` with `driverHasApp: false` (description "Pending
installation"). The ELD and Hybrid transitions are on
[ELD and Hybrid tracking](/eld-tracking#statuses).

Each transition the driver, the carrier or the schedule makes is also a [webhook event](/webhooks#events):
`driver.installed` (pending to request), `tracking.started` (`schedule.start`), `driver.accepted`,
`driver.rejected`, `driver.paused`, `driver.resumed` and `tracking.finished`.

## Driver position

`driverCoords` is filled only while the tracking is live: stored status `active` or `stop` (the
derived `notMoving`, `waiting` and `settings` come from `active`, so they carry a position too), or
`activeEld` or `stopEld` for ELD and Hybrid. It is `null` for `request`, `cancel`, `done`, pending
installation and scheduled tracking items, and for `requestEld`, `scheduledEld`, `cancelEld` and
`doneEld` - on `get-tracking`, in webhooks and in list items. History items always have
`driverCoords: null`.

The reason: positions are stored per driver, not per tracking. Outside the live window the driver's
newest position can belong to another load. For the same reason `driverCoords` of a mobile tracking stays
`null` after the driver accepts, until the phone sends its first position for this tracking.

[Potential Fraud Alerts](/fraud-alerts) (`fraudAlerts`) follow the same window for the same reason:
they describe the driver's phone, not one load.

## Expiration and history

- Every tracking has an expiration date (`expDate`, or `schedule.end`), at most 10 days after it
  starts. When the tracking time runs out, the server finishes the tracking item automatically.
- `done` and `doneEld` tracking items are listed by `GET /get-all-tracking-items?type=history` and
  stay readable with `get-tracking`. History keeps finished tracking items for a limited time only, so
  sync what you need regularly - see [Daily history sync](/guides/daily-history-sync).
- `delete-tracking` removes a tracking at any time.
