App opens¶
Each time someone opens the app, the phone reports it to the data Worker, which keeps it in D1 and copies it to a Google Sheet. The sheet is a backup the family can read without any tools; D1 stays the record.
This is the first thing the app logs on its own. The session log still covers what a person has to watch for.
What counts as an open¶
- Opened the app: the app starts fresh.
- Came back to the app: it returns from the background, for example after the home screen or another app. Pulling down Control Center or a notification does not count, because the app never leaves the screen.
What is recorded¶
| Field | Example | Notes |
|---|---|---|
| Opened at | 2026-10-12 10:03 | The phone's clock. |
| Who | Tom | The phone's person when it is named in known-devices.ts, otherwise its account. |
| How | Opened the app | Or "Came back to the app". |
| Phone | 9f3a77c2 |
The start of a random id the phone makes once. Two phones on one account differ. |
| Device | ios 26.1 | Platform and its version. |
| App version | 1.170.0 | The same number TestFlight shows. |
| Received at | 2026-10-12 10:03 | When the Worker got it. Later than "Opened at" when the phone was offline. |
| Event id | 0c1e… |
Made by the phone, so a resend is not counted twice. |
Nothing about what is on screen is recorded here: which memories were shown, or what was tapped. That is the next step (the session log's "The app can record it" column).
Named phones¶
The Worker names a phone it knows by the first block of its install id, the
Phone column, in apps/api/src/known-devices.ts. An open from one of them
goes in Who under that person, whoever the phone is connected as:
| Phone | Who |
|---|---|
fa6ff4ff |
Thomas |
3b17278b |
Lisa |
22b4a4dd |
Tom |
So Thomas testing on his own phone while it is connected as Tom is counted
as Thomas. Any other phone keeps its account's name. D1 still records the
account (user_id); only the sheet's Who follows the phone. The Worker
writes the cell, so this works for every app version once the Worker is
deployed, and only for opens sent after that: rows already in the sheet stay
as they are. A phone reinstalled gets a new install id, which needs adding.
How it flows¶
- Phone (
apps/mobile/src/features/usage/):useAppOpenLogging, in the root layout, queues each open in AsyncStorage (app-opens-queue-v1), then sends the queue withPOST /app-opens. - An open goes with the session of the person the app is being used as (Settings > Who is using the app).
- A phone not connected to that person's memory bank cannot send. Its opens wait in the queue and go once it is connected with a code from Connect a phone. The queue keeps the newest 500.
- Offline, or with an expired session, opens wait the same way.
- Worker (
apps/api/src/routes/app-opens.ts): checks each open, stores it inapp_opens(migration 0009), and answers. Then, without making the phone wait, it sends the rows the sheet does not have yet (src/sheet-backup.ts). - Google Sheet: a small Apps Script web app
(
apps/api/sheet-backup/apps-script.gs) appends each row to the App opens tab, making the tab when missing. Quiz answers go the same way to the Quiz answers tab, and thumbs-down answers to Questions to fix (The quiz). It skips rows it already has and answers with the ids it holds; only those are markedsheet_backed_up_atin D1. A row the sheet missed goes again with the next open.
Setting up the Google Sheet¶
Do this once. Until it is done, opens are still stored in D1, and the sheet receives the backlog (up to 100 per open) once it is set up.
- Open the App Opens
sheet on the Senior Health shared drive. Rename its first tab to
App opens(the script writes to the tab with exactly that name, and makes one if it is missing). - Extensions > Apps Script. Replace what is there with the contents of
apps/api/sheet-backup/apps-script.gs, and save. - Make a secret, a long random string:
- In Apps Script, Project Settings (the gear) > Script Properties >
Add script property. Name
SECRET, value the string from step 3. - Deploy > New deployment. Type Web app. Execute as Me, who has
access Anyone. Deploy, allow the permissions it asks for, and copy
the Web app URL (it ends in
/exec).
"Anyone" means anyone with the URL can post to it; without the secret, the script refuses and writes nothing.
- Give both to the Worker. Each command asks for the value:
cd ~/Documents/GitHub/SilverApp/apps/api && npx wrangler secret put SHEET_WEBHOOK_URL --env production
cd ~/Documents/GitHub/SilverApp/apps/api && npx wrangler secret put SHEET_WEBHOOK_SECRET --env production
- In the sheet, File > Settings, set the time zone to the family's, so times read as local times.
After changing the script, deploy it again with Deploy > Manage deployments > Edit > New version. That keeps the same URL.
Deploying¶
The migration must land before the Worker that uses it, and the phone needs a new TestFlight build to start sending:
cd ~/Documents/GitHub/SilverApp && pnpm --filter @senior-health/api migrate:remote
cd ~/Documents/GitHub/SilverApp && pnpm deploy:api
cd ~/Documents/GitHub/SilverApp/apps/mobile && npx eas build --profile production --platform ios --non-interactive
Builds from before this change send nothing, and an open on them is not recorded anywhere.
Reading the opens without the sheet¶
Straight from D1, most recent first:
cd ~/Documents/GitHub/SilverApp/apps/api && npx wrangler d1 execute senior-health --remote --env production --command "SELECT o.opened_at, u.name, o.kind, substr(o.install_id, 1, 8) AS phone, o.app_version, o.sheet_backed_up_at FROM app_opens o JOIN users u ON u.id = o.user_id ORDER BY o.opened_at DESC LIMIT 50"
Rows with an empty sheet_backed_up_at have not reached the sheet yet.
Testing¶
apps/mobile/tests/app-opens.test.ts: the queue, sending with the right session, waiting while offline or not connected, and the hook's launch and return from the background.apps/api/tests/routes/app-opens-routes.test.ts: the route's checks and storage, and the backup marking only what the sheet confirms.apps/api/tests/sheet-backup.test.ts: what is posted to the sheet, and that a failing sheet never fails the request.
To try it end to end locally, run pnpm dev:api with the two values in
apps/api/.dev.vars (gitignored):