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): support custom date range on "completed event for the first time" (#59964)

posthog[bot]933c50408013
diff --git a/ee/clickhouse/queries/test/test_cohort_query.py b/ee/clickhouse/queries/test/test_cohort_query.py
index 74f7ff6556fd..07d89bb4d35f 100644
--- a/ee/clickhouse/queries/test/test_cohort_query.py
+++ b/ee/clickhouse/queries/test/test_cohort_query.py
@@ -952,6 +952,139 @@ class TestCohortQuery(ClickhouseTestMixin, BaseTest):
assert [p2.uuid] == [r[0] for r in res]
+ def test_performed_event_first_time_with_explicit_datetime(self):
+ _create_person(
+ team_id=self.team.pk,
+ distinct_ids=["p1"],
+ properties={"name": "test", "email": "[email protected]"},
+ )
+ p2 = _create_person(
+ team_id=self.team.pk,
+ distinct_ids=["p2"],
+ properties={"name": "test2", "email": "[email protected]"},
+ )
+ # p1's first event is too old to fall in the window
+ _create_event(
+ team=self.team,
+ event="$pageview",
+ properties={},
+ distinct_id="p1",
+ timestamp=datetime.now() - timedelta(days=20),
+ )
+ _create_event(
+ team=self.team,
+ event="$pageview",
+ properties={},
+ distinct_id="p1",
+ timestamp=datetime.now() - timedelta(days=4),
+ )
+ _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",
+ "explicit_datetime": "-1w", # first event must fall within the last week
+ "value": "performed_event_first_time",
+ "type": "behavioral",
+ }
+ ],
+ }
+ }
+ )
+
+ res = execute(filter, self.team)
+
+ assert [p2.uuid] == [r[0] for r in res]
+
+ def test_performed_event_first_time_with_explicit_date_range(self):
+ """First occurrence (not just any occurrence) must fall inside the explicit window."""
+ with freeze_time(datetime.now().replace(hour=12, minute=0, second=0, microsecond=0)):
+ # p1's first event is inside the window [-14d, -7d inclusive]
+ 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={},
+ distinct_id="p1",
+ timestamp=datetime.now() - timedelta(days=10),
+ )
+
+ # p2 has an event inside the window, but its FIRST event predates the lower bound,
+ # so it must be excluded by the first-time semantics.
+ _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=20),
+ )
+ _create_event(
+ team=self.team,
+ event="$pageview",
+ properties={},
+ distinct_id="p2",
+ timestamp=datetime.now() - timedelta(days=10),
+ )
+
+ # p3's first event is too recent (past the upper bound)
+ _create_person(
+ team_id=self.team.pk,
+ distinct_ids=["p3"],
+ properties={"name": "test", "email": "[email protected]"},
+ )
+ _create_event(
+ team=self.team,
+ event="$pageview",
+ properties={},
+ distinct_id="p3",
+ timestamp=datetime.now() - timedelta(days=2),
+ )
+
+ flush_persons_and_events()
+
+ filter = Filter(
+ data={
+ "properties": {
+ "type": "AND",
+ "values": [
+ {
+ "key": "$pageview",
+ "event_type": "events",
+ "explicit_datetime": "-14d",
+ "explicit_datetime_to": "-7d",
+ "value": "performed_event_first_time",
+ "type": "behavioral",
+ }
+ ],
+ }
+ }
+ )
+
+ res = execute(filter, self.team)
+
+ assert [p1.uuid] == [r[0] for r in res]
+
def test_performed_event_regularly(self):
p1 = _create_person(
team_id=self.team.pk,
diff --git a/frontend/src/scenes/cohorts/CohortFilters/constants.tsx b/frontend/src/scenes/cohorts/CohortFilters/constants.tsx
index a78e503a9ea0..4d572fe2da2a 100644
--- a/frontend/src/scenes/cohorts/CohortFilters/constants.tsx
+++ b/frontend/src/scenes/cohorts/CohortFilters/constants.tsx
@@ -730,18 +730,14 @@ export const ROWS: Record<BehavioralFilterType, Row> = {
hide: true,
},
{
- type: FilterType.Text,
- defaultValue: 'in the last',
- },
- {
- fieldKey: 'time_value',
- type: FilterType.Number,
- defaultValue: '30',
+ fieldKey: 'explicit_datetime',
+ type: FilterType.RelativeAndExactTime,
+ defaultValue: '-30d',
},
{
- fieldKey: 'time_interval',
- type: FilterType.TimeUnit,
- defaultValue: TimeUnitType.Day,
+ fieldKey: 'explicit_datetime_to',
+ type: FilterType.RelativeAndExactTime,
+ hide: true,
},
],
},
diff --git a/posthog/models/property/property.py b/posthog/models/property/property.py
index a4c1972a8536..e120cbe9211f 100644
--- a/posthog/models/property/property.py
+++ b/posthog/models/property/property.py
@@ -131,6 +131,10 @@ VALIDATE_CONDITIONAL_BEHAVIORAL_PROP_TYPES = {
{"time_value", "time_interval"},
{"explicit_datetime"},
],
+ BehavioralPropertyType.PERFORMED_EVENT_FIRST_TIME: [
+ {"time_value", "time_interval"},
+ {"explicit_datetime"},
+ ],
}
VALIDATE_BEHAVIORAL_PROP_TYPES = {
@@ -149,8 +153,6 @@ VALIDATE_BEHAVIORAL_PROP_TYPES = {
"key",
"value",
"event_type",
- "time_value",
- "time_interval",
],
BehavioralPropertyType.PERFORMED_EVENT_SEQUENCE: [
"key",
diff --git a/posthog/models/property/test/test_property_behavioral_validation.py b/posthog/models/property/test/test_property_behavioral_validation.py
index c9dd36ddf369..73e70419289b 100644
--- a/posthog/models/property/test/test_property_behavioral_validation.py
+++ b/posthog/models/property/test/test_property_behavioral_validation.py
@@ -44,3 +44,35 @@ class TestBehavioralPropertyDateRangeValidation(BaseTest):
value=BehavioralPropertyType.PERFORMED_EVENT,
explicit_datetime_to="-7d",
)
+
+ def test_performed_event_first_time_accepts_explicit_datetime(self) -> None:
+ self._build(
+ value=BehavioralPropertyType.PERFORMED_EVENT_FIRST_TIME,
+ explicit_datetime="-30d",
+ )
+
+ def test_performed_event_first_time_accepts_explicit_datetime_range(self) -> None:
+ self._build(
+ value=BehavioralPropertyType.PERFORMED_EVENT_FIRST_TIME,
+ explicit_datetime="-30d",
+ explicit_datetime_to="-7d",
+ )
+
+ def test_performed_event_first_time_still_accepts_time_value_interval(self) -> None:
+ # Legacy shape emitted by the old UI must keep validating so saved cohorts don't break
+ self._build(
+ value=BehavioralPropertyType.PERFORMED_EVENT_FIRST_TIME,
+ time_value=30,
+ time_interval="day",
+ )
+
+ def test_performed_event_first_time_rejects_no_time_fields(self) -> None:
+ with self.assertRaises(ValueError):
+ self._build(value=BehavioralPropertyType.PERFORMED_EVENT_FIRST_TIME)
+
+ def test_performed_event_first_time_rejects_only_upper_bound(self) -> None:
+ with self.assertRaises(ValueError):
+ self._build(
+ value=BehavioralPropertyType.PERFORMED_EVENT_FIRST_TIME,
+ explicit_datetime_to="-7d",
+ )