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.

fix(cohorts): allow static cohorts with behavioral filters in batch triggers (#57776)

Meikel Ratzb67a50638191
diff --git a/posthog/api/cohort.py b/posthog/api/cohort.py
index 962297db080a..fe8b80c1fd43 100644
--- a/posthog/api/cohort.py
+++ b/posthog/api/cohort.py
@@ -1251,6 +1251,10 @@ class CohortViewSet(TeamAndOrgViewSetMixin, ForbidDestroyModel, viewsets.ModelVi
check_property_values(value["values"], source_id)
for cohort_id, cohort in all_cohorts.items():
+ # Static cohorts have pre-computed membership and don't re-evaluate
+ # their filters, so they're always safe to use regardless of filter type.
+ if cohort.is_static:
+ continue
if cohort.filters:
properties = cohort.filters.get("properties", {})
if isinstance(properties, dict):
diff --git a/posthog/api/test/test_cohort.py b/posthog/api/test/test_cohort.py
index c0d0ccace21a..29b2812677f5 100644
--- a/posthog/api/test/test_cohort.py
+++ b/posthog/api/test/test_cohort.py
@@ -1708,6 +1708,81 @@ [email protected],
self.assertEqual(len(results), 1)
self.assertEqual(results[0]["id"], regular_cohort.id)
+ @patch("posthog.api.cohort.report_user_action")
+ def test_static_cohort_with_behavioral_filters_not_excluded(self, patch_capture):
+ static_cohort = Cohort.objects.create(
+ team=self.team,
+ name="static behavioral cohort",
+ is_static=True,
+ filters={
+ "properties": {
+ "type": "OR",
+ "values": [
+ {
+ "type": "OR",
+ "values": [
+ {
+ "type": "behavioral",
+ "key": "$pageview",
+ "value": "performed_event",
+ "event_type": "events",
+ "time_value": 30,
+ "time_interval": "day",
+ }
+ ],
+ }
+ ],
+ }
+ },
+ )
+
+ response = self.client.get(f"/api/projects/{self.team.id}/cohorts?hide_behavioral_cohorts=true")
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ result_ids = {r["id"] for r in response.json()["results"]}
+ self.assertIn(static_cohort.id, result_ids)
+
+ @patch("posthog.api.cohort.report_user_action")
+ def test_dynamic_cohort_referencing_static_behavioral_cohort_not_excluded(self, patch_capture):
+ static_behavioral = Cohort.objects.create(
+ team=self.team,
+ name="static behavioral cohort",
+ is_static=True,
+ filters={
+ "properties": {
+ "type": "OR",
+ "values": [
+ {
+ "type": "behavioral",
+ "key": "$pageview",
+ "value": "performed_event",
+ "event_type": "events",
+ "time_value": 30,
+ "time_interval": "day",
+ }
+ ],
+ }
+ },
+ )
+
+ parent_cohort = Cohort.objects.create(
+ team=self.team,
+ name="dynamic cohort referencing static behavioral",
+ filters={
+ "properties": {
+ "type": "OR",
+ "values": [
+ {"type": "cohort", "key": "id", "value": str(static_behavioral.pk)},
+ ],
+ }
+ },
+ )
+
+ response = self.client.get(f"/api/projects/{self.team.id}/cohorts?hide_behavioral_cohorts=true")
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ result_ids = {r["id"] for r in response.json()["results"]}
+ self.assertIn(static_behavioral.id, result_ids)
+ self.assertIn(parent_cohort.id, result_ids)
+
@parameterized.expand(
[
("realtime_backfilled_flag_on", CohortType.REALTIME, True, True, True),