> The index of all pages is at https://docs.afffast.com/llms.txt.

# Links and click tracking

Make tracked links, count clicks, and see which page earned each commission.

The API has its own redirector. A visitor clicks a link of yours. The redirector saves the click and sends the visitor to the network. When the visitor buys, the conversion comes back with the same click ID. So you know which link, which page and which SubID earned the commission.

## How it works

1. You make a link for a program. The API gives you a short URL, for example `https://afffast.link/k3Xa9Qz`.
2. A visitor opens the short URL. The redirector saves the click and sends a `302` redirect to the page of the advertiser, through the tracking of the network.
3. The redirector puts our click ID in a SubID slot of the network. The click ID has 12 characters.
4. The network reports the conversion with that SubID. The sync joins the conversion to the click. The conversion gets `click_id`, `user_subids` and `click_context` (site, page, referrer, country and device).

A bot, a prefetch request and a `HEAD` request get the redirect and no click. Redirects never stop when you reach a limit of your plan.

A network with no SubID slot cannot join a conversion to a click. Read `capabilities.subid_slots` of the network with `GET /v1/networks/{id}`.

## Make a saved link

A program needs a deeplink template (`deeplink_support: true`). The host of the destination must be on a domain that the program allows. You need the scope `links:write`.

```bash
curl -X POST https://api.afffast.com/v1/links \
  -H "Authorization: Bearer $AFFFAST_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{
    "program": "prog_...",
    "destination": "https://shop.example/products/boots",
    "site": "site_...",
    "subids": { "s1": "blog-post-12" }
  }'
```

The response has `url`, the short URL that you share. The code of the link has 7 or 8 characters.

- `subids` are your own labels `s1` to `s5`. A value has 255 characters at most. The redirector stores them with the click. It sends them to the network when the network has a slot for them.
- `active: false` pauses the link. A paused link does not redirect.
- `POST /v1/links/batch` makes up to 100 links. It returns the results in the order of the items.
- Read the clicks of a link with `GET /v1/links/{id}?include_clicks=true`.

Errors that you can get: [`program_deeplinks_unsupported`](https://docs.afffast.com/errors/program-deeplinks-unsupported.md) and [`link_destination_not_allowed`](https://docs.afffast.com/errors/link-destination-not-allowed.md).

## Make a deeplink without saving it

`POST /v1/deeplinks` makes up to 100 deeplinks in one request. By default the API saves nothing. Each item gives a signed URL: `<redirector>/r?p=...&u=...&sig=...`. Use `"save": true` to save the link and get a short URL (this needs the scope `links:write` too). You need the scope `deeplinks:write`.

The API checks each item alone. An item that is not valid has an error in its result. The other items still succeed.

## Track the page

The page of a click is the value of the query parameter `page` of the link, or the `Referer` header when the link has no `page`. Add the parameter to the link when you know the page and the referrer is not sent:

```text
https://afffast.link/k3Xa9Qz?page=https://blog.example/boots-review
```

The redirector normalizes the page. It removes the fragment, the slash at the end of the path and all query parameters except `utm_*`. So all clicks from one article have one page value.

## Read the clicks and the revenue

- `GET /v1/clicks` lists the clicks, newest first. Filter by `link`, `program`, `site`, `country`, `device` and time. It needs the scope `clicks:read`. The API keeps raw clicks for 13 months.
- `GET /v1/stats/clicks` counts the clicks. Use `group_by` with `day`, `week`, `month`, `country`, `device` or `link`.
- `GET /v1/stats?group_by=page` shows the revenue of each page. `group_by=subid` shows the revenue of each SubID. Choose the slot with `subid_slot`. A conversion with no click is in the group `unattributed`.
- Bot clicks are not counted. Use `include_bots=true` on `GET /v1/clicks` to see them.

Listen to the event `click.attributed` with a [webhook](https://docs.afffast.com/docs/webhooks.md) to know when a conversion is joined to a click.

## Test the attribution

Use a test key. Make a test link and open it. Then send `POST /v1/test/conversions` with the `click_id` of the click. The conversion goes through the same join as a conversion of a sync. See [Test mode](https://docs.afffast.com/docs/test-mode.md).
