From f79cb71d32872c7353e02df600bcc514b5dfc203 Mon Sep 17 00:00:00 2001 From: Surbhi Date: Wed, 4 Dec 2024 12:24:44 -0400 Subject: [PATCH] feat: delete capture events in reverse proxy (#26625) --- posthog/api/proxy_record.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/posthog/api/proxy_record.py b/posthog/api/proxy_record.py index 8c8f9d488f07b..552835aa9562f 100644 --- a/posthog/api/proxy_record.py +++ b/posthog/api/proxy_record.py @@ -25,6 +25,20 @@ def generate_target_cname(organization_id, domain) -> str: return f"{digest}.{settings.PROXY_BASE_CNAME}" +def _capture_proxy_event(request, record: ProxyRecord, event_type: str) -> None: + organization = Organization.objects.get(id=record.organization_id) + posthoganalytics.capture( + request.user.distinct_id, + f"managed reverse proxy {event_type}", + properties={ + "proxy_record_id": record.id, + "domain": record.domain, + "target_cname": record.target_cname, + }, + groups=groups(organization), + ) + + class ProxyRecordSerializer(serializers.ModelSerializer): class Meta: model = ProxyRecord @@ -79,17 +93,7 @@ def create(self, request, *args, **kwargs): ) serializer = self.get_serializer(record) - organization = Organization.objects.get(id=record.organization_id) - posthoganalytics.capture( - request.user.distinct_id, - "managed reverse proxy created", - properties={ - "proxy_record_id": record.id, - "domain": record.domain, - "target_cname": record.target_cname, - }, - groups=groups(organization), - ) + _capture_proxy_event(request, record, "created") return Response(serializer.data) def destroy(self, request, *args, pk=None, **kwargs): @@ -120,6 +124,8 @@ def destroy(self, request, *args, pk=None, **kwargs): record.status = ProxyRecord.Status.DELETING record.save() + _capture_proxy_event(request, record, "deleted") + return Response( {"success": True}, status=status.HTTP_200_OK,