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(insights): count calendar heatmap unique users per event bucket (#57296)

Xander90c9e6cd495a
diff --git a/posthog/hogql_queries/insights/trends/calendar_heatmap_query_runner.py b/posthog/hogql_queries/insights/trends/calendar_heatmap_query_runner.py
index 198966882679..5ef18f172233 100644
--- a/posthog/hogql_queries/insights/trends/calendar_heatmap_query_runner.py
+++ b/posthog/hogql_queries/insights/trends/calendar_heatmap_query_runner.py
@@ -49,34 +49,17 @@ SEPARATOR = "','"
# We need to use a CTE, otherwise we'll get this error because of the sub-query containing some auto-generated conditions:
# Aggregate function any(if(NOT empty(events__override.distinct_id), events__override.person_id, events.person_id)) AS person_id is found in WHERE in query.
templateUniqueUsers = """
-WITH uniqueSessionEvents AS (
+WITH filteredEvents AS (
SELECT
person_id,
- $session_id,
timestamp
FROM events
WHERE and(
{event_expr},
{all_properties},
- {test_account_filters}
+ {test_account_filters},
+ {current_period}
)
-),
-uniqueSessionEventsGrouped AS (
- SELECT
- any(person_id) as person_id,
- $session_id as session_id,
- min(timestamp) as timestamp
- FROM uniqueSessionEvents
- GROUP BY $session_id
-),
-query AS (
- SELECT
- uniqMap(map(concatWithSeparator({separator},toString(toDayOfWeek(uniqueSessionEventsGrouped.timestamp)),toString(toHour(uniqueSessionEventsGrouped.timestamp))), uniqueSessionEventsGrouped.person_id)) as hoursAndDays,
- uniqMap(map(toHour(uniqueSessionEventsGrouped.timestamp), uniqueSessionEventsGrouped.person_id)) as hours,
- uniqMap(map(toDayOfWeek(uniqueSessionEventsGrouped.timestamp), uniqueSessionEventsGrouped.person_id)) as days,
- uniq(person_id) as total
- FROM uniqueSessionEventsGrouped
- WHERE {current_period}
)
SELECT
mapKeys(query.hoursAndDays) as hoursAndDaysKeys,
@@ -86,7 +69,14 @@ SELECT
mapKeys(query.days) as daysKeys,
mapValues(query.days) as daysValues,
query.total
-FROM query
+FROM (
+ SELECT
+ uniqMap(map(concatWithSeparator({separator},toString(toDayOfWeek(timestamp)),toString(toHour(timestamp))), person_id)) as hoursAndDays,
+ uniqMap(map(toHour(timestamp), person_id)) as hours,
+ uniqMap(map(toDayOfWeek(timestamp), person_id)) as days,
+ uniq(person_id) as total
+ FROM filteredEvents
+) as query
"""
templateAllUsers = """
diff --git a/posthog/hogql_queries/insights/trends/test/test_calendar_heatmap_query_runner.py b/posthog/hogql_queries/insights/trends/test/test_calendar_heatmap_query_runner.py
index 78c0a5af0686..7a27ab16ebfb 100644
--- a/posthog/hogql_queries/insights/trends/test/test_calendar_heatmap_query_runner.py
+++ b/posthog/hogql_queries/insights/trends/test/test_calendar_heatmap_query_runner.py
@@ -1321,3 +1321,42 @@ class TestCalendarHeatmapQueryRunner(ClickhouseTestMixin, APIBaseTest):
assert (date_from_explicit.hour, date_from_explicit.minute, date_from_explicit.second) == (14, 32, 11)
assert (date_to_explicit.hour, date_to_explicit.minute, date_to_explicit.second) == (14, 32, 11)
assert (date_to_explicit - date_from_explicit).total_seconds() == 7 * 24 * 60 * 60
+
+ def test_unique_users_buckets_per_event_not_per_session(self):
+ # Regression test: with `dau` math, every (day-of-week, hour) bucket the user touched
+ # must report the user — not just the bucket containing the session's first event.
+ # All events below share one session_id, so the previous templateUniqueUsers
+ # implementation would only count the user in the 23:00 bucket of Saturday.
+ shared_session_id = str(uuid7())
+ timestamps = [
+ "2023-12-02 23:50:00", # Saturday 23:00
+ "2023-12-03 00:10:00", # Sunday 00:00 — same session, next day/hour
+ "2023-12-03 01:30:00", # Sunday 01:00
+ ]
+ with freeze_time(timestamps[0]):
+ _create_person(team_id=self.team.pk, distinct_ids=["userA"], properties={"name": "userA"})
+ for timestamp in timestamps:
+ _create_event(
+ team=self.team,
+ event="$pageview",
+ distinct_id="userA",
+ timestamp=timestamp,
+ uuid=str(uuid7()),
+ properties={"$session_id": shared_session_id, "$start_timestamp": timestamps[0]},
+ )
+
+ query = CalendarHeatmapQuery(
+ dateRange=DateRange(date_from="2023-12-01", date_to="2023-12-04"),
+ properties=[],
+ filterTestAccounts=False,
+ series=[EventsNode(kind="EventsNode", math="dau")],
+ )
+ response = CalendarHeatmapQueryRunner(team=self.team, query=query).calculate()
+ results_dict = {(r.row, r.column): r.value for r in response.results.data}
+
+ assert results_dict.get((6, 23)) == 1, f"Expected 1 unique user at Sat 23:00, got {results_dict.get((6, 23))}"
+ assert results_dict.get((7, 0)) == 1, f"Expected 1 unique user at Sun 00:00, got {results_dict.get((7, 0))}"
+ assert results_dict.get((7, 1)) == 1, f"Expected 1 unique user at Sun 01:00, got {results_dict.get((7, 1))}"
+ assert response.results.allAggregations == 1, (
+ f"Expected 1 unique user total, got {response.results.allAggregations}"
+ )