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(cohorts): report cohort type on cohort created and updated (#106279)

ruby childs76b21294cebd
diff --git a/posthog/api/test/test_cohort.py b/posthog/api/test/test_cohort.py
index b6db3a2a8498..f7812c1359c5 100644
--- a/posthog/api/test/test_cohort.py
+++ b/posthog/api/test/test_cohort.py
@@ -195,6 +195,12 @@ class TestCohort(TestExportMixin, ClickhouseTestMixin, APIBaseTest, QueryMatchin
},
"name_length": 8,
"deleted": False,
+ "is_static": False,
+ # The legacy `groups` payload leaves `filters` empty, so neither classification
+ # is computed for it.
+ "cohort_type": None,
+ "condition_type": None,
+ "realtime_enabled": False,
},
team=ANY,
request=ANY,
@@ -238,12 +244,62 @@ class TestCohort(TestExportMixin, ClickhouseTestMixin, APIBaseTest, QueryMatchin
},
"name_length": 9,
"deleted": False,
+ "is_static": False,
+ "cohort_type": None,
+ "condition_type": None,
+ "realtime_enabled": False,
"updated_by_creator": True,
},
team=ANY,
request=ANY,
)
+ @patch("django.db.transaction.on_commit", side_effect=lambda func: func())
+ @patch("posthog.api.cohort.report_user_action")
+ @patch("posthog.tasks.calculate_cohort.calculate_cohort_ch.delay")
+ def test_cohort_created_reports_realtime_classification(
+ self, patch_calculate_cohort, patch_capture, patch_on_commit
+ ):
+ response = self.client.post(
+ f"/api/projects/{self.team.id}/cohorts",
+ data={
+ "name": "performed an action",
+ "filters": {
+ "properties": {
+ "type": "OR",
+ "values": [
+ {
+ "type": "AND",
+ "values": [
+ {
+ "key": "$pageview",
+ "type": "behavioral",
+ "value": "performed_event",
+ "event_type": "events",
+ "time_value": 30,
+ "time_interval": "day",
+ }
+ ],
+ }
+ ],
+ }
+ },
+ },
+ )
+ self.assertEqual(response.status_code, 201, response.content)
+
+ cohort = Cohort.objects.get(id=response.json()["id"])
+ reported = patch_capture.call_args[0][2]
+ self.assertEqual(reported["cohort_type"], cohort.cohort_type)
+ self.assertEqual(reported["cohort_type"], CohortType.REALTIME)
+ self.assertFalse(reported["is_static"])
+ self.assertEqual(
+ reported["condition_type"],
+ {"person_properties": False, "behavioral": True, "lifecycle": False, "cohorts": False},
+ )
+ # Realtime-eligible filters on a team the realtime pipeline does not cover.
+ self.assertFalse(reported["realtime_enabled"])
+
@patch("django.db.transaction.on_commit", side_effect=lambda func: func())
@patch("posthog.api.cohort.report_user_action")
@patch(
diff --git a/products/cohorts/backend/models/cohort.py b/products/cohorts/backend/models/cohort.py
index b554c0018a1b..f74340802e46 100644
--- a/products/cohorts/backend/models/cohort.py
+++ b/products/cohorts/backend/models/cohort.py
@@ -698,6 +698,15 @@ class Cohort(FileSystemSyncMixin, RootTeamMixin, models.Model):
"filters": self.properties.to_dict(),
"name_length": len(self.name) if self.name else 0,
"deleted": self.deleted,
+ "is_static": self.is_static,
+ # `cohort_type` is what the cohort was classified as at this moment: a later
+ # calculation clears it when the cohort grows past the realtime member ceiling.
+ "cohort_type": self.cohort_type,
+ "condition_type": self.condition_type,
+ # `cohort_type` reads `realtime` whenever the filters are realtime-eligible, on every
+ # team. This reports whether the team runs the realtime pipeline at all, so a consumer
+ # can tell an eligible cohort from one that realtime membership is maintained for.
+ "realtime_enabled": bool(self.team_id) and is_realtime_cohort_team(self.team_id),
}
def _safe_reset_calculating_state(self, completed_version: int) -> None: