# Tracking and pixel dedup

> What to send with a hold so a sale can be traced to its source, and how to keep a browser pixel and a server event from counting twice

Source: https://rcms-tickets-test.web.app/docs/tracking

> **What exists today.** Capturing attribution on a hold and an order, tracking links, partner pages, the event id for deduplication, sales by source, and server-side conversions to Meta and GA4 are built. See [what exists and what does not](https://rcms-tickets-test.web.app/docs/status.md).

## Send where the buyer came from

Pass `attribution` on the hold. The checkout kit captures all of it for you; if you build your own, capture it on the page the buyer lands on, because they arrive somewhere other than checkout.

| Field | What it is |
| --- | --- |
| `utm_source`, `utm_medium`, `utm_campaign`, `utm_term`, `utm_content` | The visit's UTM tags. The flat fields are the **last touch**. |
| `first_touch` | The first visit, if you kept it (the kit keeps it 90 days), with the same fields. |
| `referrer`, `landing_page` | The page the visitor came from, and the first page of the visit on your site. |
| `fbclid`, `gclid`, `ttclid` | Ad click ids, kept apart from each other. |
| `fbp`, `fbc` | Meta's `_fbp` and `_fbc` cookies. |
| `ga_client_id`, `ga_session_id` | GA4's client id and session id. The client id is the end of the `_ga` cookie, `1234567890.1700000000`, without its `GA1.1.` start, which is refused. The session id is from the `_ga_<id>` cookie. |
| `client_ip`, `user_agent` | The buyer's, as your server saw them. |
| `ad_consent` | What your cookie banner said: `granted`, `denied` or `unknown`. |
| `page_url` | The checkout page's URL. |

```ts
const tracked = await tickets.holds.create({
  event,
  items: [{ ticket_type: ga, quantity: 1 }],
  attribution: {
    utm_source: 'meta',
    utm_campaign: 'onsale',
    fbclid: 'IwAR-docs-example',
    ad_consent: 'granted',
    page_url: 'https://example.com/tickets',
  },
});

const bought = await tickets.orders.create({
  hold: tracked.id,
  buyer: { name: 'Maria Delgado', email: 'maria@example.com' },
  payment: { provider: 'test', token: 'tok_success' },
});

console.log(bought.attribution?.last_touch?.utm_source, bought.attribution?.ad_consent); // meta granted
console.log(bought.tracking_event_id === bought.id); // true
```

Consent is yours to ask and ours to respect. `ad_consent: 'denied'` stops the kit storing tags, and stops any server-side conversion from being sent ([below](#sending-purchases-from-our-server)). If the banner's answer changes after the hold, send it again as `ad_consent` on `POST /v1/orders` (the kit does). It can only narrow: a `denied` there also takes these details off the order and the hold, and a `granted` never widens what the hold said.

With `denied` we store no IP, user agent, click id, `fbp`, `fbc` or GA id, and a page address keeps only its origin and path. Send `client_ip` anyway: the per-network caps on open holds count a keyed hash of it, which ends with the hold, and the address itself is not kept.

A marketing consent is a separate record. Send the address it was given from as `buyer.marketing.<channel>.ip` on the order. Without that it falls back to the hold's `client_ip`, which is empty for a buyer who said no to ad tracking. Only a yes keeps an address; a no is kept as a refusal with no IP.

## One sale, one count

A browser pixel and a server-side event for the same purchase must carry the **same event id**, or the ad platform counts the sale twice. The id is `order.tracking_event_id`. Use it as the event id of the browser's `Purchase`:

```ts
// In the browser, on your confirmation page, with the order your server sent:
fbq('track', 'Purchase', { value: (order.price.total.amount - (order.price.pass_through?.amount ?? 0)) / 100, currency: 'USD' }, { eventID: order.tracking_event_id });
```

- Fire the browser `Purchase` only from a confirmation of a real order, never from a test-mode order.
- Send the value from the order, not from the cart, and leave refund protection out: it is money held for someone else, and our server's event leaves it out too.
- The kit's `pixel.ts` does this and does not fire `Purchase` in test mode.

## Sending purchases from our server

Ad blockers and iOS hide the browser pixel. Set up Meta's Conversions API or GA4's Measurement Protocol and we send each paid order from our server, with the order's own event id, so the platform counts the browser's event and ours once.

```
PUT /v1/integrations/meta   { "pixel_id": "…", "access_token": "…", "test_event_code": "TEST12345" }
PUT /v1/integrations/ga4    { "measurement_id": "G-…", "api_secret": "…" }
POST /v1/integrations/meta/test      send a test purchase
GET  /v1/integrations/meta/deliveries   what was sent, and what was not, and why
```

A first save leaves the integration off, so you can send a test before any real purchase goes. Set `enabled: true` to start. An owner or admin (or a `full` key) sets them up, and changing a pixel or measurement id emails the owners. The token is sealed when it is saved and is **never shown again**, to anyone: the list says only `secret_set`. We call Meta and Google at addresses we choose; you cannot name a host.

**What is sent, and when**

- A `Purchase` when an order's money has landed (`order.paid`). An order that is still `pending` (a processor that captures later) sends nothing until it is captured. A comp, a free order and a test-mode order send nothing.
- **The value is what the buyer paid for the sale, less refund protection.** Protection is money held for someone else, so a $97.42 order with $9.00 of it sends $88.42, the same as the kit's browser `Purchase`. Tax and fees stay in the value. GA4, whose guide keeps tax apart, also gets `tax`.
- **Exchanges.** An exchange's top-up is not sent to either platform today. Both are told the first sale's value. Sending top-ups as their own Purchase is a later task.
- **Refunds.** `order.refunded` is a refund. GA4 gets a `refund` event for what went back, net of protection, so a part refund sends the part, cut down to what GA4 was told less earlier refunds. So after a top-up a whole refund takes back the first sale's value and no more: GA4's net for the sale is zero, not below it. Meta has no refund event and is sent none. A refund goes only after its sale is sent; if the sale is never sent, neither is the refund. An order wholly refunded before its first send sends nothing (`refunded`). A refund made as part of an exchange writes no `order.refunded`, and a chargeback is not a refund: neither reaches GA4, so GA4 stays high by those amounts. An order a chargeback took entirely before its first send sends nothing either.
- **Consent.** `granted` is sent, `denied` never is. `unknown` is sent only if you turn on the integration's `send_when_consent_unknown`: it is off by default, and turning it on is recorded in the audit trail. An order with no attribution, and a door, dashboard or guest-list sale, was never shown a banner and is never sent, whatever the switch says (`ad_consent_unknown`). GA4 also needs the buyer's own `ga_client_id`: without it nothing is sent, because GA4 would credit a stranger.
- **Meta** gets the SHA-256 of the email and phone, normalized as Meta documents, and the buyer's IP address, browser, `fbp` and `fbc` unhashed, as Meta asks. `action_source` is `website` when we have both the page's address and the browser, otherwise `other`. A marketing consent's address (`buyer.marketing.<channel>.ip`) is evidence for your records and is never sent.
- **GA4** gets no email, phone, name, address or IP: the visitor's `client_id`, the session, and the purchase. Its `value` is the same as Meta's and the browser's, so tax and fees are in it even though GA4's guide leaves tax out; `tax` is sent beside it.

**Delivery.** The first try is made at once. A try that fails is repeated after 1 minute, 5, 30, 2 hours, 5, 12 and 24 hours, then marked `failed`. An event older than a platform takes (Meta 7 days, GA4 72 hours) is failed, not sent late. Sending pauses if a platform **refuses the saved token**, **can't find the pixel with it** (code 100 with subcode 33, or 33), or if **we can't read the saved token**: the owners get an email for that pause, no further tries are made until a save fixes it, and then everything held back is sent, a page at a time. A setup problem is often fixed inside Meta, so any save lifts that one. There is one exception: Meta matches a server event to the browser's for 48 hours, so a held-back sale older than that is not sent if a browser may have sent it too (`fbp` is set), because it would count twice. Each purchase and each platform is one delivery, so a repeat never sends twice. The log keeps ids, status, a platform's error code and our own sentence, never a platform's words or a buyer's details, for 60 days. A try that can't finish counts as a try, so one bad row holds up no other.

**Test it first.** Meta: save the `test_event_code` from Events Manager's Test Events tab; only the test purchase uses it, and a test is refused without one, so it never reaches your real pixel. Real purchases always go to the real pixel. GA4: the test goes to Google's validation endpoint, which keeps it out of reports. It reports problems with the payload but **does not check your API secret**, and the real endpoint answers success even for a wrong one, so look for the first real purchase in GA4's Realtime report.

**Erasing a person** removes their deliveries from our log. A platform keeps what it was sent: Meta has no way to delete a conversion, and deleting a GA4 user needs your own Google sign-in, which we never hold. We send GA4 no name, email or phone.

## Reading where sales came from

Orders carry their attribution, and `GET /v1/reports/breakdown?dimension=source` groups sales by `source`, `medium`, `campaign`, `code`, `tracking_link`, `ticket_type`, `day`, `channel` or `comp_reason`, on the last touch or the first (`touch=first`).

Reports are added up from the event log in the background, so a sale shows there shortly after it is made.

A word nobody planned is bounded: a source, medium or campaign is cut to 64 bytes, and past a limit on how many a report can hold, new ones count together in an `(other)` row. The numbers still add up.

## Tracking links

`POST /v1/tracking_links` makes a UTM-tagged link to a page of yours. We never redirect or count a click: the link goes straight to your page, and your checkout sends its `rtl` parameter back as `attribution.tracking_link` (the kit and a hosted checkout link do this, so a `/c/` address works as a destination). An event's list includes the links made for every event.

## Partners and their pages

A partner is a person or a company (a radio station, a creator, a tourism board) that owns codes and tracking links. `POST /v1/partners` adds one, a code or a link names it with `partner`, and a sale through either is credited to it: the link's partner if the sale came through a link, else the code's. The `partner` dimension of the breakdown reads those.

Each partner can have one private page, `/p#<link>`, with only their own numbers. You make the link and turn it off.

```ts
const sam = await tickets.partners.create({ name: 'Sam Rivera', kind: 'influencer' });

const made = await tickets.partners.createPage(sam.id, {});
console.log(made.page.url?.startsWith('https://'), made.page.enabled); // true true

// The link is shown once. A read says it is on, and never shows it again.
const again = await tickets.partners.retrieve(sam.id);
console.log(again.page.url, again.page.enabled); // null true

// Turning it off ends that link. Making a new one later gives them a page again.
const off = await tickets.partners.revokePage(sam.id);
console.log(off.page.enabled); // false
```

What the page shows, for each event the partner has a code, a link or sales of their own in:

- **Tickets** and **revenue**: the sales credited to them, net of refunds and of money held for someone else (refund protection). Revenue is what buyers paid, with fees and tax: not ticket money alone, so don't work a commission out of it.
- **Comps**: free tickets given through them: comps, and an order that took no money (a 100% code, a $0 ticket type). Your own reports count those $0 orders as tickets, and the page as free.
- By day. A day's money is net of refunds, so the days add up to the revenue. A refund comes off a day that lost its tickets, else the latest sales before it.
- By code and by link: what each sold while they have owned it, whoever was credited, each link with the tagged address to copy (the 50 links that sold most). A link made for every event is in each event still selling, even before it has sold.
- What to share: only a code or link usable now. Stock isn't looked at.
- Nothing else. Never a buyer's name, email, phone or address, never an order, never another partner's numbers, and no commission or payout: nothing here defines one.

A sale is credited when it is made and stays credited. Move a code or link to another partner and the old partner keeps its past sales. The new one's row starts at the move, and shows no sale from before it. A later refund of an older sale can still come off that row's money, since a refund doesn't say which sale it undid.

Ended events stay. For a link made for every event, that's among your newest 200 events. The page holds 20, with the latest last day, and says when older ones may be missing.

Single-use codes and batch codes are one row, not a row each. That hides which code sold, not everything: if a partner hands their own code to one person, that code's row and its day show what that person did. Refreshing shows a sale to within about a minute, a refund as a falling number, and a price can hint at the ticket type. The page can't prevent that without hiding the totals.

If the reports ran out of room to keep this partner on its own (a very large number of partners on one event), the page says the totals may be short instead of showing a zero.

The link is the whole credential, so treat it like a password:

- We keep only a hash of it. The whole link is in the answer that made it and, if you email it, in that mail. While a mail is held or waiting to go, our outbox row keeps its body, link included, for up to 30 days. Once it sends, the row keeps no body.
- It travels after the `#`, which no server receives, and the page sends it in a `Partner-Token` header, so it is in no request log. A new link replaces the old one at once.
- It ends by itself after a year. A link that never existed, was replaced, was turned off or has ended all answer the same way (`partner_link_revoked`, and the page says "This link was turned off"), so a guess learns nothing.
- Asking for pages is rate limited per link and per network, per server instance. A browser or a proxy never keeps a page, and it is never indexed.
- The numbers are at most about a minute old, 5 minutes for a partner with over 1,000 codes. Past that it answers `202` with `retry_after` for a few seconds while new ones are put together, and the page at `/p` looks again by itself. If new numbers can't be put together, it answers the last ones, and `as_of` says how old they are. An open page refreshes about once a minute while it's on screen. A link that's replaced or turned off stops at once.
- `email_partner: true` emails the link to the partner (at most three times a day), only to the address on the partner at that moment. `page.emailed` says whether it went (a mail that is only held says false). If it did not, the link is still in the answer.
- Only a full API key or a signed-in owner, admin or staff member can make or turn off a link. A read key can see that a page is on, never the link.
