By default, PostHog provides an easy way to group events by week, day, and even hour. Sometimes, smaller, more specific breakdowns are required. With HogQL, you can break down events by time of day, hourly, and even minute-by-minute to help you do a detailed analysis of when they happen, and this tutorial shows you how to do that.
Time of day breakdown
We start with an easy scenario: breaking down what part of the day events take place in. To do this, we create an insight and then break it down by checking if the event timestamp is in the morning, afternoon, evening, or night. We use a HogQL multiIf()
expression using the hours of the day to check against toHour(timestamp)
like so:
multiIf(5 >= toHour(timestamp) and toHour(timestamp) < 12, 'morning',12 >= toHour(timestamp) and toHour(timestamp) < 17, 'afternoon',17 >= toHour(timestamp) and toHour(timestamp) < 20, 'evening','night')
You can visualize this with a total value bar chart to the sums for the date range or time series bar to see how they change day to day.
Note: Event timestamps default to UTC. You need to adjust your times depending on your local time. For example, if you are in Pacific Daylight Time, subtract 7 hours from UTC timestamps to match your local time.
Hourly breakdown
PostHog enables you to group data by hour for single-day date ranges. If you want to sum these values across ranges longer than one day, you can break it down by toHour(timestamp)
and, once again, use a total value or time series bar chart to visualize.
Minute-by-minute breakdown
If hourly isn’t enough for you, we can move down to by the minute. We use an SQL insight for this. To make one, select the "SQL" tab when creating an insight. In this insight, we select a formatted count of toStartOfMinute(timestamp)
where the timestamp is in the past day and order by the time (minute
). In HogQL, this looks like this:
selectformatDateTime(toStartOfMinute(timestamp), '%T') AS minute,count(*)from eventswhere timestamp > now() - interval 1 daygroup by minuteorder by minute desclimit 100
If instead, you care about which minutes have the highest count, you can order by count(*)
instead of minute
.
select toStartOfMinute(timestamp)as minute, count(*)from eventswhere timestamp > now() - interval 1 daygroup by minuteorder by count(*) desclimit 100
HogQL also includes functions to get events for every 5, 10, and 15 minutes. To do this, replace toStartOfMinute
with toStartOfFiveMinutes
, toStartOfTenMinutes
, and toStartOfFifteenMinutes
.
Real time filtering
Even minute-by-minute analysis might not be good enough for some people. They want a real time understanding of their analytics. To do this in PostHog, we can filter for events within a time frame. Because it takes time to ingest events into PostHog, we recommend using a 5-30 minutes filter as "real time."
To set up one of these filters, add a filter to your series or dashboard, select HogQL, then use a dateDiff()
comparing the timestamp to the current time like this:
dateDiff('minute', timestamp, now()) < 5
You can use this filter on specific insights or on an entire dashboard. As example, below we use the filter to get the number of unique sessions in the last 5 minutes:
To help you get set up faster with this, you can use our real time dashboard template which has basic insights and real time filters set up for you.