: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
fix(workflows): favor revenue moments and personalize idea emails (#119927)
Daniel Marchuka56c4bf51c57
diff --git a/products/workflows/backend/services/data_suggestions/builder.py b/products/workflows/backend/services/data_suggestions/builder.pyindex b104355a920c..936c9dbc93cf 100644--- a/products/workflows/backend/services/data_suggestions/builder.py+++ b/products/workflows/backend/services/data_suggestions/builder.py@@ -8,7 +8,7 @@ from posthog.dataclasses import frozenfrom products.messaging.backend.facade.api import EmailTemplateContentfrom products.workflows.backend.facade.contracts import MissingInput-from products.workflows.backend.services.data_suggestions.email_design import EmailCopy, render_email+from products.workflows.backend.services.data_suggestions.email_design import EmailCopy, render_email, with_variablesfrom products.workflows.backend.services.data_suggestions.planner import (MAX_PLANNED_STEPS,Channel,@@ -41,6 +41,7 @@ class BuildContext:sender: EmailSender | Noneslack_integration_id: int | Noneonce_per_person: bool = False+ event_properties: frozenset[str] = frozenset()@frozen@@ -78,8 +79,11 @@ def build_workflow(idea: WorkflowIdea, plan: PlannedWorkflow, context: BuildContlinks: dict[str, list[_Link]] = {}for step in steps:step_links = _links(step)- if step_links is None or any(link.target is not None and link.target not in steps_by_id for link in step_links):- _log_rejection("step links to an unknown step", step_id=step.id, step_type=step.type)+ unknown = [+ link.target for link in step_links or [] if link.target is not None and link.target not in steps_by_id+ ]+ if step_links is None or unknown:+ _log_rejection("step links to an unknown step", step_id=step.id, step_type=step.type, targets=unknown)return Nonelinks[step.id] = step_linksif not _is_acyclic_and_connected(plan.first_step, links):@@ -323,10 +327,12 @@ def _build_email_step(step: PlannedStep, action_id: str, context: BuildContext)subject=clean_copy(step.email_subject),heading=clean_copy(step.email_heading),paragraphs=tuple(clean_copy(paragraph) for paragraph in step.email_paragraphs[:3]),+ variables=context.event_properties,))body = {"subject": rendered.subject, "html": rendered.html, "text": rendered.text, "design": rendered.design}- name = rendered.subject+ # The step name is cut to fit the canvas, so it takes the fallback words rather than a Liquid tag.+ name = with_variables(clean_copy(step.email_subject), frozenset())sender = context.senderreturn {diff --git a/products/workflows/backend/services/data_suggestions/email_design.py b/products/workflows/backend/services/data_suggestions/email_design.pyindex d46047859b99..cf7ea7e59f0b 100644--- a/products/workflows/backend/services/data_suggestions/email_design.py+++ b/products/workflows/backend/services/data_suggestions/email_design.py@@ -1,4 +1,6 @@-from html import escape+import re+from collections.abc import Callable+from html import escape, unescapefrom posthog.dataclasses import frozen@@ -10,6 +12,8 @@ class EmailCopy:subject: strheading: strparagraphs: tuple[str, ...]+ # The trigger event's properties the copy may show, written as [[name|fallback]].+ variables: frozenset[str] = frozenset()@frozen@@ -20,9 +24,45 @@ class RenderedEmail:design: dict+_VARIABLE = re.compile(r"\[\[\s*([A-Za-z_][A-Za-z0-9_]*)\s*(?:\|([^\[\]|]*))?\]\]")+++def _unescape_fully(value: str) -> str:+ while (decoded := unescape(value)) != value:+ value = decoded+ return value+++def with_variables(+ text: str, variables: frozenset[str], escape_text: Callable[[str], str] = str, raw: bool = False+) -> str:+ """Turns [[name|fallback]] into a Liquid variable on the trigger event. An unknown name keeps only its fallback,+ so the model can never reach person data or other events. Pass raw for subjects and plain text, which the send+ would otherwise HTML-escape."""++ def replace(match: re.Match[str]) -> str:+ name = match.group(1)+ # Stripped after decoding and before escaping: the send decodes entities inside a Liquid tag, so an encoded+ # quote or a trailing backslash would end the default early.+ fallback = escape_text(re.sub(r"['\"{}%\\]", "", _unescape_fully(match.group(2) or "")).strip())+ if name not in variables:+ return fallback+ return f"{{{{ event.properties.{name} | default: '{fallback}'{' | raw' if raw else ''} }}}}"++ parts: list[str] = []+ end = 0+ for match in _VARIABLE.finditer(text):+ parts.append(escape_text(text[end : match.start()]))+ parts.append(replace(match))+ end = match.end()+ parts.append(escape_text(text[end:]))+ return "".join(parts).replace("[[", "").replace("]]", "")++def render_email(copy: EmailCopy) -> RenderedEmail:paragraphs = "".join(- f'<p style="margin:0 0 16px;font-size:16px;line-height:24px;color:#3b3d45">{escape(paragraph)}</p>'+ f'<p style="margin:0 0 16px;font-size:16px;line-height:24px;color:#3b3d45">'+ f"{with_variables(paragraph, copy.variables, escape)}</p>"for paragraph in copy.paragraphs)html = (@@ -31,12 +71,14 @@ def render_email(copy: EmailCopy) -> RenderedEmail:'<table role="presentation" width="100%" cellpadding="0" cellspacing="0" ''style="max-width:560px;margin:0 auto;background:#ffffff;border-radius:8px">''<tr><td style="padding:32px">'- f'<h1 style="margin:0 0 16px;font-size:24px;line-height:32px;color:#1d1f27">{escape(copy.heading)}</h1>'+ f'<h1 style="margin:0 0 16px;font-size:24px;line-height:32px;color:#1d1f27">'+ f"{with_variables(copy.heading, copy.variables, escape)}</h1>"f"{paragraphs}""</td></tr>"'<tr><td style="padding:16px 32px 32px;font-size:12px;color:#6b6e76">''<a href="{{ unsubscribe_url }}" style="color:#6b6e76">Unsubscribe</a></td></tr>'"</table></body></html>")- text = "\n\n".join([copy.heading, *copy.paragraphs])- return RenderedEmail(subject=copy.subject, html=html, text=text, design=build_html_wrap_design(html))+ text = with_variables("\n\n".join([copy.heading, *copy.paragraphs]), copy.variables, raw=True)+ subject = with_variables(copy.subject, copy.variables, raw=True)+ return RenderedEmail(subject=subject, html=html, text=text, design=build_html_wrap_design(html))diff --git a/products/workflows/backend/services/data_suggestions/planner.py b/products/workflows/backend/services/data_suggestions/planner.pyindex f47d45d62d9f..d6f8ea3e0fff 100644--- a/products/workflows/backend/services/data_suggestions/planner.py+++ b/products/workflows/backend/services/data_suggestions/planner.py@@ -119,6 +119,8 @@ class StepsContext:events: tuple[str, ...]channels: frozenset[Channel]has_slack: bool+ # The trigger event's own properties, like a product name or an amount, that an email can show.+ event_properties: tuple[str, ...] = ()_SIMPLE_RULE = (@@ -132,6 +134,13 @@ _ONBOARDING_RULE = ("even when other events fire more often. Then prefer other meaningful moments.")+_REVENUE_RULE = (+ "Prefer moments where a message earns or saves money: a trial that ends soon, a cart or checkout left "+ "unfinished, a failed payment, a cancellation or pause to win back, and a sign-up that has not reached its "+ "first key action. Prefer them over thank-you, feedback and review requests. Prefer events that happen every "+ "week over rare ones, so the workflow keeps sending."+)+_COPY_RULES = "Write plain, friendly copy in sentence case. No emojis, no em dashes, no placeholders like [Name], no template tags."_IDEAS_PROMPT = f"""You suggest marketing and automation workflows for a product team using PostHog Workflows.@@ -140,6 +149,7 @@ Suggest up to 6 workflows the project does not have yet. Each one starts when onRules:- trigger_event must be copied exactly from the event list.- {_ONBOARDING_RULE}+- {_REVENUE_RULE}- Each idea must use a different trigger_event.- Email is the main way to reach people. {{channel_rule}} {{slack_rule}}- Use the steps the workflow needs, including waits for an event and branches. {_SIMPLE_RULE}@@ -166,6 +176,12 @@ Rules:- Steps form a graph: next, branches and wait_event_next point at step ids. Never point back at an earlier step.- Leave settings you cannot know blank: API keys, URLs, channels, phone numbers, property filters and \recipients. The team fills them in. Say what is left in setup_note.+- A wait_until_event step waits for the event that shows the workflow reached its goal for this person, like a \+successful payment after a failed one, or an order after an abandoned cart. Use it only when an event in the list \+clearly means that. Another event of the same kind is not enough: a paid invoice from the person's own customer is not \+their subscription payment. When no event means it, use a delay instead.+- {{personalization_rule}}+- Never promise a discount, offer or gift. The team adds one if it has one.- {{email_rule}}- {{channel_rule}}- {{slack_rule}}@@ -225,6 +241,15 @@ def plan_steps(if context.has_slackelse "The team has not connected Slack yet. A slack step is fine, the team connects it on the step.")+ personalization_rule = (+ "The trigger event carries the properties listed in trigger_event_properties. To show one in an email, write "+ "[[property|fallback words]], for example [[product_name|your items]]. The fallback shows when the event has "+ "no value. Use a property where it makes the email specific, like naming the product, plan or amount. Never show a "+ "code, id or error value, which reads as noise to a customer. Never write a value yourself. This is the only "+ "placeholder allowed."+ if context.event_properties+ else "Do not mention a product name, price or amount you cannot know."+ )idea = context.ideahuman = as_untrusted_data("workflow",@@ -239,6 +264,12 @@ def plan_steps(human += "\n\n" + as_untrusted_data("project_events", list(context.events), source="event names sent by this project's app")+ if context.event_properties:+ human += "\n\n" + as_untrusted_data(+ "trigger_event_properties",+ list(context.event_properties),+ source="property names this project sends on the trigger event",+ )human += "\n\n" + as_untrusted_data("step_catalog",[@@ -267,6 +298,7 @@ def plan_steps(email_rule=email_rule,channel_rule=_channel_rule(context.channels),slack_rule=slack_rule,+ personalization_rule=personalization_rule,max_steps=MAX_PLANNED_STEPS,),excluded_values=_missing_channels(context.channels),diff --git a/products/workflows/backend/services/data_suggestions/ranking.py b/products/workflows/backend/services/data_suggestions/ranking.pyindex 58233b1b29a2..cbe433323ddf 100644--- a/products/workflows/backend/services/data_suggestions/ranking.py+++ b/products/workflows/backend/services/data_suggestions/ranking.py@@ -30,7 +30,7 @@ _STAGE_REGEXES: tuple[tuple[LifecycleStage, str], ...] = (("signup", r"sign.?up|signed.?up|regist|(account|user|workspace|organi[sz]ation|team).?created"),("onboarding", r"onboard|activat|welcome|invite"),("trial", r"trial"),- ("churn_risk", r"cancel|churn|downgrad|unsubscri|(account|user|workspace|organi[sz]ation).?delet"),+ ("churn_risk", r"cancel|churn|downgrad|unsubscri|paus|(account|user|workspace|organi[sz]ation).?delet"),("failure", r"fail|error"),("purchase", r"subscri|upgrad|purchas|order|checkout|payment|paid|plan"),("support", r"ticket|survey|feedback"),diff --git a/products/workflows/backend/services/data_suggestions/service.py b/products/workflows/backend/services/data_suggestions/service.pyindex 10ce0d0c050e..e48e20363254 100644--- a/products/workflows/backend/services/data_suggestions/service.py+++ b/products/workflows/backend/services/data_suggestions/service.py@@ -71,6 +71,7 @@ FAILURE_CACHE_TTL_SECONDS = 5 * 60_GENERATION_LOCK_SECONDS = 15 * 60# A built draft is kept this long, so a retry after a failed save does not pay for another plan._BUILD_CACHE_SECONDS = 60 * 60+_PLAN_ATTEMPTS = 2MAX_SUGGESTIONS = 3_PROMPT_EVENTS_LIMIT = 60_GENERATION_ERRORS = (@@ -289,36 +290,44 @@ def _plan_and_build(slack_integration_id = team_context.slack_integration_id(team)step_templates = tuple(list_step_templates(team))channels = team_context.channels(team)- with _analytics_client() as analytics_client:- plan = plan_steps(- team=team,- user=user,- context=StepsContext(- idea=idea,- email_templates=email_templates,- step_templates=step_templates,- events=events,- channels=channels,- has_slack=slack_integration_id is not None,- ),- analytics_client=analytics_client,- )- if plan is None:- return _BUILD_FAILED, None-- built = build_workflow(- idea,- plan,- BuildContext(- allowed_events=frozenset({suggestion.trigger_event, *events}),- step_template_ids=frozenset(template.template_id for template in step_templates),- channels=channels,- email_templates=_chosen_template_bodies(team, plan, email_templates),- sender=team_context.email_sender(team),- slack_integration_id=slack_integration_id,- once_per_person=suggestion.stage in _ONCE_PER_PERSON_STAGES,- ),+ event_properties = team_context.event_property_names(team, suggestion.trigger_event)+ context = BuildContext(+ allowed_events=frozenset({suggestion.trigger_event, *events}),+ step_template_ids=frozenset(template.template_id for template in step_templates),+ channels=channels,+ email_templates={},+ sender=team_context.email_sender(team),+ slack_integration_id=slack_integration_id,+ once_per_person=suggestion.stage in _ONCE_PER_PERSON_STAGES,+ event_properties=frozenset(event_properties),)+ built = None+ # The model now and then links a step to an id it never wrote. A second plan almost always holds up.+ for _ in range(_PLAN_ATTEMPTS):+ with _analytics_client() as analytics_client:+ plan = plan_steps(+ team=team,+ user=user,+ context=StepsContext(+ idea=idea,+ email_templates=email_templates,+ step_templates=step_templates,+ events=events,+ channels=channels,+ has_slack=slack_integration_id is not None,+ event_properties=event_properties,+ ),+ analytics_client=analytics_client,+ )+ if plan is None:+ return _BUILD_FAILED, None+ built = build_workflow(+ idea,+ plan,+ dataclasses.replace(context, email_templates=_chosen_template_bodies(team, plan, email_templates)),+ )+ if built is not None:+ breakif built is None:return _BUILD_FAILED, Nonevalidated = validate_draft(team, built.workflow)@@ -518,7 +527,9 @@ def _generate(*, team: Team, user: User, analytics_client: Any) -> DataSuggestiodef _suggest(*, team: Team, user: User, analytics_client: Any) -> DataSuggestionsResult:used_events = team_context.used_trigger_events(team)- all_counts = weekly_counts(team)+ # The model can copy an event name into its JSON answer without escaping its quotes, which breaks the answer and+ # loses every idea of the run. Names that JSON must escape are left out.+ all_counts = {name: count for name, count in weekly_counts(team).items() if '"' not in name and "\\" not in name}# A build can wait on any busy event, including one another workflow already starts from.planning_events = tuple(name for name, _ in select_prompt_events(all_counts, limit=_PROMPT_EVENTS_LIMIT))counts = {name: count for name, count in all_counts.items() if name not in used_events}diff --git a/products/workflows/backend/services/data_suggestions/stages.py b/products/workflows/backend/services/data_suggestions/stages.pyindex 3315992e50ce..682e0762454e 100644--- a/products/workflows/backend/services/data_suggestions/stages.py+++ b/products/workflows/backend/services/data_suggestions/stages.py@@ -89,8 +89,11 @@ def classify_event_stages(*, team_id: int, event_names: Sequence[str]) -> dict[sif isinstance(answer, ChoiceAnswer) and answer.choice in valid_stages:required = MIN_CONFIDENCE_BY_STAGE.get(answer.choice, MIN_CONFIDENCE)confident = answer.confidence >= required+ # When the name also looks like the stage Jev picked, the answer is kept even at low confidence.+ # Otherwise a plain `account created` that Jev is unsure about reads as routine usage.+ looks_like = guess_stage(name) == answer.choice# Jev can call a busy usage event a sign-up, and its volume then outranks every real moment. The two# highest weights need the event name to look like the stage too.- agrees = answer.choice not in MIN_CONFIDENCE_BY_STAGE or guess_stage(name) == answer.choice- stages[name] = cast(LifecycleStage, answer.choice) if confident and agrees else "other"+ agrees = answer.choice not in MIN_CONFIDENCE_BY_STAGE or looks_like+ stages[name] = cast(LifecycleStage, answer.choice) if looks_like or (confident and agrees) else "other"return stagesdiff --git a/products/workflows/backend/services/data_suggestions/team_context.py b/products/workflows/backend/services/data_suggestions/team_context.pyindex f0ed5238da77..3049253397b1 100644--- a/products/workflows/backend/services/data_suggestions/team_context.py+++ b/products/workflows/backend/services/data_suggestions/team_context.py@@ -1,8 +1,12 @@"""What the team already has: workflows, integrations and saved email templates."""+import refrom typing import Any-from posthog.models import Team+from django.db.models import BigIntegerField, F+from django.db.models.functions import Coalesce++from posthog.models import EventProperty, Teamfrom posthog.models.integration import Integrationfrom products.messaging.backend.facade.api import EmailTemplateSummary, list_email_templates@@ -12,6 +16,9 @@ from products.workflows.backend.services.data_suggestions.builder import EmailSefrom products.workflows.backend.services.data_suggestions.planner import Channel_TEMPLATES_LIMIT = 20+_EVENT_PROPERTIES_LIMIT = 15+# Only plain names, so the email's Liquid variable needs no quoting.+_PLAIN_PROPERTY_NAME = re.compile(r"^[A-Za-z_][A-Za-z0-9_]{0,63}$")def used_trigger_events(team: Team) -> set[str]:@@ -99,3 +106,19 @@ def _verified_senders(team: Team) -> list[EmailSender]:def email_templates(team: Team) -> tuple[EmailTemplateSummary, ...]:return tuple(list_email_templates(team.id, limit=_TEMPLATES_LIMIT))+++def event_property_names(team: Team, event: str) -> tuple[str, ...]:+ """The properties the project sends on `event`, except PostHog's own `$` properties."""+ names = (+ # Matches the leading column of the (coalesce(project_id, team_id), event, property) unique index, so the+ # lookup is an index seek.+ EventProperty.objects.alias(+ effective_project_id=Coalesce(F("project_id"), F("team_id"), output_field=BigIntegerField())+ )+ .filter(effective_project_id=team.project_id, event=event)+ .exclude(property__startswith="$")+ .order_by("property")+ .values_list("property", flat=True)[:100]+ )+ return tuple(name for name in names if _PLAIN_PROPERTY_NAME.match(name))[:_EVENT_PROPERTIES_LIMIT]diff --git a/products/workflows/backend/test/test_data_suggestions.py b/products/workflows/backend/test/test_data_suggestions.pyindex 13cb2a9b169d..175eb0e59849 100644--- a/products/workflows/backend/test/test_data_suggestions.py+++ b/products/workflows/backend/test/test_data_suggestions.py@@ -289,6 +289,12 @@ class TestDataSuggestions(SimpleTestCase):lambda inputs: inputs["text"]["value"],"{person",),+ (+ "a placeholder for a property the trigger event does not carry",+ step(email_paragraphs=["You left [[secret|an item]] behind"]),+ lambda inputs: inputs["email"]["value"]["html"],+ "secret",+ ),])def test_build_workflow_strips_template_tags_from_model_copy(@@ -300,6 +306,37 @@ class TestDataSuggestions(SimpleTestCase):assert leftover not in copyassert "{%" not in copy+ def test_build_workflow_shows_trigger_event_properties_through_liquid(self) -> None:+ planned = step(+ email_subject="You left [[product_name|something]] behind",+ email_paragraphs=[+ "Your [[product_name|items]] cost [[price|less than you think]].",+ "Pick [[product_name|your team's choice]] or [[price|Tom's deal]].",+ "See [[product_name|Tom's plan\\]] today.",+ ],+ )++ built = build_workflow(IDEA, plan(planned), context(event_properties=frozenset({"product_name"})))++ assert built is not None+ email = built.workflow["actions"][1]+ assert email["config"]["inputs"]["email"]["value"]["subject"] == (+ "You left {{ event.properties.product_name | default: 'something' | raw }} behind"+ )+ assert (+ "See {{ event.properties.product_name | default: 'Toms plan' | raw }} today."+ in (email["config"]["inputs"]["email"]["value"]["text"])+ )+ assert (+ "Your {{ event.properties.product_name | default: 'items' }} cost less than you think."+ in (email["config"]["inputs"]["email"]["value"]["html"])+ )+ assert (+ "Pick {{ event.properties.product_name | default: 'your teams choice' }} or Toms deal."+ in (email["config"]["inputs"]["email"]["value"]["html"])+ )+ assert email["name"] == "You left something behind"+@parameterized.expand([("starts with a capital", "welcome new trial users", "Welcome new trial users"),@@ -366,18 +403,30 @@ class TestDataSuggestions(SimpleTestCase):"e2": ChoiceAnswer(choice="signup", confidence=0.6, probabilities={"signup": 0.6}),"e3": ChoiceAnswer(choice="failure", confidence=0.6, probabilities={"failure": 0.6}),"e4": ChoiceAnswer(choice="signup", confidence=0.9, probabilities={"signup": 0.9}),+ "e5": ChoiceAnswer(choice="signup", confidence=0.1, probabilities={"signup": 0.1}),+ "e6": ChoiceAnswer(choice="churn_risk", confidence=0.3, probabilities={"churn_risk": 0.3}),},input_tokens=10,)assert classify_event_stages(team_id=1,- event_names=["account_created", "thing_happened", "summary_generated", "upload_failed", "query executed"],+ event_names=[+ "account_created",+ "thing_happened",+ "summary_generated",+ "upload_failed",+ "query executed",+ "workspace created",+ "subscription paused",+ ],) == {"account_created": "signup","thing_happened": "other","summary_generated": "other","upload_failed": "failure","query executed": "other",+ "workspace created": "signup",+ "subscription paused": "churn_risk",}@parameterized.expand([("jev is not configured", True), ("jev does not answer", False)])diff --git a/products/workflows/backend/tests/api/test_workflow_data_suggestions.py b/products/workflows/backend/tests/api/test_workflow_data_suggestions.pyindex 624cf828a1d2..b9d54cbd73f2 100644--- a/products/workflows/backend/tests/api/test_workflow_data_suggestions.py+++ b/products/workflows/backend/tests/api/test_workflow_data_suggestions.py@@ -137,6 +137,9 @@ class TestWorkflowDataSuggestions(ClickhouseTestMixin, APIBaseTest):@patch(f"{SERVICE}.suggest_ideas")def test_suggests_only_busy_events_and_caches_the_answer(self, suggest_ideas: MagicMock, _flag) -> None:suggest_ideas.return_value = [_idea("trial_started"), _idea("made_up_event")]+ for index in range(3):+ _create_event(team=self.team, event='clicked "buy"', distinct_id=f"person-{index}")+ flush_persons_and_events()body = self._list()@@ -754,6 +757,27 @@ class TestWorkflowDataSuggestions(ClickhouseTestMixin, APIBaseTest):assert actions[0]["config"]["inputs"]["token"] == {"value": "<set>"}assert "super-secret" not in str(actions)+ @patch(f"{SERVICE}.plan_steps")+ @patch(f"{SERVICE}.suggest_ideas")+ def test_a_plan_that_links_to_a_missing_step_is_planned_again(+ self, suggest_ideas: MagicMock, plan_steps: MagicMock, _flag+ ) -> None:+ suggest_ideas.return_value = [_idea("trial_started")]+ plan_steps.side_effect = [+ PlannedWorkflow(first_step="welcome", steps=[_email_step("welcome", "Welcome", next="never_written")]),+ PlannedWorkflow(first_step="welcome", steps=[_email_step("welcome", "Welcome")]),+ ]+ suggestion_id = self._list()["suggestions"][0]["id"]++ built = self.client.post(+ f"/api/projects/{self.team.id}/workflow_data_suggestions/build/",+ {"suggestion_id": suggestion_id},+ format="json",+ ).json()++ assert built["status"] == "ready"+ assert plan_steps.call_count == 2+@patch(f"{SERVICE}.plan_steps")@patch(f"{SERVICE}.suggest_ideas")def test_a_saved_build_is_read_again_from_its_draft(