# Customer.io Event Forwarding

## Objective

Forward real product behaviour to Customer.io so campaigns fire on what users do.

A server-side pipeline that sends a defined catalogue of product events and profile attributes to Customer.io under a stable identity.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Write down the event catalogue before writing any code: a small fixed set of names in one tense and one convention, each with a documented property list. An open-ended stream of ad-hoc names is unusable for anyone building a campaign.
2. Emit events from the server after the underlying change has committed, through the app's existing background-job system. Do not emit from the browser, where events are lost to ad blockers, duplicated by reloads, and trivially forged.
3. Key every event and every profile on the app's own immutable user identifier rather than the email address, so changing an email moves the profile instead of creating a second one.
4. Separate the two payload shapes deliberately: durable profile attributes such as plan, workspace, and lifecycle stage belong on the profile, and per-occurrence detail belongs on the event. Do not accumulate one-off transactional values as profile attributes.
5. Where a contact-and-list sync brief is also applied to this app, that feature owns identity and subscription state. This one owns behaviour only and must never write subscription status, or the two will fight over the same contact.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- Anonymous activity captured before signup must be attached to the real profile exactly once. A merge that replays those events as new ones double-counts every campaign trigger built on them.
- Retried and delayed jobs arrive out of order. Send each event's own occurrence timestamp rather than letting the provider stamp receipt time, or a campaign reacts to something that happened hours ago as if it were new.
- Data the app is not permitted to share — payment details, message bodies, identity documents, anything under a processing restriction — must be excluded by an allowlist of properties, never by a blocklist that the next new field escapes.
- Renaming an event breaks every campaign already listening for it. Treat names as a public contract: deprecate rather than rename, and emit both during any transition.
- Rate limits and provider outages must apply backoff and hold events in the queue. Dropping events on a rate-limit response leaves campaigns permanently missing a step for those users.
- A user deleted in the app must be deleted or suppressed at the provider too, or campaigns keep mailing someone who left.
- A burst from a bulk import, a backfill, or a runaway loop must be throttled and capped, so one job cannot exhaust the quota that every other event depends on.
- Development, staging, and seeded test activity must never reach the production workspace.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] A documented event catalogue exists and every forwarded event conforms to it.
- [ ] Events are emitted server-side after commit, through the existing background-job system.
- [ ] Profiles are keyed on a stable internal identifier that survives an email change.
- [ ] Anonymous pre-signup activity is attached to the account once, with no duplicated events.
- [ ] Event properties are assembled from an allowlist and contain no restricted personal data.
- [ ] Rate limiting and provider downtime delay events rather than discarding them.
- [ ] Account deletion in the app propagates to the provider.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
