Skip to content

Commit

Permalink
Fixed Tinybird KPI results (#21914)
Browse files Browse the repository at this point in the history
ref https://linear.app/ghost/issue/ANAL-120/bounce-rate-data-seems-to-mix-units
closes https://linear.app/ghost/issue/ANAL-119/visit-duration-metric-inaccurate
closes https://linear.app/ghost/issue/ANAL-118/charts-are-empty-with-only-1-data-point

- The original [web analytics starter kit KPI endpoint](https://github.com/tinybirdco/web-analytics-starter-kit/blob/ad1efb766e2bfbcc85d6c037cf23f02d36a2d0a1/tinybird/pipes/kpis.pipe#L122) had this simpler endpoint, but as I've messed around adding features, I've unintentionally overcomplicated it and introduced a tonne of bugs.
- This reverts the KPI endpoint back towards the original structure, and moves all the calculations and where statements up to the data node
- This means that the left join at the end works and pulls in all the dates from the timeseries node correctly, without the need for using `WITH FILL STEP 1` which generated a result for every second when looking at a single days data. 
- Moving the where clause handling up to the `data` node, rather than being on the endpoint still works as expected, which confused me when I first started working with tinybird
- This should resolve several bugs we've experienced with the visit duration, with missing data points and empty charts, and perhaps even the bounce rate (but need to look at that more closely)
  • Loading branch information
ErisDS authored Dec 18, 2024
1 parent ad44d7a commit 79e5991
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7,230 deletions.
36 changes: 11 additions & 25 deletions ghost/tinybird/pipes/kpis.pipe
Original file line number Diff line number Diff line change
Expand Up @@ -185,36 +185,14 @@ DESCRIPTION >
General KPIs per date, works for both summary metrics and trends charts.

SQL >
%
select
site_uuid,
date,
member_status,
device,
browser,
location,
source,
pathname,
uniq(session_id) as visits,
sum(pageviews) as pageviews,
sum(case when latest_view_aux = first_view_aux then 1 else 0 end) / visits as bounce_rate,
avg(latest_view_aux - first_view_aux) as avg_session_sec
from pageviews
group by date, site_uuid, member_status, device, browser, location, source, pathname

NODE endpoint
DESCRIPTION >
Join and generate timeseries with metrics

SQL >
%
select
a.date as date,
sum(b.visits) as visits,
sum(b.pageviews) as pageviews,
avg(b.bounce_rate) as bounce_rate,
sum(b.avg_session_sec) as avg_session_sec
from timeseries a
left join data b using date
where
site_uuid = {{String(site_uuid, 'mock_site_uuid', description="Tenant ID", required=True)}}

Expand All @@ -224,5 +202,13 @@ SQL >
{% if defined(source) %} and source = {{ String(source, description="Source to filter on", required=False) }} {% end %}
{% if defined(location) %} and location = {{ String(location, description="Location to filter on", required=False) }} {% end %}
{% if defined(pathname) %} and pathname = {{ String(pathname, description="Pathname to filter on", required=False) }} {% end %}
group by date
order by date WITH FILL STEP 1
group by date

NODE endpoint
DESCRIPTION >
Join and generate timeseries with metrics

SQL >
select a.date, b.visits, b.pageviews, b.bounce_rate, b.avg_session_sec
from timeseries a
left join data b using date
8 changes: 4 additions & 4 deletions ghost/tinybird/tests/all_kpis.test.result
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"date","visits","pageviews","bounce_rate","avg_session_sec"
"2100-01-01",3,5,0.3333333333333333,1741
"2100-01-01",3,5,0.3333333333333333,580.3333333333334
"2100-01-02",1,3,0,1027
"2100-01-03",3,7,0,9999
"2100-01-04",3,7,0.3333333333333333,1716
"2100-01-05",2,5,0,616
"2100-01-03",3,7,0,3333
"2100-01-04",3,7,0.3333333333333333,572
"2100-01-05",2,5,0,308
"2100-01-06",2,2,1,0
"2100-01-07",2,2,1,0
Loading

0 comments on commit 79e5991

Please sign in to comment.