# Link every sale to the visit it came from (clicks.page)

clicks.page credits each payment to the visit that made it: its traffic source, campaign and landing page. Your job,
if you're an AI agent reading this: make sure every checkout this project creates carries the visitor's clicks.page id.

## What already works with no code
- Checkout links (Stripe Payment Links, Lemon Squeezy, Polar, Dodo, Creem) and Paddle.js: the tracker adds the id itself.
- Checkouts the page opens: the tracker reports "Checkout started" when a checkout link or form is used, window.open
  opens a checkout, or the site's own server replies with a checkout URL or id (fetch / XHR). The payment is then
  matched to that visit by the checkout's id (Stripe Checkout Session, Paddle transaction) or its timing.
- Buyers who come back to the site from the checkout.

## Make it exact: pass the visit id when the server creates the checkout
1. Browser: send the id with the request that starts checkout.

       const clicksId = window.clicks?.id?.() ?? null;   // "clk_…" (null if the tracker is blocked)
       await fetch("/api/checkout", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ plan, clicksId }) });

   A plain HTML form that posts to your server needs nothing: the tracker adds a hidden `clicks_id` field to
   checkout forms (read it with `formData.get("clicks_id")`).

2. Server: put it in the checkout's reference field.

   | Provider | Field |
   | --- | --- |
   | Stripe | `client_reference_id: clicksId` (or `metadata: { clicks_session: clicksId }`) |
   | Paddle | `custom_data: { clicks_session: clicksId }` |
   | Polar | `metadata: { clicks_session: clicksId }` |
   | Lemon Squeezy | `checkout_data: { custom: { clicks_session: clicksId } }` |
   | Dodo Payments | `metadata: { clicks_session: clicksId }` |
   | Creem | `metadata: { clicks_session: clicksId }` (or `request_id: clicksId`) |

   Stripe example (Node):

       const session = await stripe.checkout.sessions.create({
         mode: "payment", line_items, success_url, cancel_url,
         client_reference_id: body.clicksId ?? undefined,   // ← the one line
       });

   Leave the field out when there's no id; never block checkout on it.

3. Subscriptions: nothing more. Renewals are credited to the customer's first visit.

## Check it
Make a test purchase (or wait for the next sale), then open Revenue in the dashboard: the sale shows its source.
Questions: https://clicks.page/llms.txt
