: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(email): let the workflows email flag target each region (#123060)
Harley Alexander6b047b9e3cff
diff --git a/posthog/email.py b/posthog/email.pyindex 885853852055..e81fabf2477a 100644--- a/posthog/email.py+++ b/posthog/email.py@@ -210,8 +210,9 @@ WORKFLOWS_TEMPLATE_ID_MAP: dict[str, str] = {"loop_run_summary": "01a16473-9c4d-0000-99d8-958c03b0f1a0",}-# Release conditions match the `email` and `transactional_email_template` person properties passed at evaluation,-# so one flag picks both the recipients and the templates that move.+# Release conditions match the `email`, `transactional_email_template` and `region` person properties passed at+# evaluation, so one flag picks the recipients, the templates that move and the instances that move them. Both regions+# send to the US project, so a condition on `region` lets each one roll out on its own.WORKFLOWS_TRANSACTIONAL_EMAIL_FLAG = "transactional-emails-via-workflows"# Matches how long the send API keeps an idempotency key, which outlasts every Celery retry.@@ -315,11 +316,14 @@ def get_workflows_template_id(return Noneif pinned_template_id:return pinned_template_id+ person_properties = {"email": dest["raw_email"], "transactional_email_template": template_name}+ if region := get_instance_region():+ person_properties["region"] = regiontry:enabled = posthoganalytics.feature_enabled(WORKFLOWS_TRANSACTIONAL_EMAIL_FLAG,dest.get("distinct_id") or dest["raw_email"],- person_properties={"email": dest["raw_email"], "transactional_email_template": template_name},+ person_properties=person_properties,send_feature_flag_events=False,)except Exception as err:diff --git a/posthog/test/test_email.py b/posthog/test/test_email.pyindex 408b0c2d90e1..2695f126c7e2 100644--- a/posthog/test/test_email.py+++ b/posthog/test/test_email.py@@ -32,6 +32,7 @@ from posthog.email import (_send_via_http,_send_via_smtp,get_email_team_and_org_context,+ get_workflows_template_id,sanitize_email_properties,)from posthog.models import Organization, Person, Team, User@@ -407,6 +408,27 @@ class TestEmail(BaseTest):record = MessagingRecord.objects.filter(campaign_key="test_campaign").first()self.assertIsNone(record)+ @parameterized.expand([("us", "US"), ("eu", "EU"), ("self_hosted", None)])+ @patch("posthog.email.posthoganalytics.feature_enabled", return_value=True)+ def test_workflows_flag_sees_the_instance_region(+ self, _name: str, region: str | None, mock_feature_enabled: MagicMock+ ) -> None:+ dest = {"raw_email": "[email protected]", "distinct_id": "user-1"}+ with (+ self.settings(+ CLOUD_DEPLOYMENT=region,+ POSTHOG_TRANSACTIONAL_EMAIL_API_KEY="phs_test",+ POSTHOG_HOST="https://us.posthog.com",+ ),+ patch.dict("posthog.email.WORKFLOWS_TEMPLATE_ID_MAP", {"password_reset": "tmpl-1"}),+ ):+ self.assertEqual(get_workflows_template_id("password_reset", dest), "tmpl-1")++ expected = {"email": "[email protected]", "transactional_email_template": "password_reset"}+ if region:+ expected["region"] = region+ self.assertEqual(mock_feature_enabled.call_args.kwargs["person_properties"], expected)+@parameterized.expand([("workflows_accepts", True, [202], ["workflows"], "workflows", ["transactional email triggered"]),