-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cohorts): Allow adding property filters to events in cohorts (#2…
- Loading branch information
1 parent
7d483a3
commit de511ee
Showing
29 changed files
with
768 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
from datetime import datetime, timedelta | ||
|
||
from freezegun import freeze_time | ||
|
||
from ee.clickhouse.queries.enterprise_cohort_query import check_negation_clause | ||
from posthog.client import sync_execute | ||
from posthog.constants import PropertyOperatorType | ||
|
@@ -216,10 +218,76 @@ def test_performed_event(self): | |
{ | ||
"key": "$pageview", | ||
"event_type": "events", | ||
"explicit_datetime": "-1w", | ||
"value": "performed_event", | ||
"type": "behavioral", | ||
} | ||
], | ||
} | ||
} | ||
) | ||
|
||
q, params = CohortQuery(filter=filter, team=self.team).get_query() | ||
res = sync_execute(q, {**params, **filter.hogql_context.values}) | ||
|
||
self.assertEqual([p1.uuid], [r[0] for r in res]) | ||
|
||
@snapshot_clickhouse_queries | ||
@freeze_time("2024-04-05 13:01:01") | ||
def test_performed_event_with_event_filters_and_explicit_date(self): | ||
p1 = _create_person( | ||
team_id=self.team.pk, | ||
distinct_ids=["p1"], | ||
properties={"name": "test", "email": "[email protected]"}, | ||
) | ||
_create_event( | ||
team=self.team, | ||
event="$pageview", | ||
properties={"$filter_prop": "something"}, | ||
distinct_id="p1", | ||
timestamp=datetime.now() - timedelta(days=2), | ||
) | ||
|
||
_create_person( | ||
team_id=self.team.pk, | ||
distinct_ids=["p2"], | ||
properties={"name": "test", "email": "[email protected]"}, | ||
) | ||
_create_event( | ||
team=self.team, | ||
event="$pageview", | ||
properties={}, | ||
distinct_id="p2", | ||
timestamp=datetime.now() - timedelta(days=2), | ||
) | ||
_create_event( | ||
team=self.team, | ||
event="$pageview", | ||
properties={"$filter_prop": "something"}, | ||
distinct_id="p2", | ||
# rejected because explicit datetime is set to 3 days ago | ||
timestamp=datetime.now() - timedelta(days=5), | ||
) | ||
flush_persons_and_events() | ||
|
||
filter = Filter( | ||
data={ | ||
"properties": { | ||
"type": "AND", | ||
"values": [ | ||
{ | ||
"key": "$pageview", | ||
"event_type": "events", | ||
"explicit_datetime": str( | ||
datetime.now() - timedelta(days=3) | ||
), # overrides time_value and time_interval | ||
"time_value": 1, | ||
"time_interval": "week", | ||
"value": "performed_event", | ||
"type": "behavioral", | ||
"event_filters": [ | ||
{"key": "$filter_prop", "value": "something", "operator": "exact", "type": "event"} | ||
], | ||
} | ||
], | ||
} | ||
|
@@ -292,6 +360,78 @@ def test_performed_event_multiple(self): | |
|
||
self.assertEqual([p1.uuid], [r[0] for r in res]) | ||
|
||
def test_performed_event_multiple_with_event_filters(self): | ||
p1 = _create_person( | ||
team_id=self.team.pk, | ||
distinct_ids=["p1"], | ||
properties={"name": "test", "email": "[email protected]"}, | ||
) | ||
_create_event( | ||
team=self.team, | ||
event="$pageview", | ||
properties={"$filter_prop": "something"}, | ||
distinct_id="p1", | ||
timestamp=datetime.now() - timedelta(days=2), | ||
) | ||
|
||
_create_event( | ||
team=self.team, | ||
event="$pageview", | ||
properties={"$filter_prop": "something"}, | ||
distinct_id="p1", | ||
timestamp=datetime.now() - timedelta(days=4), | ||
) | ||
|
||
_create_person( | ||
team_id=self.team.pk, | ||
distinct_ids=["p2"], | ||
properties={"name": "test", "email": "[email protected]"}, | ||
) | ||
_create_event( | ||
team=self.team, | ||
event="$pageview", | ||
properties={}, | ||
distinct_id="p2", | ||
timestamp=datetime.now() - timedelta(days=2), | ||
) | ||
_create_event( | ||
team=self.team, | ||
event="$pageview", | ||
properties={}, | ||
distinct_id="p2", | ||
timestamp=datetime.now() - timedelta(days=4), | ||
) | ||
flush_persons_and_events() | ||
|
||
filter = Filter( | ||
data={ | ||
"properties": { | ||
"type": "AND", | ||
"values": [ | ||
{ | ||
"key": "$pageview", | ||
"event_type": "events", | ||
"operator": "gte", | ||
"operator_value": 1, | ||
"time_value": 1, | ||
"time_interval": "week", | ||
"value": "performed_event_multiple", | ||
"type": "behavioral", | ||
"event_filters": [ | ||
{"key": "$filter_prop", "value": "something", "operator": "exact", "type": "event"}, | ||
{"key": "$filter_prop", "value": "some", "operator": "icontains", "type": "event"}, | ||
], | ||
} | ||
], | ||
} | ||
} | ||
) | ||
|
||
q, params = CohortQuery(filter=filter, team=self.team).get_query() | ||
res = sync_execute(q, {**params, **filter.hogql_context.values}) | ||
|
||
self.assertEqual([p1.uuid], [r[0] for r in res]) | ||
|
||
def test_performed_event_lte_1_times(self): | ||
_create_person( | ||
team_id=self.team.pk, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.