: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
"""Fetch full survey response rows (with metadata) for the responses API.
The summarization fetch helper returns response strings only — sufficient for LLM
input but not for agents that need to cross-pivot to recordings, events, or persons.
This module returns rows with `distinct_id`, `session_id`, `submitted_at`, resolved
per-question answers, and event-level metadata (device, geoip, etc).
"""
from dataclasses import dataclass, field
from datetime import UTC, datetime
from typing import Any, cast
from posthog.hogql import ast
from posthog.hogql.parser import parse_expr, parse_select
from posthog.hogql_queries.paginators import HogQLHasMorePaginator
from posthog.models import Team
from products.surveys.backend.models import Survey
# Stamped by the frontend editor into a translation's choices when a spot needs a human
# translation. Must never be treated as real translated content.
TRANSLATION_NEEDED_PLACEHOLDER = "[Translation needed]"
@dataclass(frozen=True)
class QuestionAnswer:
question_id: str
question_index: int
question_text: str
question_type: str
answer: Any
@dataclass(frozen=True)
class SurveyResponseRow:
uuid: str
distinct_id: str
session_id: str | None
submitted_at: datetime
answers: list[QuestionAnswer]
extra: dict[str, Any] = field(default_factory=dict)
def build_choice_translation_map(question: dict[str, Any]) -> dict[str, str]:
"""Map each translated choice back to its base-language choice, matched by position.
Mirrors the frontend twin ``buildChoiceTranslationMap`` in
``frontend/src/scenes/surveys/surveyTranslationUtils.ts``: base choices are seeded last so
they win on collision with a translation that reuses another base choice's string.
"""
base_choices = question.get("choices")
if not isinstance(base_choices, list):
return {}
# An untranslated question still needs its base choices mapped, so a missing/malformed
# `translations` means "no translated entries", never an early return.
translations = question.get("translations")
if not isinstance(translations, dict):
translations = {}
mapping: dict[str, str] = {}
for translation in translations.values():
if not isinstance(translation, dict):
continue
translated_choices = translation.get("choices")
if not isinstance(translated_choices, list):
continue
# The length guard only catches out-of-band edits (API/Max tool) that change array
# length — the editor path preserves length and stamps the placeholder instead (see the
# placeholder skip below), which is why that skip is the real guard for that path.
if len(translated_choices) != len(base_choices):
continue
for index, choice in enumerate(translated_choices):
base_choice = base_choices[index]
# Skip untranslated placeholders and blanks — they carry no real mapping and would
# otherwise collapse onto whichever base choice the last language lands on.
if (
isinstance(choice, str)
and choice.strip()
and choice != TRANSLATION_NEEDED_PLACEHOLDER
and isinstance(base_choice, str)
):
mapping[choice] = base_choice
# Seed base choices last so they take precedence: a translation that reuses another
# base-choice string must not remap that base choice to a different option. The tradeoff is
# lossy — a translated pick that collides with a different base choice is attributed to that
# base option — but it's the safe default (base-language data is never misattributed).
for choice in base_choices:
if isinstance(choice, str):
mapping[choice] = choice
return mapping
def resolve_question_metadata(survey: Survey) -> list[dict[str, Any]]:
"""Return survey questions with stable `(id, index, text, type, choices, choice_map)` shape."""
questions = survey.questions or []
resolved: list[dict[str, Any]] = []
for index, question in enumerate(questions):
if not isinstance(question, dict):
continue
resolved.append(
{
"id": question.get("id") or "",
"index": index,
"text": question.get("question") or "",
"type": question.get("type") or "open",
"choices": question.get("choices"),
"choice_map": build_choice_translation_map(question),
}
)
return resolved
# HogQL expression that collapses a submission's events into one group. Events without a
# `$survey_submission_id` are keyed by their own uuid, so each stays a distinct response
# (the legacy single-event behavior). Shared with the summarization + per-question-stats
# fetchers so the grouping stays identical across every response surface.
SUBMISSION_GROUPING_KEY = (
"if(coalesce(properties.$survey_submission_id, '') = '', toString(uuid), properties.$survey_submission_id)"
)
RESPONSE_EVENT_FILTER = """(event = 'survey sent' OR (
event IN ('survey dismissed', 'survey abandoned')
AND coalesce(JSONExtractString(properties, '$survey_partially_completed'), '') = 'true'
))"""
# Metadata columns appended to every row — known $-prefixed event properties resolved via
# HogQL property accessors (backticked because the keys are literal property names, not
# placeholder values).
_METADATA_COLUMNS: list[tuple[str, str]] = [
("session_id", "properties.`$session_id`"),
("device_type", "properties.`$device_type`"),
("browser", "properties.`$browser`"),
("os", "properties.`$os`"),
("geoip_country_code", "properties.`$geoip_country_code`"),
("geoip_country_name", "properties.`$geoip_country_name`"),
("geoip_city_name", "properties.`$geoip_city_name`"),
("current_url", "properties.`$current_url`"),
("iteration", "properties.`$survey_iteration`"),
]
def fetch_response_rows(
*,
survey: Survey,
team: Team,
since: datetime | None = None,
until: datetime | None = None,
question_id: str | None = None,
score_lte: float | None = None,
score_gte: float | None = None,
limit: int = 100,
offset: int = 0,
exclude_uuids: set[str] | None = None,
) -> tuple[list[SurveyResponseRow], bool]:
"""Fetch survey response rows for the responses API.
`score_lte` / `score_gte` require `question_id` — score filtering only
makes sense against a specific rating question.
"""
if (score_lte is not None or score_gte is not None) and not question_id:
raise ValueError("score_lte / score_gte require question_id")
survey_id = str(survey.id)
questions = resolve_question_metadata(survey)
questions_in_scope = [q for q in questions if (not question_id or q["id"] == question_id)]
if question_id and not questions_in_scope:
return [], False
paginator = HogQLHasMorePaginator(limit=limit, offset=offset)
placeholders: dict[str, ast.Expr] = {
"survey_id": ast.Constant(value=survey_id),
# The submission-merge query is bounded by date — fall back to the survey
# lifetime when the caller didn't supply explicit bounds.
"start_date": ast.Constant(value=since or survey.start_date or survey.created_at),
"end_date": ast.Constant(value=until or survey.end_date or datetime.now(UTC)),
}
# Resolve one answer column per question with the HogQL getSurveyResponse helper (the
# same helper the summarization fetch uses, so resolution semantics match), then merge
# them across the submission: for each $survey_submission_id we keep the latest non-null
# answer to each question. This stops multi-event submissions — e.g. AI-feedback manual
# capture, where each answer can arrive on its own event — from dropping answers that
# only ever appeared on a non-final event.
inner_answer_columns: list[str] = []
outer_answer_columns: list[str] = []
for q in questions_in_scope:
idx_name = f"q_idx_{q['index']}"
id_name = f"q_id_{q['index']}"
inner_answer_columns.append(f"getSurveyResponse({{{idx_name}}}, {{{id_name}}}) AS raw_answer_{q['index']}")
outer_answer_columns.append(
f"argMaxIf(raw_answer_{q['index']}, timestamp, isNotNull(raw_answer_{q['index']})) AS answer_{q['index']}"
)
placeholders[idx_name] = ast.Constant(value=q["index"])
placeholders[id_name] = ast.Constant(value=q["id"])
placeholders["grouping_key"] = parse_expr(SUBMISSION_GROUPING_KEY)
# Per-event projection (inner) → per-submission merge (outer). Column order in the outer
# SELECT must stay uuid, distinct_id, submitted_at, <metadata…>, <answers…> to match the
# positional parsing below.
inner_select = ", ".join(
[
"uuid",
"distinct_id",
"timestamp",
*(f"{expr} AS {alias}" for alias, expr in _METADATA_COLUMNS),
*inner_answer_columns,
"{grouping_key} AS submission_key",
]
)
outer_select = ", ".join(
[
"argMax(uuid, timestamp) AS uuid",
"argMax(distinct_id, timestamp) AS distinct_id",
"max(timestamp) AS submitted_at",
*(f"argMax({alias}, timestamp) AS {alias}" for alias, _ in _METADATA_COLUMNS),
*outer_answer_columns,
]
)
inner_conditions = [
RESPONSE_EVENT_FILTER,
"properties.`$survey_id` = {survey_id}",
"timestamp >= {start_date}",
"timestamp <= {end_date}",
]
# Filters that depend on the merged answer run in HAVING, after the submission has been
# collapsed to a single row.
having_conditions: list[str] = []
if question_id:
# Only return submissions where this question has a non-empty answer.
# coalesce-then-trim defends against NULL semantics in HogQL — without it the
# equivalent `trim(...) != ''` predicate evaluates to NULL for nullified responses
# and is silently ignored in some contexts.
target_q = next(q for q in questions_in_scope if q["id"] == question_id)
having_conditions.append(f"length(trim(coalesce(answer_{target_q['index']}, ''))) > 0")
if score_lte is not None:
having_conditions.append(f"toFloat(answer_{target_q['index']}) <= {{score_lte}}")
placeholders["score_lte"] = ast.Constant(value=score_lte)
if score_gte is not None:
having_conditions.append(f"toFloat(answer_{target_q['index']}) >= {{score_gte}}")
placeholders["score_gte"] = ast.Constant(value=score_gte)
if exclude_uuids:
# Archiving records the submission's representative (latest) uuid, which is exactly
# what argMax(uuid) surfaces here — so exclude by that.
having_conditions.append("uuid NOT IN {exclude_uuids}")
placeholders["exclude_uuids"] = ast.Tuple(exprs=[ast.Constant(value=u) for u in exclude_uuids])
having_clause = ("HAVING " + " AND ".join(having_conditions)) if having_conditions else ""
query_str = (
"SELECT "
+ outer_select
+ " FROM ( SELECT "
+ inner_select
+ " FROM events WHERE "
+ " AND ".join(inner_conditions)
+ " ) GROUP BY submission_key "
+ having_clause
+ " ORDER BY submitted_at DESC"
)
select_ast = cast(ast.SelectQuery, parse_select(query_str, placeholders))
paginator.execute_hogql_query(
team=team,
query_type="survey_responses_rows_query",
query=select_ast,
)
# Column order matches the SELECT list above. The metadata columns slot in between
# the fixed leading 3 (uuid, distinct_id, submitted_at) and the per-question answer columns.
metadata_offset = 3 # uuid, distinct_id, submitted_at
answer_offset = metadata_offset + len(_METADATA_COLUMNS)
rows: list[SurveyResponseRow] = []
for raw in paginator.results:
uuid_val, distinct_id, submitted_at = raw[0], raw[1], raw[2]
extra: dict[str, Any] = {}
session_id: str | None = None
for i, (alias, _) in enumerate(_METADATA_COLUMNS):
value = raw[metadata_offset + i]
if alias == "session_id":
session_id = str(value) if value else None
else:
# Pass empty/null through as None to keep payloads clean for agents.
extra[alias] = value if value not in (None, "") else None
answers: list[QuestionAnswer] = []
for i, q in enumerate(questions_in_scope):
value = raw[answer_offset + i]
if value in (None, ""):
continue
answers.append(
QuestionAnswer(
question_id=q["id"],
question_index=q["index"],
question_text=q["text"],
question_type=q["type"],
answer=value,
)
)
rows.append(
SurveyResponseRow(
uuid=str(uuid_val),
distinct_id=str(distinct_id),
session_id=session_id,
submitted_at=submitted_at,
answers=answers,
extra=extra,
)
)
return rows, paginator.has_more()