# Real check: email open and click tracking

The tests use a fake Gmail, so opening and clicking a real tracked email has to be checked once
(HUB-170). Run it on staging once IT has set up a tracking host.

## What you need

- A tracking host for one business, set up as in `deploy/README.md` ("Email open and click
  tracking"): DNS, certificate, and the proxy forwarding `/o/` and `/c/`.
  `https://<host>/c/x` must answer `404 Link not found` from the API.
- The business settings: `PATCH /api/v1/business/settings`
  `{ "trackingDomain": "<host>", "trackingEnabled": true }`.
- A connected Gmail mailbox (see `docs/gmail-real-send-check.md`), and a second address you can
  read in Gmail and another mail app.
- Database access.

## Steps

1. **Send.** `POST /api/v1/inbox/send` to the second address only, with a link:

   ```json
   {
     "to": ["<second address>"],
     "subject": "Tracking check <today>",
     "bodyHtml": "<p>Please look at <a href=\"https://www.admedia.com/\">our site</a>.</p>",
     "idempotencyKey": "tracking-check-0001"
   }
   ```

   Make sure the second address is a contact of the business.

2. **The email as received.** Wait a minute, then open it in Gmail (as the second address).
   Show original: the link points to `https://<host>/c/...` and there is an image
   `https://<host>/o/....gif`.

3. **The open.** Within a minute of opening:

   ```sql
   SELECT open_count, first_opened_at FROM email_messages WHERE id = '<message id>';
   SELECT type, machine, machine_reason, country, user_agent FROM email_events
   WHERE message_id = '<message id>';
   ```

   `open_count` is at least 1, with an `open` event that is not `machine` (Gmail fetches images
   through its image proxy, which counts as the reader). The contact's `lastEmailOpenedAt`
   (`GET /api/v1/contacts/<id>`) is set.

   Open it again a few times: each load is stored with `is_repeat = 1`, `last_opened_at` moves,
   `open_count` stays the same (a person's opens count once an hour).

4. **The click.** Click the link: the browser lands on https://www.admedia.com/ at once. A
   `click` event is stored with `pending = 1`; a few seconds later (the worker's
   `tracking.settle-click` job) it is `pending = 0`, `click_count` is 1 and the contact's
   `lastEmailClickedAt` is set. If a security scanner of the receiving side followed every link
   first, those clicks show as `machine` with `many_links_at_once`.

5. **A forged link.** Change one character of the token in the link's address and open it: the
   answer is `404 Link not found`, and nothing new is stored.

6. **Apple Mail (if available).** Open the email in Apple Mail with Mail Privacy Protection on:
   its open shows as `machine` with `apple_privacy_proxy` and does not change `open_count`.

7. **The sender's view.** Open the thread in the CRM (`GET /api/v1/inbox/threads/<id>`): the
   body has no `<host>` image or link, and no new open is stored.

8. **A reply.** Reply to the email from the second address (Gmail quotes it). After the next
   sync, open the thread in the CRM: the reply shows no `<host>` image or link either. Open the
   original email again as the second address: the open is stored as `machine` with
   `quoted_copy` (once the email is quoted in the thread, the pixel no longer tells who opened
   it).

## Result

Fill this in and attach a screenshot of step 2 to the ticket (HUB-170).

|                                                        |                     |
| ------------------------------------------------------ | ------------------- |
| Date, environment                                      |                     |
| Who ran it                                             |                     |
| Pixel and tracked link in the received email (2)?      |                     |
| Open recorded, not as a machine (3)?                   |                     |
| Click recorded and landed on the right page (4)?       |                     |
| Forged link refused (5)?                               |                     |
| Apple Mail open flagged as machine (6)?                |                     |
| Sender's CRM view without tracking, no new open (7)?   |                     |
| Reply shown without tracking, quoted open flagged (8)? |                     |
| Screenshot                                             | attached to HUB-170 |

Known limits: the sender opening their own email in Gmail's Sent folder loads the image through
Gmail's proxy too, and can't be told apart from the recipient (it counts once an hour at most).
A mail program that fetches images directly is recognised as the sender's when it uses an
address the sender is signed in to the CRM from (`machine_reason = 'sender'`). A scanner that
follows a single link with a browser's user agent, more than 10 seconds after sending, still
counts as a click.
