From b8c0a8cf5ca5fd6515f9ab383fd4ec04db5455b6 Mon Sep 17 00:00:00 2001 From: timgl Date: Tue, 26 Jul 2022 22:36:14 +0200 Subject: [PATCH] chore: Instrument license activation events (#10811) --- ee/api/license.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ee/api/license.py b/ee/api/license.py index 09e0e9a59a1..9319b8ba7f0 100644 --- a/ee/api/license.py +++ b/ee/api/license.py @@ -1,3 +1,4 @@ +import posthoganalytics import requests from django.conf import settings from django.db.models import QuerySet @@ -7,6 +8,7 @@ from rest_framework import mixins, request, serializers, viewsets from rest_framework.response import Response from ee.models.license import License, LicenseError +from posthog.event_usage import groups from posthog.models.organization import Organization from posthog.models.team import Team @@ -27,8 +29,22 @@ class LicenseSerializer(serializers.ModelSerializer): def validate(self, data): validation = requests.post("https://license.posthog.com/licenses/activate", data={"key": data["key"]}) resp = validation.json() + user = self.context["request"].user if not validation.ok: + posthoganalytics.capture( + user.distinct_id, + "license key activation failure", + properties={"error": validation.content}, + groups=groups(user.current_organization, user.current_team), + ) raise LicenseError(resp["code"], resp["detail"]) + + posthoganalytics.capture( + user.distinct_id, + "license key activation success", + properties={}, + groups=groups(user.current_organization, user.current_team), + ) data["valid_until"] = resp["valid_until"] data["plan"] = resp["plan"] data["max_users"] = resp.get("max_users", 0)