# Sales

How paid Chargebee invoices become Purchase events, how refunds change them, and how to tie each sale to the ad that brought it.

Each paid invoice on the connected site becomes one `Purchase` event worth what was collected, net of refunds. It counts in [campaign results](/guides/campaign-results) and [ad rules](/guides/ad-rules) once it is tied to a campaign.

## Tie a sale to its ad

Set the visitor id the [Zernio tag](/products/server-side-tracking) keeps in its `_zid` cookie in the subscription's `meta_data` (or the customer's, for one-off charges) as `zernio_visitor_id`; Zernio then adds the click ids of that visit.

You can also set the visit's `zrn`, `gclid`, `fbclid` and `utm_*` keys, and `landing_url` for the page it landed on.

```bash
curl -X POST "https://acme.chargebee.com/api/v2/subscriptions/SUBSCRIPTION_ID/update_for_items" \
  -u "$CHARGEBEE_API_KEY:" \
  --data-urlencode 'meta_data={"zernio_visitor_id":"ZID_COOKIE_VALUE","utm_campaign":"fall-sale"}'
```

A sale without any of these is still stored and counts in totals, but not on a campaign.

## Read the sales

Chargebee sales are events with source `chargebee`. Read them with `GET /v1/events?source=chargebee`, or per campaign in campaign results. On **Connections**, **View sales** on the account lists the latest ones with their campaign.

Each event's `eventId` is `chargebee:` followed by the Zernio account id and the Chargebee invoice id. The customer id is sent as the external id.

## Send sales to ad platforms

Off by default. Turn it on with **Send to ad platforms** in **View sales**, or:

```bash
curl -X PATCH "https://zernio.com/api/v1/accounts/ACCOUNT_ID/chargebee-settings" \
  -H "Authorization: Bearer $ZERNIO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "forwardSales": true }'
```

Only sales paid after you turn it on are sent, through the profile's [forwarding rules](/conversions/create-forwarding-rule) that take the `chargebee` source:

| Invoice | Sent as |
|---|---|
| A one-off invoice (no subscription) | `Purchase` |
| A subscription's first invoice | `Subscribe`, not also `Purchase`, so a platform never counts it twice |
| A renewal invoice or a refund | Nothing |

## How it behaves

- An invoice counts once it is fully paid. Its value is what was collected on it (`amount_paid`, so credits that paid part of it are left out), converted from minor units (`4999` is 49.99). A part payment counts nothing until the last one lands.
- A refund counts when Chargebee sends `payment_refunded`, or `credit_note_created` for a refundable credit note: Zernio reads the invoice and its buyer back and stores it at what was collected less the credit notes that refund it, keeping its email and campaign. A full refund retracts it. Adjustment and store-credit notes change nothing.
- An invoice that took no money is not a sale.
- The amount keeps the invoice's currency. Campaign results convert it to the ad account's currency.

## Related

- [Conversions API](/products/conversions-api)
- [Campaign results](/guides/campaign-results)