# Google Analytics Server Events

## Objective

Report confirmed outcomes to Google Analytics when browser tracking cannot be trusted.

Server-side delivery of completed conversions to Google Analytics, joined to the browser session and deduplicated against it.

## 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. Identify the outcomes the browser cannot see or reports unreliably — a payment confirmed by a webhook, a subscription that renewed, a signup completed after a redirect, anything blocked by an ad blocker — and send those from the server.
2. Capture the analytics client identifier from the browser when the visit begins, store it against the session or the record, and send it with the server event so the conversion joins the same session and campaign rather than appearing as a new anonymous visit.
3. Generate a stable event identifier at the moment the outcome happens and use it for both the browser and the server send, so the provider can collapse the pair into one event. Assign each event type a primary origin and treat the other as the fallback.
4. Keep the measurement credential in server configuration only, never in a browser bundle, and never send an email address, a name, a payment detail, a health or financial attribute, or anything else the provider's policies prohibit. Send an opaque internal identifier if a user must be identified.
5. Queue every send through the app's existing background processing so the user's transaction commits and returns without waiting on the provider. This entry owns server-side delivery only; if the app has a separate brief for browser tracking, that one owns page views and in-page interactions.

## 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

- Without the browser's client identifier the server event lands as a brand-new session with no campaign attribution, which makes the conversion useless for measuring acquisition. Capture and store it early, and record what happens when it is absent.
- The same conversion sent from both the browser and the server double-counts unless both carry the same stable event identifier. Generate that identifier once, at the outcome, not independently on each side.
- Session and campaign context is only legitimately available for a visit the app actually observed. Do not fabricate a source, and do not reuse a stale identifier from a previous visitor on a shared device.
- Sending prohibited values — email addresses, names, payment details, or sensitive category attributes — breaches the provider's terms and can have the property's data deleted. Enforce an allowed parameter list rather than passing the record through.
- Server sends must not sit on the request path. Queue them, so a slow or unreachable provider cannot delay a checkout or leave a transaction half-finished.
- Events accepted at the endpoint can still be discarded downstream for a malformed or unknown parameter, often with no error returned. Validate against the configured event and parameter schema before sending, and verify in the provider's own debug view.
- Events arriving long after the fact may fall outside the provider's accepted time window and be dropped. Send promptly, and set expectations for anything replayed from a backlog.
- Rate limits and outages need backoff with jitter and a bounded queue. Failing telemetry must never fail or retry the underlying business transaction.

## 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

- [ ] Outcomes the browser cannot reliably observe are reported from the server.
- [ ] Server events carry the stored client identifier and join the originating session and campaign where one was observed.
- [ ] Browser and server sends of the same conversion share one stable event identifier and are counted once.
- [ ] No prohibited or personally identifying value is sent, enforced by an allowed parameter list.
- [ ] Sends are queued off the request path and never delay or fail the user's transaction.
- [ ] Credentials exist only in server configuration, and outages result in backoff and a bounded queue rather than lost transactions.
- [ ] 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.
