Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: Change bounce rate calculation formula to 'single-page sessions / total sessions * 100'

Blue Mouseb7ffff4ca849
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts
index cb2ff28a0..4c94ec063 100644
--- a/backend/apps/cloud/src/analytics/analytics.service.ts
+++ b/backend/apps/cloud/src/analytics/analytics.service.ts
@@ -2588,6 +2588,14 @@ export class AnalyticsService {
return result
}
+ private calculateBounceRate(bounces: number, sessions: number): number {
+ if (!sessions) {
+ return 0
+ }
+
+ return _round((bounces * 100) / sessions, 1)
+ }
+
async getAnalyticsSummary(
pids: string[],
timeBucket?: string,
@@ -2660,11 +2668,34 @@ export class AnalyticsService {
)
GROUP BY psid
)
+ ),
+ bounce_counts AS (
+ SELECT count() AS bounces
+ FROM (
+ SELECT psid
+ FROM events
+ WHERE pid = {pid:FixedString(12)}
+ AND type = 'pageview'
+ AND psid IS NOT NULL
+ AND psid != 0
+ AND psid IN (
+ SELECT DISTINCT psid
+ FROM events
+ WHERE pid = {pid:FixedString(12)}
+ AND type = 'pageview'
+ AND psid IS NOT NULL
+ AND psid != 0
+ ${filtersQuery}
+ )
+ GROUP BY psid
+ HAVING count() = 1
+ )
)
SELECT
analytics_counts.*,
- duration_avg.sdur
- FROM analytics_counts, duration_avg
+ duration_avg.sdur,
+ bounce_counts.bounces
+ FROM analytics_counts, duration_avg, bounce_counts
`
const { data } = await clickhouse
@@ -2683,8 +2714,11 @@ export class AnalyticsService {
let bounceRate = 0
- if (data[0].all > 0 && !customEVFilterApplied) {
- bounceRate = _round((data[0].unique * 100) / data[0].all, 1)
+ if (!customEVFilterApplied) {
+ bounceRate = this.calculateBounceRate(
+ data[0].bounces,
+ data[0].unique,
+ )
}
result[pid] = {
@@ -2796,11 +2830,36 @@ export class AnalyticsService {
)
GROUP BY psid
)
+ ),
+ bounce_counts AS (
+ SELECT count() AS bounces
+ FROM (
+ SELECT psid
+ FROM events
+ WHERE pid = {pid:FixedString(12)}
+ AND type = 'pageview'
+ AND psid IS NOT NULL
+ AND psid != 0
+ AND created BETWEEN {groupFromUTC:String} AND {groupToUTC:String}
+ AND psid IN (
+ SELECT DISTINCT psid
+ FROM events
+ WHERE pid = {pid:FixedString(12)}
+ AND type = 'pageview'
+ AND psid IS NOT NULL
+ AND psid != 0
+ AND created BETWEEN {groupFromUTC:String} AND {groupToUTC:String}
+ ${currentFiltersQuery}
+ )
+ GROUP BY psid
+ HAVING count() = 1
+ )
)
SELECT
analytics_counts.*,
- duration_avg.sdur
- FROM analytics_counts, duration_avg
+ duration_avg.sdur,
+ bounce_counts.bounces
+ FROM analytics_counts, duration_avg, bounce_counts
`
const queryPrevious = `
@@ -2836,11 +2895,36 @@ export class AnalyticsService {
)
GROUP BY psid
)
+ ),
+ bounce_counts AS (
+ SELECT count() AS bounces
+ FROM (
+ SELECT psid
+ FROM events
+ WHERE pid = {pid:FixedString(12)}
+ AND type = 'pageview'
+ AND psid IS NOT NULL
+ AND psid != 0
+ AND created BETWEEN {periodSubtracted:String} AND {groupFromUTC:String}
+ AND psid IN (
+ SELECT DISTINCT psid
+ FROM events
+ WHERE pid = {pid:FixedString(12)}
+ AND type = 'pageview'
+ AND psid IS NOT NULL
+ AND psid != 0
+ AND created BETWEEN {periodSubtracted:String} AND {groupFromUTC:String}
+ ${previousFiltersQuery}
+ )
+ GROUP BY psid
+ HAVING count() = 1
+ )
)
SELECT
analytics_counts.*,
- duration_avg.sdur
- FROM analytics_counts, duration_avg
+ duration_avg.sdur,
+ bounce_counts.bounces
+ FROM analytics_counts, duration_avg, bounce_counts
`
const query = `${queryCurrent} UNION ALL ${queryPrevious}`
@@ -2866,17 +2950,15 @@ export class AnalyticsService {
let bounceRate = 0
let prevBounceRate = 0
- if (currentPeriod.all > 0 && !customEVFilterApplied) {
- bounceRate = _round(
- (currentPeriod.unique * 100) / currentPeriod.all,
- 1,
+ if (!customEVFilterApplied) {
+ bounceRate = this.calculateBounceRate(
+ currentPeriod.bounces,
+ currentPeriod.unique,
)
- }
- if (previousPeriod.all > 0 && !customEVFilterApplied) {
- prevBounceRate = _round(
- (previousPeriod.unique * 100) / previousPeriod.all,
- 1,
+ prevBounceRate = this.calculateBounceRate(
+ previousPeriod.bounces,
+ previousPeriod.unique,
)
}
@@ -3569,6 +3651,7 @@ export class AnalyticsService {
const visits = Array(x.length).fill(0)
const uniques = Array(x.length).fill(0)
const sdur = Array(x.length).fill(0)
+ const bounces = Array(x.length).fill(0)
for (let row = 0; row < _size(result); ++row) {
const dateString = this.generateDateString(result[row])
@@ -3578,6 +3661,7 @@ export class AnalyticsService {
visits[index] = result[row].pageviews
uniques[index] = result[row].uniques
sdur[index] = _round(result[row].sdur)
+ bounces[index] = result[row].bounces || 0
}
}
@@ -3585,6 +3669,7 @@ export class AnalyticsService {
visits,
uniques,
sdur,
+ bounces,
}
}
@@ -3693,7 +3778,8 @@ export class AnalyticsService {
${selector},
avgOrNull(sessions_data.duration) as sdur,
count() as pageviews,
- count(DISTINCT psid) as uniques
+ count(DISTINCT subquery.psid) as uniques,
+ countIf(session_counts.pageviews = 1) as bounces
FROM (
SELECT
pid,
@@ -3715,6 +3801,29 @@ export class AnalyticsService {
) as sessions_data
ON subquery.pid = sessions_data.pid
AND subquery.psid = sessions_data.psid
+ LEFT JOIN (
+ SELECT
+ pid,
+ psid,
+ count() as pageviews
+ FROM events
+ PREWHERE pid = {pid:FixedString(12)} AND type = 'pageview'
+ WHERE created BETWEEN ${tzFromDate} AND ${tzToDate}
+ AND psid IS NOT NULL
+ AND psid != 0
+ AND psid IN (
+ SELECT DISTINCT psid
+ FROM events
+ PREWHERE pid = {pid:FixedString(12)} AND type = 'pageview'
+ WHERE created BETWEEN ${tzFromDate} AND ${tzToDate}
+ AND psid IS NOT NULL
+ AND psid != 0
+ ${filtersQuery}
+ )
+ GROUP BY pid, psid
+ ) as session_counts
+ ON subquery.pid = session_counts.pid
+ AND subquery.psid = session_counts.psid
GROUP BY ${groupBy}
ORDER BY ${groupBy}
`
@@ -3724,7 +3833,8 @@ export class AnalyticsService {
SELECT
*,
sum(pageviews) OVER (ORDER BY ${groupBy} ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as pageviews,
- sum(uniques) OVER (ORDER BY ${groupBy} ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as uniques
+ sum(uniques) OVER (ORDER BY ${groupBy} ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as uniques,
+ sum(bounces) OVER (ORDER BY ${groupBy} ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as bounces
FROM (${baseQuery})
`
}
diff --git a/backend/apps/cloud/src/analytics/interfaces/index.ts b/backend/apps/cloud/src/analytics/interfaces/index.ts
index b16a31f95..a1bf62359 100644
--- a/backend/apps/cloud/src/analytics/interfaces/index.ts
+++ b/backend/apps/cloud/src/analytics/interfaces/index.ts
@@ -14,6 +14,7 @@ export interface TrafficCHResponse {
sdur: number
uniques: number
pageviews: number
+ bounces: number
}
export interface BirdseyeCHResponse {
@@ -21,6 +22,7 @@ export interface BirdseyeCHResponse {
unique: number
users: number
sdur: number
+ bounces: number
}
export interface PerformanceCHResponse {
@@ -105,6 +107,7 @@ export interface IExtractChartData {
visits: number[]
uniques: number[]
sdur: number[]
+ bounces?: number[]
}
export interface IAggregatedMetadata {
@@ -183,6 +186,7 @@ interface IPerformanceObject {
interface IOverallChart {
x: string[]
visits: number[]
+ bounces?: number[]
}
interface IOverallObject {
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index 72584f985..7456623b3 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -2314,6 +2314,14 @@ export class AnalyticsService {
return data[0]?.c || 0
}
+ private calculateBounceRate(bounces: number, sessions: number): number {
+ if (!sessions) {
+ return 0
+ }
+
+ return _round((bounces * 100) / sessions, 1)
+ }
+
async getAnalyticsSummary(
pids: string[],
timeBucket?: string,
@@ -2384,11 +2392,34 @@ export class AnalyticsService {
)
GROUP BY psid
)
+ ),
+ bounce_counts AS (
+ SELECT count() AS bounces
+ FROM (
+ SELECT psid
+ FROM events
+ WHERE pid = {pid:FixedString(12)}
+ AND type = 'pageview'
+ AND psid IS NOT NULL
+ AND psid != 0
+ AND psid IN (
+ SELECT DISTINCT psid
+ FROM events
+ WHERE pid = {pid:FixedString(12)}
+ AND type = 'pageview'
+ AND psid IS NOT NULL
+ AND psid != 0
+ ${filtersQuery}
+ )
+ GROUP BY psid
+ HAVING count() = 1
+ )
)
SELECT
analytics_counts.*,
- duration_avg.sdur
- FROM analytics_counts, duration_avg
+ duration_avg.sdur,
+ bounce_counts.bounces
+ FROM analytics_counts, duration_avg, bounce_counts
`
const { data } = await clickhouse
@@ -2403,8 +2434,11 @@ export class AnalyticsService {
let bounceRate = 0
- if (data[0].all > 0 && !customEVFilterApplied) {
- bounceRate = _round((data[0].unique * 100) / data[0].all, 1)
+ if (!customEVFilterApplied) {
+ bounceRate = this.calculateBounceRate(
+ data[0].bounces,
+ data[0].unique,
+ )
}
result[pid] = {
@@ -2510,11 +2544,36 @@ export class AnalyticsService {
)
GROUP BY psid
)
+ ),
+ bounce_counts AS (
+ SELECT count() AS bounces
+ FROM (
+ SELECT psid
+ FROM events
+ WHERE pid = {pid:FixedString(12)}
+ AND type = 'pageview'
+ AND psid IS NOT NULL
+ AND psid != 0
+ AND created BETWEEN {groupFromUTC:String} AND {groupToUTC:String}
+ AND psid IN (
+ SELECT DISTINCT psid
+ FROM events
+ WHERE pid = {pid:FixedString(12)}
+ AND type = 'pageview'
+ AND psid IS NOT NULL
+ AND psid != 0
+ AND created BETWEEN {groupFromUTC:String} AND {groupToUTC:String}
+ ${filtersQuery}
+ )
+ GROUP BY psid
+ HAVING count() = 1
+ )
)
SELECT
analytics_counts.*,
- duration_avg.sdur
- FROM analytics_counts, duration_avg
+ duration_avg.sdur,
+ bounce_counts.bounces
+ FROM analytics_counts, duration_avg, bounce_counts
`
const queryPrevious = `
@@ -2550,11 +2609,36 @@ export class AnalyticsService {
)
GROUP BY psid
)
+ ),
+ bounce_counts AS (
+ SELECT count() AS bounces
+ FROM (
+ SELECT psid
+ FROM events
+ WHERE pid = {pid:FixedString(12)}
+ AND type = 'pageview'
+ AND psid IS NOT NULL
+ AND psid != 0
+ AND created BETWEEN {periodSubtracted:String} AND {groupFromUTC:String}
+ AND psid IN (
+ SELECT DISTINCT psid
+ FROM events
+ WHERE pid = {pid:FixedString(12)}
+ AND type = 'pageview'
+ AND psid IS NOT NULL
+ AND psid != 0
+ AND created BETWEEN {periodSubtracted:String} AND {groupFromUTC:String}
+ ${filtersQuery}
+ )
+ GROUP BY psid
+ HAVING count() = 1
+ )
)
SELECT
analytics_counts.*,
- duration_avg.sdur
- FROM analytics_counts, duration_avg
+ duration_avg.sdur,
+ bounce_counts.bounces
+ FROM analytics_counts, duration_avg, bounce_counts
`
const query = `${queryCurrent} UNION ALL ${queryPrevious}`
@@ -2580,17 +2664,15 @@ export class AnalyticsService {
let bounceRate = 0
let prevBounceRate = 0
- if (currentPeriod.all > 0 && !customEVFilterApplied) {
- bounceRate = _round(
- (currentPeriod.unique * 100) / currentPeriod.all,
- 1,
+ if (!customEVFilterApplied) {
+ bounceRate = this.calculateBounceRate(
+ currentPeriod.bounces,
+ currentPeriod.unique,
)
- }
- if (previousPeriod.all > 0 && !customEVFilterApplied) {
- prevBounceRate = _round(
- (previousPeriod.unique * 100) / previousPeriod.all,
- 1,
+ prevBounceRate = this.calculateBounceRate(
+ previousPeriod.bounces,
+ previousPeriod.unique,
)
}
@@ -3227,6 +3309,7 @@ export class AnalyticsService {
const visits = Array(x.length).fill(0)
const uniques = Array(x.length).fill(0)
const sdur = Array(x.length).fill(0)
+ const bounces = Array(x.length).fill(0)
for (let row = 0; row < _size(result); ++row) {
const dateString = this.generateDateString(result[row])
@@ -3236,6 +3319,7 @@ export class AnalyticsService {
visits[index] = result[row].pageviews
uniques[index] = result[row].uniques
sdur[index] = _round(result[row].sdur)
+ bounces[index] = result[row].bounces || 0
}
}
@@ -3243,6 +3327,7 @@ export class AnalyticsService {
visits,
uniques,
sdur,
+ bounces,
}
}
@@ -3383,7 +3468,8 @@ export class AnalyticsService {
${selector},
avgOrNull(sessions_data.duration) as sdur,
count() as pageviews,
- count(DISTINCT psid) as uniques
+ count(DISTINCT subquery.psid) as uniques,
+ countIf(session_counts.pageviews = 1) as bounces
FROM (
SELECT
pid,
@@ -3405,6 +3491,29 @@ export class AnalyticsService {
) as sessions_data
ON subquery.pid = sessions_data.pid
AND subquery.psid = sessions_data.psid
+ LEFT JOIN (
+ SELECT
+ pid,
+ psid,
+ count() as pageviews
+ FROM events
+ PREWHERE pid = {pid:FixedString(12)} AND type = 'pageview'
+ WHERE created BETWEEN ${tzFromDate} AND ${tzToDate}
+ AND psid IS NOT NULL
+ AND psid != 0
+ AND psid IN (
+ SELECT DISTINCT psid
+ FROM events
+ PREWHERE pid = {pid:FixedString(12)} AND type = 'pageview'
+ WHERE created BETWEEN ${tzFromDate} AND ${tzToDate}
+ AND psid IS NOT NULL
+ AND psid != 0
+ ${filtersQuery}
+ )
+ GROUP BY pid, psid
+ ) as session_counts
+ ON subquery.pid = session_counts.pid
+ AND subquery.psid = session_counts.psid
GROUP BY ${groupBy}
ORDER BY ${groupBy}
`
@@ -3414,7 +3523,8 @@ export class AnalyticsService {
SELECT
*,
sum(pageviews) OVER (ORDER BY ${groupBy} ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as pageviews,
- sum(uniques) OVER (ORDER BY ${groupBy} ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as uniques
+ sum(uniques) OVER (ORDER BY ${groupBy} ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as uniques,
+ sum(bounces) OVER (ORDER BY ${groupBy} ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as bounces
FROM (${baseQuery})
`
}
diff --git a/backend/apps/community/src/analytics/interfaces/index.ts b/backend/apps/community/src/analytics/interfaces/index.ts
index a57eb8f2a..fdff59e5d 100644
--- a/backend/apps/community/src/analytics/interfaces/index.ts
+++ b/backend/apps/community/src/analytics/interfaces/index.ts
@@ -14,6 +14,7 @@ export interface TrafficCHResponse {
sdur: number
uniques: number
pageviews: number
+ bounces: number
}
export interface PerformanceCHResponse {
@@ -98,6 +99,7 @@ export interface IExtractChartData {
visits: number[]
uniques: number[]
sdur: number[]
+ bounces?: number[]
}
export interface IAggregatedMetadata {
@@ -154,6 +156,7 @@ export interface BirdseyeCHResponse {
unique: number
users: number
sdur: number
+ bounces: number
}
interface IOverallPeriodStats {
@@ -183,6 +186,7 @@ interface IPerformanceObject {
interface IOverallChart {
x: string[]
visits: number[]
+ bounces?: number[]
}
interface IOverallObject {
diff --git a/web/app/api/api.server.ts b/web/app/api/api.server.ts
index 8b76a555d..62c2cb1f7 100644
--- a/web/app/api/api.server.ts
+++ b/web/app/api/api.server.ts
@@ -711,6 +711,7 @@ export interface TrafficLogResponse {
visits: number[]
uniques: number[]
sdur: number[]
+ bounces?: number[]
}
customs: Record<string, number>
properties: Record<string, number>
@@ -740,7 +741,7 @@ export interface OverallObject {
bounceRateChange?: number
sdurChange?: number
customEVFilterApplied?: boolean
- chart?: { x: string[]; visits: number[] }
+ chart?: { x: string[]; visits: number[]; bounces?: number[] }
}
export interface AnalyticsParams {
diff --git a/web/app/lib/models/Project.ts b/web/app/lib/models/Project.ts
index a616581aa..64195dfa5 100644
--- a/web/app/lib/models/Project.ts
+++ b/web/app/lib/models/Project.ts
@@ -11,6 +11,7 @@ interface OverallPeriodStats {
export interface OverallChart {
x: string[]
visits: number[]
+ bounces?: number[]
}
export interface OverallObject {
diff --git a/web/app/pages/Project/View/ViewProject.helpers.tsx b/web/app/pages/Project/View/ViewProject.helpers.tsx
index d06888b10..81c553176 100644
--- a/web/app/pages/Project/View/ViewProject.helpers.tsx
+++ b/web/app/pages/Project/View/ViewProject.helpers.tsx
@@ -330,8 +330,17 @@ const getColumns = (
}
if (bounce) {
- const bounceArray = _map(chart.uniques, (el, i) => {
- return _round((_toNumber(el) * 100) / _toNumber(chart.visits[i]), 1) || 0
+ const bounceArray = _map(chart.uniques, (sessions, i) => {
+ if (!sessions) {
+ return 0
+ }
+
+ return (
+ _round(
+ (_toNumber(chart.bounces?.[i]) * 100) / _toNumber(sessions),
+ 1,
+ ) || 0
+ )
})
columns.push(['bounce', ...bounceArray])
}
diff --git a/web/app/pages/Project/View/interfaces/traffic.ts b/web/app/pages/Project/View/interfaces/traffic.ts
index e4da7a052..ce630b6dc 100644
--- a/web/app/pages/Project/View/interfaces/traffic.ts
+++ b/web/app/pages/Project/View/interfaces/traffic.ts
@@ -59,6 +59,7 @@ export interface TrafficLogResponse {
visits: number[]
uniques: number[]
sdur: number[]
+ bounces?: number[]
}
customs: Customs
properties: Properties