Skip to content

Commit

Permalink
fix(cohorts): Fix all events action with cohorts
Browse files Browse the repository at this point in the history
  • Loading branch information
timgl committed Jan 20, 2025
1 parent b56d6d1 commit 0ea83f9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
58 changes: 58 additions & 0 deletions ee/clickhouse/models/test/test_cohort.py
Original file line number Diff line number Diff line change
Expand Up @@ -1447,3 +1447,61 @@ def test_calculate_people_ch_in_multiteam_project(self):

self.assertCountEqual([r[0] for r in results_team1], [person2_team1.uuid])
self.assertCountEqual([r[0] for r in results_team2], [person1_team2.uuid])

def test_cohortpeople_action_all_events(self):
# Create an action that matches all events (no specific event defined)
action = Action.objects.create(team=self.team, name="all events", steps_json=[{"event": None}])

# Create two people
Person.objects.create(
team_id=self.team.pk,
distinct_ids=["1"],
properties={"$some_prop": "something", "$another_prop": "something"},
)

Person.objects.create(
team_id=self.team.pk,
distinct_ids=["2"],
properties={"$some_prop": "something", "$another_prop": "something"},
)

# Create different types of events for both people
_create_event(
event="$pageview",
team=self.team,
distinct_id="1",
properties={"attr": "some_val"},
timestamp=datetime.now() - timedelta(hours=12),
)

_create_event(
event="$autocapture",
team=self.team,
distinct_id="2",
properties={"attr": "some_val"},
timestamp=datetime.now() - timedelta(hours=12),
)

# Create a cohort based on the "all events" action
cohort = Cohort.objects.create(
team=self.team, groups=[{"action_id": action.pk, "days": 1}], name="cohort_all_events"
)
cohort.calculate_people_ch(pending_version=0)

# Both people should be in the cohort since they both performed some event
results = self._get_cohortpeople(cohort)
self.assertEqual(len(results), 2)

# Create a person with no events
Person.objects.create(
team_id=self.team.pk,
distinct_ids=["3"],
properties={"$some_prop": "something", "$another_prop": "something"},
)

# Recalculate cohort
cohort.calculate_people_ch(pending_version=1)

# Should still only have 2 people since person 3 has no events
results = self._get_cohortpeople(cohort)
self.assertEqual(len(results), 2)
7 changes: 6 additions & 1 deletion posthog/queries/foss_cohort_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,11 +360,16 @@ def _get_behavior_subquery(self) -> tuple[str, dict[str, Any], str]:
)

date_condition, date_params = self._get_date_condition()
if len(self._events) > 0:
event_condition = f"AND event IN %({event_param_name})s"
else:
event_condition = ""

query = f"""
SELECT {", ".join(_fields)} FROM events {self.EVENT_TABLE_ALIAS}
{self._get_person_ids_query()}
WHERE team_id = %(team_id)s
AND event IN %({event_param_name})s
{event_condition}
{date_condition}
{person_prop_query}
GROUP BY person_id
Expand Down

0 comments on commit 0ea83f9

Please sign in to comment.