Skip to content

Commit

Permalink
Rewrites for bounce rate
Browse files Browse the repository at this point in the history
  • Loading branch information
bijanbwb committed Dec 11, 2024
1 parent fc4da4e commit 1be0698
Showing 1 changed file with 55 additions and 79 deletions.
134 changes: 55 additions & 79 deletions contents/tutorials/bounce-rate.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ Your bounce rate is the percentage of sessions that resulted in a bounce. A boun
- did not [autocapture](/docs/product-analytics/autocapture) any events
- only had one pageview

> **How does Google Analytics 4 [calculate the bounce rate](https://support.google.com/analytics/answer/12195621?hl=en)?** It is the percentage of sessions that **do not** last longer than 10 seconds, have a conversion event, or have at least 2 pageviews or screenviews.
> **How does Google Analytics 4 [calculate bounce rate](https://support.google.com/analytics/answer/12195621?hl=en)?** It is the percentage of sessions that **do not** last longer than 10 seconds, have a conversion event, or have at least 2 pageviews or screenviews.
## Viewing bounce rate with Web analytics

[Web analytics](/web-analytics) makes it easy to see the bounce rate for a given page on your website. Navigate to your web analytics [dashboard](https://app.posthog.com/web), and you'll find the bounce rate listed alongside each page in the paths section.
[Web analytics](/web-analytics) makes it easy to see the bounce rate for a given page on your website. Navigate to your [web analytics dashboard](https://app.posthog.com/web), and you'll find the bounce rate listed alongside each page in the paths section.

<ProductScreenshot
imageLight="https://res.cloudinary.com/dmukukwp6/image/upload/web_analytics_paths_light_fb2b05a261.png"
Expand All @@ -45,118 +45,94 @@ Your bounce rate is the percentage of sessions that resulted in a bounce. A boun

You can also use [SQL insights](/docs/product-analytics/sql) to calculate bounce rate using the `sessions` table. To create a new SQL insight, go to the **Product analytics** tab, click [new insight](https://app.posthog.com/insights/new), then go to the SQL tab. This is where we write our SQL statements.

To use the default bounce criteria from above, we can use `$is_bounce` to see the bounce rate across all sessions. The value of `$is_bounce` is `1` for sessions that resulted in a bounce, otherwise it's `0`. The following query takes the average of all sessions that resulted in a bounce, multiplies by 100 to get a percentage, and then rounds to a single decimal point.
We can use `$is_bounce` to find sessions that meet the default bounce criteria mentioned above. The value of `$is_bounce` is either `1` or `0`, so we can take the average for all sessions to get the bounce rate. Then, we multiply by `100` to view it as a percentage and round to a single decimal place.

```sql
select
round(avg(sessions.$is_bounce) * 100, 1) as bounce_rate
from sessions
SELECT
round(avg($is_bounce) * 100, 1) AS bounce_rate
FROM
sessions
```

```sql
select(
count(
if($is_bounce = 1, session_id, null)
) / count(session_id) * 100 as bounce_rate
) from sessions
```
## Calculating bounce rate for a specific page

For more granular control, we can redefine which sessions constitute a bounce with our own criteria. For example, we could find all sessions that had a single pageview and did not capture any events, but disregard the session duration.
To find the bounce rate for sessions that began on a specific page, add a `where` clause to the query with the `$entry_pathname`. For example, this query will find the bounce rate for sessions that started on the home page:

```sql
select
round(
avg(
case
when $autocapture_count = 0
and $pageview_count = 1 then 1.0
else 0.0
end
), 1
) * 100 as bounce_rate
from sessions
SELECT
round(avg($is_bounce) * 100, 1) AS bounce_rate
FROM
sessions
WHERE
$entry_pathname = '/'
```

## Calculating bounce rate with raw session replay data

Another way to calculate the bounce rate is to use the `raw_session_replay_events` table.
The query above will only work for sessions that started on the home page. If we want to find the bounce rate for a page without

We can count a bounce as a session where the user is active for less than 10 seconds (without factoring in autocaptured events). To do this in SQL, we use a count of sessions (using `session_id`) where `active_milliseconds` is less than `10000` and divide by the total session count, then multiply by `100`. Together, it looks like this:
If we want to find the bounce rate for the `/pricing` page (not just for sessions that started on that page), we'll need to join on the `events` table and filter for pageviews that match the URL.

```sql
select(
count(
multiIf(active_milliseconds < 10000, session_id, NULL)
) / count(session_id)
) * 100 as bounce_rate
from raw_session_replay_events
SELECT
round(avg($is_bounce) * 100, 1) AS bounce_rate
FROM
sessions
INNER JOIN events ON sessions.id = events.properties.$session_id
WHERE
event = '$pageview'
AND properties.$current_url LIKE '%/pricing%'
```

This gives us a bounce rate percentage insight we can save, update, and add to dashboards.
## Using different bounce criteria

![Bounce rate](https://res.cloudinary.com/dmukukwp6/video/upload/v1710055416/posthog.com/contents/images/tutorials/bounce-rate/bounce-rate.mp4)
For more granular control, we can redefine which sessions constitute a bounce with our own criteria. For example, we could find all sessions that had a single pageview and did not capture any events, but disregard the session duration.

## Using different bounce criteria
```sql
SELECT
round(
avg(
CASE
WHEN $autocapture_count = 0
AND $pageview_count = 1 THEN 1.0
ELSE 0.0
END
) * 100, 1
) AS bounce_rate
FROM
sessions
WHERE
$entry_pathname = '/pricing'
```

Although we used active time as our criteria for bounce rate, PostHog has other options. They include using `click_count`, `keypress_count`, or `mouse_activity_count`. We can find these in the [database data management](https://app.posthog.com/data-management/database) tab under the `raw_session_replay_events` table.
## Calculating bounce rate with raw session replay data

Another way to calculate the bounce rate is to use the `raw_session_replay_events` table. This allows us to use different criteria that isn't available in the `sessions` data, like `click_count`, `keypress_count`, or `mouse_activity_count`. We can find these in the [database data management](https://app.posthog.com/data-management/database) tab under the `raw_session_replay_events` table.

Using different bounce criteria is as simple as changing `active_milliseconds < 10000` to the new criteria. For example, if we wanted to count bounce rate as the percentage of sessions with fewer than 3 clicks, we can use `click_count < 3` like this
We can use a `multiIf` statement to check conditions and set new criteria for calculating bounce rate. For example, if we wanted to count bounce rate as the percentage of sessions with fewer than 3 clicks, we can use `click_count < 3` like this:

```sql
select (
SELECT (
count(
multiIf(click_count < 3, session_id, NULL)
) / count(session_id)
) * 100 as bounce_rate
from raw_session_replay_events
) * 100 AS bounce_rate
FROM raw_session_replay_events
```

We can add more criteria to our `multiIf` statement as well. For example, if we wanted to count bounce rate as the percentage of sessions with fewer than 3 clicks and 2 keypresses or less than 10 seconds, we can use `click_count < 3 and keypress_count < 2 or active_milliseconds < 10000` like this
We can add more criteria to our `multiIf` statement as well. For example, if we wanted to count bounce rate as the percentage of sessions with fewer than 3 clicks and 2 keypresses or less than 10 seconds, we can use `click_count < 3 AND keypress_count < 2 OR active_milliseconds < 10000` like this:

```sql
select (
SELECT (
count(
multiIf(click_count < 3 and keypress_count < 2 or active_milliseconds < 10000, session_id, NULL)
multiIf(click_count < 3 AND keypress_count < 2 OR active_milliseconds < 10000, session_id, NULL)
) / count(session_id)
) * 100 as bounce_rate
from raw_session_replay_events
) * 100 AS bounce_rate
FROM raw_session_replay_events
```

## Calculating bounce rate for a specific page

We use a more complicated SQL query to get the bounce rate for a specific page.

1. We get a count of distinct `session_id` values where the `click_count` is 0 and the `active_milliseconds` is 60000.
2. We divide this the total number of distinct `session_id` values for the page.
3. Use an `INNER JOIN` to add the `events` table to get the `created_at` date and `$properties.current_url` value.
4. Filter for `created_at` dates in the last week with the `$current_url` of a specific URL (in this case, `https://posthog.com/`).

Altogether this looks like this:

```sql
SELECT
(COUNT(DISTINCT CASE
WHEN (raw_session_replay_events.click_count = 0 AND raw_session_replay_events.active_milliseconds < 60000)
THEN raw_session_replay_events.session_id
ELSE NULL
END) * 100.0) / COUNT(DISTINCT events.properties.$session_id) AS bounce_rate
FROM
events
INNER JOIN
raw_session_replay_events ON events.properties.$session_id = raw_session_replay_events.session_id
WHERE
events.created_at >= now() - INTERVAL 7 DAY
AND events.properties.$current_url = 'https://posthog.com/'
```

This gives a bounce rate percentage for our homepage, and you can edit it for any specific page you want.

## Further reading

- [Calculating average session duration, time on site, and other session-based metrics](/tutorials/session-metrics)
- [Using HogQL for advanced time and date filters](/tutorials/hogql-date-time-filters)
- [Using HogQL for advanced breakdowns](/tutorials/hogql-breakdowns)

<NewsletterForm />

$entry_pathname

0 comments on commit 1be0698

Please sign in to comment.