: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
feat(surveys): let Max create hosted (external) surveys (#71333)
Ian Vanagaseb2d3c378cd6
diff --git a/products/surveys/backend/max_tools.py b/products/surveys/backend/max_tools.pyindex 65a0c80da7de..82f71202e154 100644--- a/products/surveys/backend/max_tools.py+++ b/products/surveys/backend/max_tools.py@@ -220,21 +220,26 @@ def _build_targeting_conditions(SURVEY_CREATION_TOOL_DESCRIPTION = dedent("""- Create and optionally launch an in-app survey.+ Create and optionally launch a survey.# When to use- The user wants to create a new survey- The user mentions NPS, CSAT, PMF, or feedback surveys+ - The user wants a hosted survey with a shareable link# Design principles- These are in-app surveys shown as overlays. Keep to 1-3 questions.- DO NOT set should_launch=true unless the user explicitly asks to launch.+ Keep to 1-3 questions. DO NOT set should_launch=true unless the user explicitly asks to launch.# Survey types- - "popover" (default): small overlay that appears on the page — use this for most surveys- - "widget": persistent tab/button on the page edge, good for always-available feedback+ - "popover" (default): small in-app overlay that appears on the page — use this for most surveys+ - "widget": persistent in-app tab/button on the page edge, good for always-available feedback+ - "external_survey": a hosted standalone page with a shareable public link — use this when the+ user wants to send a survey via a link (email, social, QR code) rather than show it inside their app- "api": headless, no UI — for custom implementations only- Note: hosted surveys (standalone pages with a shareable link) are not yet supported by this tool. If a user asks, let them know it's coming soon.++ # Targeting+ Targeting (target_url, feature flags, device types, wait period) only applies to in-app surveys.+ It does not apply to "external_survey" hosted surveys and is ignored for them.# Semantic question types- "nps": 0-10 numeric rating (Net Promoter Score)@@ -258,7 +263,9 @@ class CreateSurveyToolArgs(BaseModel):name: str = Field(description="Survey name")description: str = Field(default="", description="Brief survey description")questions: list[SimpleSurveyQuestion] = Field(description="List of questions")- survey_type: Literal["popover", "widget", "api"] = Field(default="popover", description="Survey display type")+ survey_type: Literal["popover", "widget", "external_survey", "api"] = Field(+ default="popover", description="Survey display type"+ )should_launch: bool = Field(default=False, description="Launch immediately after creation")target_url: str | None = Field(default=None, description="URL path to target (e.g. '/pricing')")target_url_match: Literal["exact", "contains", "regex"] | None = Field(@@ -313,15 +320,20 @@ class CreateSurveyTool(MaxTool):"enable_partial_responses": True,}- if linked_flag_id is not None:- survey_data["linked_flag_id"] = linked_flag_id-if responses_limit is not None:survey_data["responses_limit"] = responses_limit- conditions = _build_targeting_conditions(target_url, target_url_match, linked_flag_variant, wait_period_days)- if conditions:- survey_data["conditions"] = conditions+ # Hosted (external) surveys are standalone pages with no in-app display context, so the+ # backend rejects flag/URL/device targeting on them. Skip targeting entirely for those.+ if survey_type != Survey.SurveyType.EXTERNAL_SURVEY:+ if linked_flag_id is not None:+ survey_data["linked_flag_id"] = linked_flag_id++ conditions = _build_targeting_conditions(+ target_url, target_url_match, linked_flag_variant, wait_period_days+ )+ if conditions:+ survey_data["conditions"] = conditionsif self.context.get("insight_id"):survey_data["linked_insight_id"] = self.context["insight_id"]@@ -374,7 +386,18 @@ class CreateSurveyTool(MaxTool):survey_url = f"/surveys/guided/{survey_id}"else:survey_url = f"/surveys/{survey_id}?edit=true"- return f"Survey '{created_survey.name}' created{launch_msg} successfully! [View survey]({survey_url})", {++ message = f"Survey '{created_survey.name}' created{launch_msg} successfully! [View survey]({survey_url})"+ if survey_type == Survey.SurveyType.EXTERNAL_SURVEY:+ # The public shareable link only works once the survey is running.+ share_hint = (+ "Its public shareable link is on the survey page."+ if should_launch+ else "Launch it to activate its public shareable link, which you'll find on the survey page."+ )+ message = f"{message} {share_hint}"++ return message, {"survey_id": created_survey.id,"survey_name": created_survey.name,"survey_type": survey_type,diff --git a/products/surveys/backend/test_max_tools.py b/products/surveys/backend/test_max_tools.pyindex 7b446779d5c5..e1d5a1a33bf8 100644--- a/products/surveys/backend/test_max_tools.py+++ b/products/surveys/backend/test_max_tools.py@@ -344,6 +344,54 @@ class TestSurveyCreatorTool(BaseTest):assert survey.conditions["url"] == "/pricing"assert survey.conditions["urlMatchType"] == "icontains"+ @pytest.mark.django_db+ @pytest.mark.asyncio+ async def test_create_external_survey(self):+ tool = self._setup_tool()++ content, artifact = await tool._arun_impl(+ name="Hosted Feedback",+ questions=[SimpleSurveyQuestion(type="open", question="How was your experience?")],+ survey_type="external_survey",+ )++ assert "successfully" in content+ assert "shareable link" in content+ assert artifact["survey_type"] == "external_survey"++ survey = await sync_to_async(Survey.objects.get)(id=artifact["survey_id"])+ assert survey.type == "external_survey"++ @pytest.mark.django_db+ @pytest.mark.asyncio+ async def test_create_external_survey_ignores_in_app_targeting(self):+ tool = self._setup_tool()++ # Targeting is invalid for external surveys server-side; the tool must drop it rather than+ # pass it through and trigger a validation error.+ flag = await sync_to_async(FeatureFlag.objects.create)(+ team=self.team,+ key="hosted-flag",+ name="Hosted Flag",+ created_by=self.user,+ )++ content, artifact = await tool._arun_impl(+ name="Hosted Feedback",+ questions=[SimpleSurveyQuestion(type="open", question="How was your experience?")],+ survey_type="external_survey",+ target_url="/pricing",+ target_url_match="contains",+ linked_flag_id=flag.id,+ wait_period_days=7,+ )++ assert "successfully" in content+ survey = await sync_to_async(Survey.objects.get)(id=artifact["survey_id"])+ assert survey.type == "external_survey"+ assert survey.linked_flag_id is None+ assert not survey.conditions+@pytest.mark.django_db@pytest.mark.asyncioasync def test_create_survey_sanitizes_question_html(self):