: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(marketing-analytics): apply organic fallback to warehouse conversion goals (#74055)
Javier Bahamondes9c73a6da3d79
diff --git a/products/marketing_analytics/backend/hogql_queries/conversion_goal_processor.py b/products/marketing_analytics/backend/hogql_queries/conversion_goal_processor.pyindex 360beedbad9d..41c180bada4f 100644--- a/products/marketing_analytics/backend/hogql_queries/conversion_goal_processor.py+++ b/products/marketing_analytics/backend/hogql_queries/conversion_goal_processor.py@@ -2114,6 +2114,21 @@ class ConversionGoalProcessor:],)+ def _apply_organic_default(self, expr: ast.Expr, default_value: str) -> ast.Call:+ """Fall back to the organic default when the value is NULL or empty, matching the+ events attribution path so all goal kinds classify unattributed conversions alike."""+ return ast.Call(+ name="if",+ args=[+ ast.Call(+ name="notEmpty",+ args=[ast.Call(name="coalesce", args=[expr, ast.Constant(value="")])],+ ),+ expr,+ ast.Constant(value=default_value),+ ],+ )+def _resolve_direct_field_expr(self, field: TrackedField, table: str) -> ast.Expr:"""Resolve a tracked field to an AST expression for direct queries (no attribution pipeline)."""if self.goal.kind in ["EventsNode", "ActionsNode"]:@@ -2200,11 +2215,9 @@ class ConversionGoalProcessor:where_conditions.extend(additional_conditions)# Campaign expression with organic default- campaign_expr = ast.Call(- name="coalesce", args=[utm_campaign_expr, ast.Constant(value=self.config.organic_campaign)]- )+ campaign_expr = self._apply_organic_default(utm_campaign_expr, self.config.organic_campaign)source_expr = self._normalize_source_field(- ast.Call(name="coalesce", args=[utm_source_expr, ast.Constant(value=self.config.organic_source)])+ self._apply_organic_default(utm_source_expr, self.config.organic_source))# Build field expressions for all tracked fieldsdiff --git a/products/marketing_analytics/backend/hogql_queries/test/external/warehouse_conversions_empty_utm.csv b/products/marketing_analytics/backend/hogql_queries/test/external/warehouse_conversions_empty_utm.csvnew file mode 100644index 000000000000..377229d3ac95--- /dev/null+++ b/products/marketing_analytics/backend/hogql_queries/test/external/warehouse_conversions_empty_utm.csv@@ -0,0 +1,3 @@+user_id,event_timestamp,campaign_name,source_name,revenue+dw_user_1,2023-01-10 10:00:00,,,100+dw_user_2,2023-01-12 11:00:00,summer_sale,google,250diff --git a/products/marketing_analytics/backend/hogql_queries/test_conversion_goal_processor.py b/products/marketing_analytics/backend/hogql_queries/test_conversion_goal_processor.pyindex e3a704459051..65845fff1dbd 100644--- a/products/marketing_analytics/backend/hogql_queries/test_conversion_goal_processor.py+++ b/products/marketing_analytics/backend/hogql_queries/test_conversion_goal_processor.py@@ -1,3 +1,5 @@+from pathlib import Path+import pytestfrom freezegun import freeze_timefrom posthog.test.base import BaseTest, ClickhouseTestMixin, _create_event, _create_person, events_cache_tests@@ -29,6 +31,7 @@ from products.marketing_analytics.backend.hogql_queries.conversion_goal_processoadd_conversion_goal_property_filters,)from products.marketing_analytics.backend.hogql_queries.marketing_analytics_config import MarketingAnalyticsConfig+from products.warehouse_sources.backend.facade.testing import create_data_warehouse_table_from_csvdef _create_action(**kwargs):@@ -253,6 +256,59 @@ class TestConversionGoalProcessor(ClickhouseTestMixin, BaseTest):assert processor.get_table_name() == "warehouse_table"assert processor.get_date_field() == "event_timestamp"+ def test_data_warehouse_node_empty_utm_falls_back_to_organic(self):+ csv_path = Path(__file__).parent / "test/external/warehouse_conversions_empty_utm.csv"+ table, _source, _credential, _df, cleanup_fn = create_data_warehouse_table_from_csv(+ csv_path,+ "conversions_empty_utm",+ {+ "user_id": "String",+ "event_timestamp": "DateTime",+ "campaign_name": "String",+ "source_name": "String",+ "revenue": "Int64",+ },+ "test_storage_bucket-posthog.marketing_analytics.empty_utm",+ self.team,+ )+ self.addCleanup(cleanup_fn)++ goal = ConversionGoalFilter3(+ kind="DataWarehouseNode",+ id=table.name,+ table_name=table.name,+ conversion_goal_id="warehouse_empty_utm",+ conversion_goal_name="Warehouse Empty UTM",+ math=BaseMathType.TOTAL,+ distinct_id_field="user_id",+ id_field="user_id",+ timestamp_field="event_timestamp",+ schema_map={+ "utm_campaign_name": "campaign_name",+ "utm_source_name": "source_name",+ "distinct_id_field": "user_id",+ "timestamp_field": "event_timestamp",+ },+ )+ processor = ConversionGoalProcessor(goal=goal, index=0, team=self.team, config=self.config)++ additional_conditions: list[ast.Expr] = [+ ast.CompareOperation(+ left=ast.Field(chain=["event_timestamp"]),+ op=ast.CompareOperationOp.GtEq,+ right=ast.Call(name="toDate", args=[ast.Constant(value="2023-01-01")]),+ ),+ ]+ cte_query = processor.generate_cte_query(additional_conditions)+ response = execute_hogql_query(query=cte_query, team=self.team)++ # Schema: [0]=match_key, [1]=campaign, [2]=id, [3]=source, [4]=conversion+ results = {row[1]: (row[3], row[4]) for row in response.results}+ assert results == {+ "organic": ("organic", 1),+ "summer_sale": ("google", 1),+ }, f"Empty utm columns must fall back to organic like event goals do, got {response.results}"+# ================================================================# 3. MATH TYPE TESTS - TOTAL, DAU, SUM, etc.# ================================================================