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.

feat(warehouse): track source create and edit origin (mcp/ui/api) (#66028)

Marcus Hof957a3f19e725
diff --git a/products/data_warehouse/backend/api/external_data_source.py b/products/data_warehouse/backend/api/external_data_source.py
index 692d67e0df7f..e8cdf48d43eb 100644
--- a/products/data_warehouse/backend/api/external_data_source.py
+++ b/products/data_warehouse/backend/api/external_data_source.py
@@ -38,6 +38,7 @@ from posthog.hogql.database.database import Database
from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.api.utils import action
+from posthog.event_usage import report_user_action
from posthog.exceptions_capture import capture_exception
from posthog.models.user import User
from posthog.rbac.access_control_api_mixin import AccessControlViewSetMixin
@@ -1466,6 +1467,24 @@ class ExternalDataSourceViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixi
direct_query_enabled=serializer.validated_data.get("direct_query_enabled", True),
)
+ def perform_update(self, serializer: serializers.BaseSerializer) -> None:
+ # Runs for both PUT and PATCH (DRF's partial_update delegates to update -> perform_update).
+ # `created_via` is write-once and reflects original creation origin; the edit's own origin
+ # comes from the request-derived `source` that report_user_action attaches.
+ super().perform_update(serializer)
+ instance = cast(ExternalDataSource, serializer.instance)
+ report_user_action(
+ cast(User, self.request.user),
+ "data warehouse source updated",
+ {
+ "source_type": instance.source_type,
+ "created_via": instance.created_via,
+ "source_id": str(instance.pk),
+ },
+ team=self.team,
+ request=self.request,
+ )
+
def _create_external_data_source(
self,
request: Request,
@@ -1978,6 +1997,24 @@ class ExternalDataSourceViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixi
managed_viewset.sync_views()
ensure_person_join(self.team.pk, new_source_model.prefix)
+ # `source` (web/api/mcp) is derived from the request by report_user_action; `created_via`
+ # is the caller's explicit intent. They usually agree but are kept separate so a transport
+ # change (e.g. a new wrapper UA) doesn't silently rewrite historical attribution.
+ report_user_action(
+ cast(User, request.user),
+ "data warehouse source created",
+ {
+ "source_type": source_type,
+ "created_via": created_via,
+ "source_access_method": access_method,
+ "direct_query_enabled": direct_query_enabled,
+ "schema_count": len(active_schemas),
+ "source_id": str(new_source_model.pk),
+ },
+ team=self.team,
+ request=request,
+ )
+
return Response(status=status.HTTP_201_CREATED, data={"id": new_source_model.pk})
def _setup_cdc_resources(
diff --git a/products/data_warehouse/backend/api/test/test_external_data_source.py b/products/data_warehouse/backend/api/test/test_external_data_source.py
index 9dd7d0ae1fb1..4349cf1e5396 100644
--- a/products/data_warehouse/backend/api/test/test_external_data_source.py
+++ b/products/data_warehouse/backend/api/test/test_external_data_source.py
@@ -426,6 +426,60 @@ class TestExternalDataSource(APIBaseTest):
source = ExternalDataSource.objects.get(id=response.json()["id"])
assert source.direct_query_enabled is expected
+ @patch("posthog.event_usage.posthoganalytics.capture")
+ @patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.stripe.source.StripeSource.validate_credentials",
+ return_value=(True, None),
+ )
+ def test_create_external_data_source_reports_analytics_event(self, _mock_validate, mock_capture):
+ # report_user_action runs for real (not mocked) and the request carries the MCP marker, so the
+ # test fails if the request stops being forwarded or `source` stops landing — the mcp/ui/api
+ # attribution this PR exists to deliver.
+ response = self.client.post(
+ f"/api/environments/{self.team.pk}/external_data_sources/",
+ data={
+ "source_type": "Stripe",
+ "created_via": "mcp",
+ "payload": {
+ "auth_method": {"selection": "api_key", "stripe_secret_key": "sk_test_123"},
+ "schemas": [
+ {"name": STRIPE_CUSTOMER_RESOURCE_NAME, "should_sync": True, "sync_type": "full_refresh"},
+ ],
+ },
+ },
+ HTTP_X_POSTHOG_CLIENT="mcp",
+ )
+
+ assert response.status_code == 201, response.json()
+ events = [c for c in mock_capture.call_args_list if c.kwargs.get("event") == "data warehouse source created"]
+ assert len(events) == 1
+ properties = events[0].kwargs["properties"]
+ assert properties["source"] == "mcp" # request-derived transport
+ assert properties["created_via"] == "mcp" # caller's explicit intent
+ assert properties["source_type"] == "Stripe"
+ assert properties["source_id"] == str(response.json()["id"])
+
+ @patch("posthog.event_usage.posthoganalytics.capture")
+ def test_patch_external_data_source_reports_analytics_event(self, mock_capture):
+ # Source originally created via the UI, then edited over MCP: the event must carry both the
+ # edit's transport (source=mcp) and the unchanged original origin (created_via=web).
+ source = self._create_external_data_source(created_via=ExternalDataSource.CreatedVia.WEB)
+
+ response = self.client.patch(
+ f"/api/environments/{self.team.pk}/external_data_sources/{source.pk}/",
+ data={"description": "edited"},
+ HTTP_X_POSTHOG_CLIENT="mcp",
+ )
+
+ assert response.status_code == 200, response.json()
+ events = [c for c in mock_capture.call_args_list if c.kwargs.get("event") == "data warehouse source updated"]
+ assert len(events) == 1
+ properties = events[0].kwargs["properties"]
+ assert properties["source"] == "mcp" # who performed the edit
+ assert properties["created_via"] == ExternalDataSource.CreatedVia.WEB # original origin, preserved
+ assert properties["source_type"] == "Stripe"
+ assert properties["source_id"] == str(source.pk)
+
def test_patch_external_data_source_toggles_direct_query_enabled(self):
source = self._create_external_data_source()
assert source.direct_query_enabled is True