|
| 1 | +DESCRIPTION > |
| 2 | + Summary with general KPIs per date, including visits, page views, bounce rate and average session duration. |
| 3 | + Accepts `date_from` and `date_to` date filter, all historical data if not passed. |
| 4 | + Daily granularity, except when filtering one single day (hourly) |
| 5 | + |
| 6 | + |
| 7 | +TOKEN "dashboard" READ |
| 8 | + |
| 9 | +NODE timeseries |
| 10 | +DESCRIPTION > |
| 11 | + Generate a timeseries for the specified time range, so we call fill empty data points. |
| 12 | + Filters "future" data points. |
| 13 | + |
| 14 | +SQL > |
| 15 | + |
| 16 | + % |
| 17 | + {% set _single_day = defined(date_from) and day_diff(date_from, date_to) == 0 %} |
| 18 | + with |
| 19 | + {% if defined(date_from) %} |
| 20 | + toStartOfDay(toDate({{Date(date_from, description="Starting day for filtering a date range", required=False)}})) as start, |
| 21 | + {% else %} |
| 22 | + toStartOfDay(timestampAdd(today(), interval -7 day)) as start, |
| 23 | + {% end %} |
| 24 | + {% if defined(date_to) %} |
| 25 | + toStartOfDay(toDate({{Date(date_to, description="Finishing day for filtering a date range", required=False)}})) as end |
| 26 | + {% else %} |
| 27 | + toStartOfDay(today()) as end |
| 28 | + {% end %} |
| 29 | + {% if _single_day %} |
| 30 | + select arrayJoin(arrayMap(x -> toDateTime(x), range(toUInt32(toDateTime(start)), toUInt32(timestampAdd(end, interval 1 day)), 3600))) as date |
| 31 | + {% else %} |
| 32 | + select arrayJoin(arrayMap(x -> toDate(x), range(toUInt32(start), toUInt32(timestampAdd(end, interval 1 day)), 24 * 3600))) as date |
| 33 | + {% end %} |
| 34 | + where date <= now() |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +NODE hits |
| 39 | +DESCRIPTION > |
| 40 | + Group by sessions and calculate metrics at that level |
| 41 | + |
| 42 | +SQL > |
| 43 | + |
| 44 | + % |
| 45 | + {% if defined(date_from) and day_diff(date_from, date_to) == 0 %} |
| 46 | + select |
| 47 | + toStartOfHour(timestamp) as date, |
| 48 | + session_id, |
| 49 | + uniq(session_id) as visits, |
| 50 | + count() as pageviews, |
| 51 | + case when min(timestamp) = max(timestamp) then 1 else 0 end as is_bounce, |
| 52 | + max(timestamp) as latest_hit_aux, |
| 53 | + min(timestamp) as first_hit_aux |
| 54 | + from |
| 55 | + analytics_hits |
| 56 | + where |
| 57 | + toDate(timestamp) = {{Date(date_from)}} |
| 58 | + group by |
| 59 | + toStartOfHour(timestamp), session_id |
| 60 | + {% else %} |
| 61 | + select |
| 62 | + date, |
| 63 | + session_id, |
| 64 | + uniq(session_id) as visits, |
| 65 | + countMerge(hits) as pageviews, |
| 66 | + case when min(first_hit) = max(latest_hit) then 1 else 0 end as is_bounce, |
| 67 | + max(latest_hit) as latest_hit_aux, |
| 68 | + min(first_hit) as first_hit_aux |
| 69 | + from |
| 70 | + analytics_sessions_mv |
| 71 | + where |
| 72 | + {% if defined(date_from) %} |
| 73 | + date >= {{Date(date_from)}} |
| 74 | + {% else %} |
| 75 | + date >= timestampAdd(today(), interval -7 day) |
| 76 | + {% end %} |
| 77 | + {% if defined(date_to) %} |
| 78 | + and date <= {{Date(date_to)}} |
| 79 | + {% else %} |
| 80 | + and date <= today() |
| 81 | + {% end %} |
| 82 | + group by |
| 83 | + date, session_id |
| 84 | + {% end %} |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +NODE data |
| 89 | +DESCRIPTION > |
| 90 | + General KPIs per date, works for both summary metrics and trends charts. |
| 91 | + |
| 92 | +SQL > |
| 93 | + |
| 94 | + select |
| 95 | + date, |
| 96 | + uniq(session_id) as visits, |
| 97 | + sum(pageviews) as pageviews, |
| 98 | + sum(case when latest_hit_aux = first_hit_aux then 1 end) / visits as bounce_rate, |
| 99 | + avg(latest_hit_aux - first_hit_aux) as avg_session_sec |
| 100 | + from |
| 101 | + hits |
| 102 | + group by |
| 103 | + date |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +NODE endpoint |
| 108 | +DESCRIPTION > |
| 109 | + Join and generate timeseries with metrics |
| 110 | + |
| 111 | +SQL > |
| 112 | + |
| 113 | + select |
| 114 | + a.date, |
| 115 | + b.visits, |
| 116 | + b.pageviews, |
| 117 | + b.bounce_rate, |
| 118 | + b.avg_session_sec |
| 119 | + from |
| 120 | + timeseries a |
| 121 | + left join data b using date |
| 122 | + |
| 123 | + |
0 commit comments