Skip to content

: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.

fix(data-warehouse): unblock google analytics setup on custom report json (#121193)

Tom Owers159fef35d878
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/settings.py
index 2d91a84a38c6..277dd0dc8c5b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/settings.py
@@ -215,11 +215,18 @@ def parse_custom_reports(custom_reports_json: str | None) -> dict[str, GoogleAna
parsed = json.loads(custom_reports_json)
except json.JSONDecodeError as e:
raise CustomReportError(
- "Custom reports must be valid JSON. Provide a JSON array of report objects, then try again."
+ "Custom reports must be valid JSON. Fix the JSON, or leave the field empty to sync only the built-in reports."
) from e
+ # A single report pasted without the surrounding brackets is unambiguous, so accept it.
+ if isinstance(parsed, dict):
+ parsed = [parsed]
+
if not isinstance(parsed, list):
- raise CustomReportError("Custom reports must be a JSON array of report objects.")
+ raise CustomReportError(
+ "Custom reports must be a JSON array of report objects. Wrap your reports in [ ], "
+ "or leave the field empty to sync only the built-in reports."
+ )
reports: dict[str, GoogleAnalyticsReportSchema] = {}
for entry in parsed:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/tests/test_google_analytics_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/tests/test_google_analytics_source.py
index b372d69655fe..05a3c45c7b77 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/tests/test_google_analytics_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/tests/test_google_analytics_source.py
@@ -30,7 +30,7 @@ def _config(property_id: str = "123456789", custom_reports: str | None = None) -
"custom_reports,expected_substring",
[
("not json", "valid JSON"),
- ('{"name": "x"}', "JSON array"),
+ ('"paid_campaigns"', "JSON array"),
('[{"dimensions": ["a"], "metrics": ["sessions"]}]', "non-empty 'name'"),
('[{"name": "website_overview", "dimensions": [], "metrics": ["sessions"]}]', "already a built-in report"),
(
@@ -56,6 +56,20 @@ def test_parse_custom_reports_rejects_invalid(custom_reports, expected_substring
assert expected_substring in str(exc.value)
+ "custom_reports",
+ [
+ '[{"name": "paid_campaigns", "dimensions": ["sessionSource"], "metrics": ["sessions"]}]',
+ '{"name": "paid_campaigns", "dimensions": ["sessionSource"], "metrics": ["sessions"]}',
+ ],
+)
+def test_parse_custom_reports_accepts_array_or_single_object(custom_reports):
+ reports = parse_custom_reports(custom_reports)
+ assert list(reports) == ["paid_campaigns"]
+ assert reports["paid_campaigns"]["dimensions"] == ["date", "sessionSource"]
+ assert reports["paid_campaigns"]["metrics"] == ["sessions"]
+
+
def test_parse_custom_reports_invalid_json_hides_parser_detail():
# The raw JSONDecodeError text (e.g. "Expecting value: line 1 column 1 (char 0)") is debug
# noise for the user pasting a report, so the message stays actionable without echoing any
@@ -64,7 +78,10 @@ def test_parse_custom_reports_invalid_json_hides_parser_detail():
with pytest.raises(CustomReportError) as exc:
parse_custom_reports("not json")
message = str(exc.value)
- assert message == "Custom reports must be valid JSON. Provide a JSON array of report objects, then try again."
+ assert (
+ message
+ == "Custom reports must be valid JSON. Fix the JSON, or leave the field empty to sync only the built-in reports."
+ )
assert "column" not in message
assert "char" not in message
assert "Expecting value" not in message