: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(data-warehouse): report warehouse property mapping usage events (#82274)
Tom Owers5b1bba0be3ab
diff --git a/products/customer_analytics/backend/presentation/views/views.py b/products/customer_analytics/backend/presentation/views/views.pyindex 2a54335f1cd3..39d5221dee42 100644--- a/products/customer_analytics/backend/presentation/views/views.py+++ b/products/customer_analytics/backend/presentation/views/views.py@@ -32,6 +32,7 @@ from rest_framework.throttling import UserRateThrottlefrom posthog.api.mixins import ValidatedRequest, validated_requestfrom posthog.api.routing import TeamAndOrgViewSetMixinfrom posthog.api.tagged_item import TaggedItemViewSetMixin+from posthog.event_usage import report_user_actionfrom posthog.exceptions import Conflictfrom posthog.helpers.impersonation import is_impersonatedfrom posthog.models import OrganizationMembership@@ -575,13 +576,21 @@ class CustomPropertySourceViewSet(return Response({"detail": "Not found."}, status=status.HTTP_404_NOT_FOUND)return Response(CustomPropertySourceSerializer(instance=source).data)- def _definition_is_group(self, definition_id) -> bool:+ def _definition_target_type(self, definition_id) -> str | None:if definition_id is None:- return False+ return Nonedefinition = api.get_custom_property_definition(self.team_id, str(definition_id), user_access_control=self.user_access_control)- return definition is not None and definition.target_type == _GROUP_TARGET_TYPE+ return definition.target_type if definition is not None else None++ def _definition_is_group(self, definition_id) -> bool:+ return self._definition_target_type(definition_id) == _GROUP_TARGET_TYPE++ def _report_usage(self, request: Request, event: str, **properties: Any) -> None:+ # The scene's $pageview says who looked at Warehouse properties; these say who actually+ # mapped a table. Emitted here rather than in the frontend so API callers count too.+ report_user_action(cast(User, request.user), event, properties, team=self.team)def _guard_group_source(self, request: Request, source_id, *, write: bool = True) -> None:# A source feeding a group definition reads/activates the group-writing pipeline, so touching@@ -594,7 +603,8 @@ class CustomPropertySourceViewSet(serializer = CustomPropertySourceSerializer(data=request.data)serializer.is_valid(raise_exception=True)data = serializer.validated_data- if self._definition_is_group(data.definition):+ target_type = self._definition_target_type(data.definition)+ if target_type == _GROUP_TARGET_TYPE:_assert_group_scope(request, write=True)try:source = api.create_custom_property_source(@@ -614,6 +624,14 @@ class CustomPropertySourceViewSet(raise ValidationError(str(e))except api.ResourceForbiddenError:raise PermissionDenied()+ self._report_usage(+ request,+ "warehouse property mapping created",+ target_type=target_type,+ mapped_column_count=len(data.column_property_map or {}),+ reads_warehouse_table=data.external_data_schema is not None,+ is_enabled=data.is_enabled,+ )return Response(CustomPropertySourceSerializer(instance=source).data, status=status.HTTP_201_CREATED)@extend_schema(request=CustomPropertySourceUpdateSerializer)@@ -632,6 +650,12 @@ class CustomPropertySourceViewSet(raise PermissionDenied()if source is None:return Response({"detail": "Not found."}, status=status.HTTP_404_NOT_FOUND)+ self._report_usage(+ request,+ "warehouse property mapping updated",+ updated_fields=sorted(write.validated_data.keys()),+ is_enabled=source.is_enabled,+ )return Response(CustomPropertySourceSerializer(instance=source).data)@extend_schema(request=CustomPropertySourceUpdateSerializer)@@ -651,6 +675,7 @@ class CustomPropertySourceViewSet(raise PermissionDenied()if not deleted:return Response({"detail": "Not found."}, status=status.HTTP_404_NOT_FOUND)+ self._report_usage(request, "warehouse property mapping deleted")return Response(status=status.HTTP_204_NO_CONTENT)@extend_schema(@@ -674,6 +699,7 @@ class CustomPropertySourceViewSet(return Response({"message": str(e)}, status=status.HTTP_400_BAD_REQUEST)if not triggered:raise ValidationError("This action is only available for enabled person- or group-property sources.")+ self._report_usage(request, "warehouse property sync triggered")return Response({"status": "triggered"}, status=status.HTTP_202_ACCEPTED)@extend_schema(@@ -698,6 +724,7 @@ class CustomPropertySourceViewSet(raise PermissionDenied()if started is None:raise ValidationError("This action is only available for enabled person- or group-property sources.")+ self._report_usage(request, "warehouse property backfill triggered", already_running=not started)return Response({"status": "started" if started else "already_running", "already_running": not started},status=status.HTTP_202_ACCEPTED,diff --git a/products/customer_analytics/backend/test/test_views.py b/products/customer_analytics/backend/test/test_views.pyindex cd4c3590e3df..a4dbcd16c543 100644--- a/products/customer_analytics/backend/test/test_views.py+++ b/products/customer_analytics/backend/test/test_views.py@@ -1966,6 +1966,38 @@ class TestCustomPropertySourceViewSet(APIBaseTest):assert body["column_property_map"] == {"plan": "plan_tier"}assert body["saved_query"] is None+ @patch("products.customer_analytics.backend.presentation.views.views.report_user_action")+ def test_mapping_lifecycle_emits_usage_events(self, report_user_action):+ definition, schema = self._person_definition_and_schema()++ created = self.client.post(+ self.endpoint,+ {+ "definition": str(definition.id),+ "external_data_schema": str(schema.id),+ "column_property_map": {"plan": "plan_tier", "seats": "seat_count"},+ "key_column": "distinct_id",+ },+ format="json",+ )+ assert created.status_code == status.HTTP_201_CREATED, created.content+ source_id = created.json()["id"]++ patched = self.client.patch(f"{self.endpoint}{source_id}/", {"is_enabled": False}, format="json")+ assert patched.status_code == status.HTTP_200_OK+ deleted = self.client.delete(f"{self.endpoint}{source_id}/")+ assert deleted.status_code == status.HTTP_204_NO_CONTENT++ assert [call.args[1] for call in report_user_action.call_args_list] == [+ "warehouse property mapping created",+ "warehouse property mapping updated",+ "warehouse property mapping deleted",+ ]+ create_properties = report_user_action.call_args_list[0].args[2]+ assert create_properties["target_type"] == TargetType.PERSON.value+ assert create_properties["mapped_column_count"] == 2+ assert create_properties["reads_warehouse_table"] is True+def test_create_person_source_with_account_binding_is_rejected(self):definition, _schema = self._person_definition_and_schema()@@ -2775,7 +2807,6 @@ class TestCalendarSyncViewSet(APIBaseTest):def _immediate_future():-async def _done():return None