Skip to content

Commit

Permalink
feat(web-analytics): Add bugfix for bounce rate workaround (#22827)
Browse files Browse the repository at this point in the history
* Add bugfix for bounce rate workaround

* Parametrize the bounce rate test across different pageview modes

* Add some more comments
  • Loading branch information
robbie-c authored Jun 9, 2024
1 parent faa95c5 commit 7f81bcb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
6 changes: 5 additions & 1 deletion posthog/hogql/database/schema/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def arg_max_merge_field(field_name: str) -> ast.Call:
aggregate_fields["$is_bounce"] = ast.Call(
name="if",
args=[
# if pageview_count is 0, return NULL so it doesn't contribute towards the bounce rate either way
ast.Call(name="equals", args=[bounce_pageview_count, ast.Constant(value=0)]),
ast.Constant(value=None),
ast.Call(
Expand All @@ -213,10 +214,13 @@ def arg_max_merge_field(field_name: str) -> ast.Call:
ast.Call(
name="or",
args=[
ast.Call(name="greater", args=[aggregate_fields["$pageview_count"], ast.Constant(value=1)]),
# if > 1 pageview, not a bounce
ast.Call(name="greater", args=[bounce_pageview_count, ast.Constant(value=1)]),
# if > 0 autocapture events, not a bounce
ast.Call(
name="greater", args=[aggregate_fields["$autocapture_count"], ast.Constant(value=0)]
),
# if session duration >= 10 seconds, not a bounce
ast.Call(
name="greaterOrEquals",
args=[aggregate_fields["$session_duration"], ast.Constant(value=10)],
Expand Down
27 changes: 16 additions & 11 deletions posthog/hogql/database/schema/test/test_sessions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from parameterized import parameterized

from posthog.hogql import ast
from posthog.hogql.database.schema.sessions import get_lazy_session_table_properties
from posthog.hogql.parser import parse_select
from posthog.hogql.query import execute_hogql_query
from posthog.models.property_definition import PropertyType
from posthog.schema import HogQLQueryModifiers, BounceRatePageViewMode
from posthog.test.base import (
APIBaseTest,
ClickhouseTestMixin,
Expand Down Expand Up @@ -139,87 +142,89 @@ def test_persons_and_sessions_on_events(self):
self.assertEqual(row1, (p1.uuid, "source1"))
self.assertEqual(row2, (p2.uuid, "source2"))

def test_bounce_rate(self):
@parameterized.expand([(BounceRatePageViewMode.uniq_urls,), (BounceRatePageViewMode.count_pageviews,)])
def test_bounce_rate(self, bounceRatePageViewMode):
# person with 2 different sessions
_create_event(
event="$pageview",
team=self.team,
distinct_id="d1",
properties={"$session_id": "s1a"},
properties={"$session_id": "s1a", "$current_url": "https://example.com/1"},
timestamp="2023-12-02",
)
_create_event(
event="$pageview",
team=self.team,
distinct_id="d1",
properties={"$session_id": "s1a"},
properties={"$session_id": "s1a", "$current_url": "https://example.com/2"},
timestamp="2023-12-03",
)
_create_event(
event="$pageview",
team=self.team,
distinct_id="d1",
properties={"$session_id": "s1b"},
properties={"$session_id": "s1b", "$current_url": "https://example.com/3"},
timestamp="2023-12-12",
)
# session with 1 pageview
_create_event(
event="$pageview",
team=self.team,
distinct_id="d2",
properties={"$session_id": "s2"},
properties={"$session_id": "s2", "$current_url": "https://example.com/4"},
timestamp="2023-12-11",
)
# session with 1 pageview and 1 autocapture
_create_event(
event="$pageview",
team=self.team,
distinct_id="d3",
properties={"$session_id": "s3"},
properties={"$session_id": "s3", "$current_url": "https://example.com/5"},
timestamp="2023-12-11",
)
_create_event(
event="$autocapture",
team=self.team,
distinct_id="d3",
properties={"$session_id": "s3"},
properties={"$session_id": "s3", "$current_url": "https://example.com/5"},
timestamp="2023-12-11",
)
# short session with a pageleave
_create_event(
event="$pageview",
team=self.team,
distinct_id="d4",
properties={"$session_id": "s4"},
properties={"$session_id": "s4", "$current_url": "https://example.com/6"},
timestamp="2023-12-11T12:00:00",
)
_create_event(
event="$pageleave",
team=self.team,
distinct_id="d4",
properties={"$session_id": "s4"},
properties={"$session_id": "s4", "$current_url": "https://example.com/6"},
timestamp="2023-12-11T12:00:01",
)
# long session with a pageleave
_create_event(
event="$pageview",
team=self.team,
distinct_id="d5",
properties={"$session_id": "s5"},
properties={"$session_id": "s5", "$current_url": "https://example.com/7"},
timestamp="2023-12-11T12:00:00",
)
_create_event(
event="$pageleave",
team=self.team,
distinct_id="d5",
properties={"$session_id": "s5"},
properties={"$session_id": "s5", "$current_url": "https://example.com/7"},
timestamp="2023-12-11T12:00:11",
)
response = execute_hogql_query(
parse_select(
"select $is_bounce, session_id from sessions ORDER BY session_id",
),
self.team,
modifiers=HogQLQueryModifiers(bounceRatePageViewMode=bounceRatePageViewMode),
)
self.assertEqual(
[
Expand Down

0 comments on commit 7f81bcb

Please sign in to comment.