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(heatmaps): show heatmap data for single-day ranges with test account filter (#58638)

Javier Bahamondesf2c4a084d165
diff --git a/posthog/heatmaps/heatmaps_api.py b/posthog/heatmaps/heatmaps_api.py
index 4c0b80845362..b753edb51037 100644
--- a/posthog/heatmaps/heatmaps_api.py
+++ b/posthog/heatmaps/heatmaps_api.py
@@ -274,6 +274,11 @@ class HeatmapViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):
return aggregation_count
def _build_test_accounts_filter(self, date_from: date, date_to: date | None) -> ast.CompareOperation:
+ # The heatmap predicate treats date_to as an inclusive day via `timestamp <= {date_to} + interval 1 day`.
+ # HogQLFilters instead emits a strict `timestamp < date_to`, so when date_from and date_to land on the same
+ # day this events subquery collapses to an impossible range and returns no sessions. Add a day so the
+ # events subquery covers the same date window as the main heatmap query.
+ events_date_to = (date_to or date.today()) + timedelta(days=1)
events_select = replace_filters(
parse_select(
"SELECT distinct $session_id FROM events where notEmpty($session_id) AND {filters}", placeholders={}
@@ -282,7 +287,7 @@ class HeatmapViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):
filterTestAccounts=True,
dateRange=DateRange(
date_from=date_from.strftime("%Y-%m-%d"),
- date_to=date_to.strftime("%Y-%m-%d") if date_to else (date.today()).strftime("%Y-%m-%d"),
+ date_to=events_date_to.strftime("%Y-%m-%d"),
),
),
self.team,
diff --git a/posthog/heatmaps/test/test_heatmaps_api.py b/posthog/heatmaps/test/test_heatmaps_api.py
index 10cda92b01df..409e42745be4 100644
--- a/posthog/heatmaps/test/test_heatmaps_api.py
+++ b/posthog/heatmaps/test/test_heatmaps_api.py
@@ -533,6 +533,46 @@ class TestSessionRecordings(APIBaseTest, ClickhouseTestMixin, QueryMatchingTest)
},
]
+ @parameterized.expand(
+ [
+ ("explicit_same_day", {"date_from": "2023-03-08", "date_to": "2023-03-08"}),
+ ("relative_today", {"date_from": "dStart"}),
+ ("relative_today_start_end", {"date_from": "dStart", "date_to": "dEnd"}),
+ ]
+ )
+ @freezegun.freeze_time("2023-03-08T13:00:00")
+ def test_filter_test_accounts_returns_data_for_single_day_range(self, _name: str, params: dict[str, str]) -> None:
+ # A single-day window (date_from and date_to on the same day) must still return data when filtering
+ # test accounts. The events subquery backing the test-account filter used to collapse to an impossible
+ # range for same-day windows, hiding the whole heatmap.
+ self.team.test_account_filters = [
+ {
+ "key": "$host",
+ "value": "127.0.0.1",
+ "operator": "not_icontains",
+ "type": "event",
+ }
+ ]
+ self.team.save()
+
+ self._create_heatmap_event("session_1", "click", "2023-03-08T09:00:00", viewport_width=100, x=5, y=10)
+ self.create_event(
+ session_id="session_1",
+ timestamp="2023-03-08T09:00:00",
+ distinct_id="12345",
+ properties={"$host": "posthog.com"},
+ )
+
+ response = self._get_heatmap({**params, "filter_test_accounts": True})
+ assert response.data["results"] == [
+ {
+ "count": 1,
+ "pointer_relative_x": 0.0,
+ "pointer_target_fixed": True,
+ "pointer_y": 16,
+ },
+ ]
+
@freezegun.freeze_time("2025-03-31")
@snapshot_clickhouse_queries
def test_can_get_count_by_aggregation(self) -> None: