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

# Destinations to Google Sheets

Write your conversions, payouts and programs to a Google Sheet with a destination.

The type `google_sheets` writes the data of your workspace to a Google Sheet. It keeps one row for each conversion, payout and program. 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

1. Make a Google service account. Download its JSON key.
2. Share the spreadsheet with the `client_email` of the service account. Give it the role Editor.
3. 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": "google_sheets",
    "label": "Conversions sheet",
    "config": { "spreadsheet_id": "1AbC..." },
    "credentials": { "service_account_json": "{ ... }" }
  }'
```

| Field                  | Kind       | Meaning                                                                                                               |
| ---------------------- | ---------- | --------------------------------------------------------------------------------------------------------------------- |
| `spreadsheet_id`       | Setting    | The ID in the URL of the sheet (`docs.google.com/spreadsheets/d/<ID>/edit`). The full URL also works. It is required. |
| `sheet_name`           | Setting    | The tab for conversions. The default is `Conversions`.                                                                |
| `payouts_sheet_name`   | Setting    | The tab for payouts. The default is `Payouts`.                                                                        |
| `programs_sheet_name`  | Setting    | The tab for programs. The default is `Programs`.                                                                      |
| `events_sheet_name`    | Setting    | The tab for other events. The default is `Events`.                                                                    |
| `service_account_json` | Credential | The JSON key 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.

## Tabs

Each kind of data goes to its own tab. Column A of a tab is the key of the row.

| Kind         | Tab                                                |
| ------------ | -------------------------------------------------- |
| Conversions  | `Conversions` (`sheet_name`). Key: column A.       |
| Payouts      | `Payouts` (`payouts_sheet_name`). Key: column A.   |
| Programs     | `Programs` (`programs_sheet_name`). Key: column A. |
| Clicks       | Not available.                                     |
| Other events | `Events` (`events_sheet_name`).                    |

- The sheet keeps one row for each conversion, payout and program. Column A is the ID of the object. A change updates the row. A new object adds a row.
- The tabs must have different names. The service account makes a missing tab.
- A payout goes to its tab when the destination selects `payout.created` or `payout.updated`. A program goes to its tab when the destination selects `program.status_changed` or `commission.changed`.

## Backfill

A Google Sheets destination takes backfills of conversions, payouts and programs. It does not take clicks. A Google Sheet holds 10 million cells at most, so a click backfill returns 422 with the reason. To keep clicks, use a [BigQuery destination](https://docs.afffast.com/docs/destinations-bigquery.md). See the [backfill rules](https://docs.afffast.com/docs/destinations.md#backfill).

## Delivery

Google Sheets 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 spreadsheet or a bad credential) sets the status `failed` at once.
