Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(signals): merge survey answers per question in the surveys scout recipe (#116683)

posthog[bot]1e62b13be1a3
diff --git a/products/signals/skills/signals-scout-surveys/references/response-querying.md b/products/signals/skills/signals-scout-surveys/references/response-querying.md
index 7ac53384c17e..182d7f72b1f1 100644
--- a/products/signals/skills/signals-scout-surveys/references/response-querying.md
+++ b/products/signals/skills/signals-scout-surveys/references/response-querying.md
@@ -3,7 +3,7 @@
The two mechanical traps in `survey sent` data — the dual response-key schemes and
multi-event submissions — plus the copy-ready SQL for rating trends and open-text pulls.
Read this before writing any response query: getting either trap wrong silently returns
-"no responses" or over-counts.
+"no responses", drops answers, or over-counts.
## Resolving the response value — coalesce both key schemes
@@ -21,78 +21,102 @@ question, so the key is bare `$survey_response` — returns all-NULL under the i
key alone, which reads as "no responses." Always coalesce id-based over the
index-based fallback.
-## Dedupe by submission
+## Merge each submission, one answer per question
-Always dedupe by `$survey_submission_id` for surveys collected after that property
-shipped — the legacy path is one row per submission, but newer client versions can
-emit multiple `survey sent` events per submission and you'll over-count rating
-responses. Pattern (from `products/surveys/backend/util.py`):
+Newer clients can emit multiple `survey sent` events for one submission, all with the same `$survey_submission_id`.
+Count each submission once, or you over-count rating responses.
+
+Do **not** keep only the latest event of a submission (`argMax(uuid, timestamp)`) and read answers from it.
+A later event can omit an answer that an earlier event carried: for example, partial-response events that each carry one answer, or a manual capture that sends each answer on its own event.
+The latest-event read then returns an empty answer, and the feedback is lost.
+
+Merge per question instead.
+Group the events by submission and keep the **latest nonempty value of each question** (`argMaxIf(answer, timestamp, answer != '')`).
+The product's responses API does the same merge (`fetch_response_rows()` in `products/surveys/backend/responses/fetch_rows.py`).
+
+Rules for the merge:
+
+- **Submission key**: `$survey_submission_id`, or the event `uuid` when the ID is missing or empty. Each legacy event is its own submission.
+- **Boundaries**: filter on `$survey_id` and also group by `$survey_iteration`, so that a submission never mixes surveys or iterations.
+- **Empty and omitted answers are the same**: the key coalesce changes an empty string to NULL, so an empty value never replaces an earlier answer.
+ The event stream does not show an explicit answer deletion as different from an omitted answer.
+ The merge keeps the earlier answer in both cases. It can keep an answer that the respondent cleared, but it never drops an answer they gave.
+- **Changed answers**: when two events carry different nonempty values for one question, the later event wins.
+- **Completion is separate**: read it from `$survey_completed`, not from the answers.
+ With partial responses on, intermediate events carry `$survey_completed: false` and the final event carries `true`. Older events and API events can omit the property.
+ A submission is complete when one or more of its events is not marked `false`.
+
+Base subquery, one row per submission. Add one `answer` column and one `argMaxIf` column for each question you need:
```sql
--- Inside the WHERE clause
-AND uuid IN (
- SELECT argMax(uuid, timestamp) FROM events
+SELECT
+ if(coalesce(properties.$survey_submission_id, '') = '', toString(uuid), properties.$survey_submission_id) AS submission_key,
+ argMax(person_id, timestamp) AS person_id,
+ max(timestamp) AS submitted_at,
+ argMaxIf(answer, timestamp, answer != '') AS response,
+ max(coalesce(properties.$survey_completed, '') != 'false') AS completed
+FROM (
+ SELECT
+ uuid,
+ person_id,
+ timestamp,
+ properties,
+ coalesce(
+ nullIf(JSONExtractString(properties, '$survey_response_<question_id>'), ''), -- id-based (modern)
+ nullIf(JSONExtractString(properties, '<index_based_key>'), ''), -- '$survey_response' (index 0) or '$survey_response_<n>'
+ ''
+ ) AS answer
+ FROM events
WHERE event = 'survey sent'
AND JSONExtractString(properties, '$survey_id') = '<survey_id>'
AND timestamp > now() - INTERVAL 30 DAY
- GROUP BY CASE
- WHEN COALESCE(JSONExtractString(properties, '$survey_submission_id'), '') = ''
- THEN toString(uuid)
- ELSE JSONExtractString(properties, '$survey_submission_id')
- END
)
+GROUP BY properties.$survey_iteration, submission_key
```
+A submission with no answer to the question returns `response = ''`.
+
## Rating trend (score regression)
-Daily average score for one rating question, both key schemes coalesced:
+Daily average score for one rating question, one row per submission:
```sql
SELECT
- toDate(timestamp) AS day,
- avg(toFloat64OrNull(coalesce(
- nullIf(JSONExtractString(properties, '$survey_response_<question_id>'), ''), -- id-based (modern)
- nullIf(JSONExtractString(properties, '<index_based_key>'), '') -- '$survey_response' (index 0) or '$survey_response_<n>'
- ))) AS avg_score,
+ toDate(submitted_at) AS day,
+ avg(toFloat(response)) AS avg_score,
count() AS responses
-FROM events
-WHERE event = 'survey sent'
- AND JSONExtractString(properties, '$survey_id') = '<survey_id>'
- AND timestamp > now() - INTERVAL 30 DAY
- -- plus the submission-dedupe clause above
+FROM (
+ -- the per-submission base subquery above, with INTERVAL 30 DAY
+)
+WHERE response != ''
GROUP BY day
ORDER BY day
```
## Open-text pull (theme aggregation)
-Recent non-empty open-text responses for one question, ready to read for clustering:
+Recent nonempty open-text responses for one question, ready to read for clustering:
```sql
SELECT
- coalesce(
- nullIf(JSONExtractString(properties, '$survey_response_<question_id>'), ''), -- id-based (modern)
- nullIf(JSONExtractString(properties, '<index_based_key>'), '') -- '$survey_response' (index 0) or '$survey_response_<n>'
- ) AS response,
+ response,
person_id,
- timestamp
-FROM events
-WHERE event = 'survey sent'
- AND JSONExtractString(properties, '$survey_id') = '<survey_id>'
- AND timestamp > now() - INTERVAL 14 DAY
- AND coalesce(
- nullIf(JSONExtractString(properties, '$survey_response_<question_id>'), ''),
- nullIf(JSONExtractString(properties, '<index_based_key>'), '')
- ) != ''
- -- plus the submission-dedupe clause above
-ORDER BY timestamp DESC
+ submitted_at,
+ completed
+FROM (
+ -- the per-submission base subquery above, with INTERVAL 14 DAY
+)
+WHERE response != ''
+ORDER BY submitted_at DESC
LIMIT 200
```
+Keep partial submissions (`completed = 0`) in a theme read. A respondent who left after one answer still gave that answer.
+
## Iteration filter
Recurring surveys tag each iteration's responses with `$survey_iteration`. To compare
-iterations cleanly, add:
+iterations cleanly, add this to the inner `WHERE` of the base subquery:
```sql
AND JSONExtractString(properties, '$survey_iteration') = '<n>'
@@ -102,7 +126,7 @@ AND JSONExtractString(properties, '$survey_iteration') = '<n>'
- `$survey_id` — which survey
- `$survey_iteration` — which iteration of a recurring survey
-- `$survey_submission_id` — dedupe key (newer events; older events lack this)
+- `$survey_submission_id` — submission merge key (newer events; older events lack this)
- `$survey_response` — first question's response, index-based legacy key (index 0)
- `$survey_response_<n>` — index-based key for question index _n_ > 0 (numeric suffix)
- `$survey_response_<question_id>` — id-based per-question key (question UUID; preferred,