# BigQuery, Firebase and Snowflake

> Sync users and events from BigQuery (including the Firebase Analytics and GA4 export) or Snowflake with your own queries, and write decisions back to a table your BI tools can read.

:::note Built and tested against each vendor's documented API; tell us if anything differs in your account.:::

Warehouses are pulled, not pushed: there is no webhook URL. You paste a key and SQL queries when you connect. An hourly job runs each active warehouse integration. **Sync now** runs one immediately. **Test** signs in, runs `SELECT 1` and up to five rows of each query, and writes nothing.

## What you'll need

- An admin of the account. Integrations are set up in **Settings → Integrations**.
- **BigQuery:** a service account with BigQuery Data Viewer and Job User on the project (plus Data Editor on the dataset for write-back), and its JSON key.
- **Snowflake:** a user with key-pair auth and a role that can read your tables and use the warehouse.
- A SQL query for users and one for events that follow the [column contract](#column-contract).

## Connect

:::note Warehouse tables and queries run in your account. Give the service account or user only the access listed here.:::

**BigQuery.** Create a service account with BigQuery Data Viewer and Job User on the project (plus Data Editor on the dataset for write-back). Create a JSON key and paste it. Settings:

| Setting | Meaning |
|---|---|
| `projectId` | Default: the key's `project_id` |
| `location` | Optional |
| `datasetId` | Needed for the Firebase preset and write-back |
| `usersQuery`, `eventsQuery` | Your queries, standard SQL |
| `audiencesQuery` | Optional: see [Audiences](#audiences) |
| `preset` | `firebase`, or none |
| `writeBack` | On or off |

**Snowflake.** Create a user with key-pair auth (`ALTER USER … SET RSA_PUBLIC_KEY = '…'`) and a role that can read your tables and use the warehouse. Paste the unencrypted PKCS#8 private key (`-----BEGIN PRIVATE KEY-----`). Settings: `account` (for example `xy12345.us-east-1`), `user`, `role` (optional), `warehouse`, `database`, `schema`, `usersQuery`, `eventsQuery`, `audiencesQuery` and `writeBack`. Column names are matched without regard to case.

## Column contract

Columns are matched by name. Other columns are ignored for events and become attributes for users.

| Query | Column | Meaning |
|---|---|---|
| users | `user_id` | Required (text or number). Rows without it are skipped and counted. |
| users | `email` | Email contact. An invalid one is dropped and the user kept. |
| users | `signup_at`, `last_active_at` | Timestamps |
| users | `updated_at` | Only moves the cursor |
| users | any other | An attribute: strings, numbers and booleans; nulls and other types are left out |
| events | `user_id`, `name`, `timestamp` | Required. A row missing one is skipped and counted. |
| events | `event_id` | Optional. Otherwise it is derived from the user, name and time, so re-reading a row is a duplicate. |
| events | `properties` | A JSON object, or a string holding one |

## `{{since}}` and the cursor

In a query, `{{since}}` becomes a quoted ISO timestamp literal, such as `'2026-11-20T09:17:00.000Z'`, which both warehouses compare to a timestamp column:

```sql
SELECT user_id, email, updated_at FROM app.users WHERE updated_at >= {{since}}
```

Each query keeps its own cursor. The first run reads from 7 days ago.

- Events: the query is wrapped as `SELECT * FROM (<query>) ORDER BY timestamp LIMIT 50000`, and the cursor becomes the newest timestamp ingested. Use `>=` when several events can share a timestamp: ids make the overlap duplicates.
- Users: the query is wrapped with `LIMIT 10000`. The cursor becomes the newest `updated_at` when the rows have one, otherwise the run's start time. Filter users on `updated_at` so a capped run catches up next hour.
- The cursor moves only when every query of the run succeeded. A failed run records the error, shown on the integration's card, and the next run retries from the same place.
- Runs never overlap. A run is skipped while another is less than 55 minutes old, and **Sync now** answers `409`.

## Firebase Analytics and GA4 (BigQuery)

Link Firebase or GA4 to BigQuery, set `datasetId` to the `analytics_<id>` dataset and turn on the Firebase preset. The events query is then generated, so you do not write one. It reads `events_*` from the dataset, takes `event_name` as the event name and turns `event_params` into the event's properties.

Only events with a `user_id` (your app's `setUserId`) are read. Events with only a `user_pseudo_id` are not. Intraday tables match `events_*` too, and their rows become duplicates when the daily table lands.

## Write-back

With `writeBack` on, the first sync at or after 02:00 UTC each day writes the previous UTC day's decisions, with their outcomes so far, to a table it creates if missing.

| Column | BigQuery | Snowflake |
|---|---|---|
| `decision_id`, `user_id`, `agent_id`, `agent_name`, `variant_id`, `variant_name`, `channel`, `conversion_event` | STRING | STRING |
| `sent_at`, `converted_at` | TIMESTAMP | TIMESTAMP_NTZ |
| `reward` | FLOAT64 | FLOAT |
| `holdout` | BOOL, always false because holdout users get no decision | BOOLEAN |

BigQuery writes `<dataset>.herald_decisions`. Snowflake writes `HERALD_DECISIONS` in the configured database and schema, merging on `decision_id`. A failed write-back is shown as the last error and retried next hour. Conversions that land after the export day are not re-sent.

## Audiences

Define segments in SQL. The optional audiences query returns two columns, `user_id` and `audience`, and each distinct `audience` becomes an externally managed segment named `BigQuery: <audience>` (or `Snowflake: <audience>`) that agents target like any other segment.

```sql
SELECT user_id, tier AS audience FROM customers WHERE tier IN ('VIP', 'Churn risk')
```

- It runs in full every hour, without `{{since}}`. Each audience's members are replaced by what the query returned, and an audience that stops appearing is emptied.
- Members Herald doesn't know yet are created, as cohort members are. Rows missing either column are skipped and counted.
- Up to 50 audiences and 50,000 rows. Over either limit, the run changes nothing and shows the error, so a segment is never half-replaced.
- You can't edit these segments in the console. Change the query instead.
- **Test** shows up to five audience rows.

## Use with Looker Studio, Tableau and Power BI

Point the tool at the write-back table (`herald_decisions` in BigQuery, `HERALD_DECISIONS` in Snowflake), or at the Google Sheets tab from [Reporting and exports](/docs/integrations/reporting). Looker Studio and Power BI read Google Sheets directly. For a one-off, use the decisions CSV.

## Limits

- 10,000 users and 50,000 events per run per integration. The rest comes next hour.
- 50 audiences and 50,000 audience rows per run.
- Queries time out after 120 seconds.

## Troubleshooting

- **Rows are skipped.** A users row needs `user_id`. An events row needs `user_id`, `name` and `timestamp`.
- **The same rows arrive every hour.** Filter on a timestamp column with `{{since}}`.
- **The Firebase preset finds nothing.** Check `datasetId` and that your app calls `setUserId`.

## Common errors

| Vendor | Message | Cause | Fix |
|---|---|---|---|
| BigQuery | `BigQuery: <message>` or `BigQuery answered HTTP <status>` | BigQuery refused the query, often a SQL error or missing permission | Run the query in BigQuery and fix it |
| BigQuery | `Google sign-in failed` | The service account key can't sign in | Paste a current JSON key |
| BigQuery | `BigQuery query timed out` | The query ran too long | Narrow it with `{{since}}` |
| BigQuery | `BigQuery refused <n> rows` | Write-back rows were refused | Check the decisions table's schema |
| BigQuery | `projectId is not a valid project id` or `datasetId may contain only letters, numbers and underscores` | Saving the integration: a bad id | Copy the ids from Google Cloud |
| Snowflake | `Snowflake: <message>` or `Snowflake answered HTTP <status>` | Snowflake refused the statement | Run the query in Snowflake and fix it |
| Snowflake | `Snowflake statement timed out` | The query ran too long | Narrow it with `{{since}}` |
| Snowflake | `account is not a valid account identifier` | Saving the integration: a bad account | Use the account identifier, such as `xy12345.us-east-1` |
| Both | `Write-back failed: <reason>` | Writing decisions back failed | Check the user's write access |
| Both | `Audiences failed: The audiences query returned more than 50,000 rows; nothing was changed` | The query returned too many rows | Narrow the query; the segments keep their members until it fits |
| Both | `Audiences failed: The audiences query returned more than 50 audiences` | Too many distinct `audience` values | Return fewer audiences |
