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

# Destinations to BigQuery

Append your conversions, payouts, programs and clicks to BigQuery tables with a destination.

The type `bigquery` appends the data of your workspace to BigQuery tables. The tables are change logs: they keep the full history. You need an API key with the scope `destinations:write` and a Google service account. Read the common rules for all types (events, filters, delivery, retries and backfill) in [Destinations](https://docs.afffast.com/docs/destinations.md).

## Make the destination

Make a Google service account with the roles BigQuery Data Editor and BigQuery Job User (BigQuery Admin to make datasets). Download its JSON key. The project needs billing. Then make the destination:

```bash
curl -X POST https://api.afffast.com/v1/destinations \
  -H "Authorization: Bearer $AFFFAST_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "type": "bigquery",
    "label": "Conversions in BigQuery",
    "config": { "project_id": "my-project", "dataset": "affiliate", "table": "conversions", "create_if_missing": true },
    "credentials": { "service_account_json": "{ ... }" }
  }'
```

| Field                  | Kind       | Meaning                                                                                                 |
| ---------------------- | ---------- | ------------------------------------------------------------------------------------------------------- |
| `project_id`           | Setting    | The Google Cloud project that holds the dataset. The default is the project of the service account key. |
| `dataset`              | Setting    | The ID of the dataset. Use letters, digits and underscores. It is required.                             |
| `table`                | Setting    | The ID of the table. Use letters, digits, underscores and hyphens. It is required.                      |
| `payouts_table`        | Setting    | The table for payouts. The default is the table plus `_payouts`.                                        |
| `programs_table`       | Setting    | The table for programs. The default is the table plus `_programs`.                                      |
| `clicks_table`         | Setting    | The table for clicks. A click backfill writes it. The default is the table plus `_clicks`.              |
| `location`             | Setting    | The location of a new dataset, for example `EU` or `US`. The default is `US`.                           |
| `create_if_missing`    | Setting    | Make the dataset, the tables and the views when they do not exist. The default is `true`.               |
| `service_account_json` | Credential | The JSON key file of the service account. The API never returns it.                                     |

The `GET /v1/destination_types` response is the source of truth. It has the description and the options of each field.

## Tables

Each kind of data goes to its own table. Each kind needs its own table name.

| Kind         | Table                                                           |
| ------------ | --------------------------------------------------------------- |
| Conversions  | `table`. One row for each change. View `<table>_current`.       |
| Payouts      | `<table>_payouts` (`payouts_table`). One row for each change.   |
| Programs     | `<table>_programs` (`programs_table`). One row for each change. |
| Clicks       | `<table>_clicks` (`clicks_table`). Only a backfill writes it.   |
| Other events | `table`, with `kind` = `event`.                                 |

- BigQuery appends rows. A payout row and a program row is the state of the object at the time of the change.
- The tables are partitioned by day.
- When `create_if_missing` is on, the API makes the dataset, the tables and the views.
- The API writes the rows with the BigQuery Storage Write API.
- A payout goes to its table when the destination selects `payout.created` or `payout.updated`. A program goes to its table when the destination selects `program.status_changed` or `commission.changed`. A click has no event type: only a [backfill](#backfill) sends it.
- A click row has the ID of the click (`clk_...`), the time, the link, the site, the program, the network, the page, the country, the device and the sub IDs. It has no IP address.

## Views

The API makes one view for each table. Query the view to get one row for each object:

| View                       | Rows                               |
| -------------------------- | ---------------------------------- |
| `<table>_current`          | The newest row of each conversion. |
| `<table>_payouts_current`  | The newest row of each payout.     |
| `<table>_programs_current` | The newest row of each program.    |
| `<table>_clicks_unique`    | One row for each click.            |

The names follow your table names (`payouts_table`, `programs_table`, `clicks_table`). A view picks the newest row with `ROW_NUMBER()` (by `occurred_at`, then `ingested_at`). Use the table when you want the history.

BigQuery can get the same row two times, because delivery is at least once. Query the views `_current` and `_unique`: they remove the repeats.

## Backfill

A BigQuery destination takes backfills of conversions, payouts, programs and clicks. A click backfill reads in pages of 1000 clicks and sends one page each second. The clicks go to `clicks_table`. A repeated backfill adds the clicks to the table again. Query the view `<clicks_table>_unique` to get one row for each click. See the [backfill rules](https://docs.afffast.com/docs/destinations.md#backfill).

## Delivery

BigQuery is a `data_sync` type. A temporary error is tried again on the schedule in [Delivery](https://docs.afffast.com/docs/destinations.md#delivery). A permanent error (for example a deleted dataset or a bad credential) sets the status `failed` at once.
