Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(today): highlight the page people rage-click most (#117302)

Shy Alter1f93c6f19b07
diff --git a/products/today/backend/logic/highlight_sources/rage.py b/products/today/backend/logic/highlight_sources/rage.py
new file mode 100644
index 000000000000..1d10fca3092c
--- /dev/null
+++ b/products/today/backend/logic/highlight_sources/rage.py
@@ -0,0 +1,95 @@
+import hashlib
+
+from products.error_tracking.backend.facade.api import get_issue_basics
+from products.web_analytics.backend.facade.rage_pages import (
+ ErrorsBeforeRage,
+ RagePage,
+ find_errors_before_rage,
+ find_top_rage_element,
+ find_top_rage_page,
+)
+
+from ...facade.enums import HighlightLabel
+from ..highlight_rules import human, short, users, uses_product
+from ..highlight_types import Candidate, Context, Found, Source
+
+RAGE_PAGE = "{people} rage-clicked on {page} in the last 7 days"
+RAGE_PAGE_DETAIL = "{clicks} rage clicks · Top element: ‘{element}’"
+RAGE_PAGE_NO_ELEMENT_DETAIL = "{clicks} rage clicks"
+RAGE_PAGE_ERROR_DETAIL = "{pct}% of sessions saw the error ‘{issue}’ in the 30 seconds before"
+
+RAGE_PRODUCTS = ("Session replay", "Web analytics", "Heatmaps")
+MIN_ERROR_SESSIONS = 3
+MIN_ERROR_SHARE = 0.3
+NO_ERRORS = ErrorsBeforeRage(sessions=0, issue_id=None, issue_sessions=0)
+
+
+def find(context: Context) -> Found:
+ if not uses_product(context, *RAGE_PRODUCTS):
+ return Found()
+ page = context.daily("rage_page", lambda: find_top_rage_page(context.team, context.user))
+ if page is None:
+ return Found()
+ path_key = hashlib.sha256(page.path.encode()).hexdigest()[:16]
+ title = RAGE_PAGE.format(people=users(page.people), page=(page.path or "/")[:40])
+ if context.allows("error_tracking:read"):
+ errors = context.guard(
+ "rage_errors",
+ lambda: context.daily(
+ f"rage_errors:{path_key}", lambda: find_errors_before_rage(context.team, context.user, page.path)
+ ),
+ NO_ERRORS,
+ )
+ error = _error_item(context, page, errors, title)
+ if error is not None:
+ return Found(items=(error,))
+ top_element = context.guard(
+ "rage_element",
+ lambda: context.daily(
+ f"rage_element:{path_key}", lambda: find_top_rage_element(context.team, context.user, page.path)
+ ),
+ None,
+ )
+ element = short(top_element or "", 30)
+ clicks = human(page.clicks)
+ return Found(
+ items=(
+ Candidate(
+ key=f"rage:{page.path}",
+ label=HighlightLabel.RISK,
+ family="replay",
+ importance=0.35,
+ relevance=1.0,
+ title=title,
+ detail=RAGE_PAGE_DETAIL.format(clicks=clicks, element=element)
+ if element
+ else RAGE_PAGE_NO_ELEMENT_DETAIL.format(clicks=clicks),
+ url="/replay/home",
+ ),
+ )
+ )
+
+
+def _error_item(context: Context, page: RagePage, errors: ErrorsBeforeRage, title: str) -> Candidate | None:
+ issue_id = errors.issue_id
+ share = errors.issue_sessions / errors.sessions if errors.sessions else 0
+ if issue_id is None or errors.issue_sessions < MIN_ERROR_SESSIONS or share < MIN_ERROR_SHARE:
+ return None
+ issue = context.guard("rage_issue", lambda: get_issue_basics(context.team.id, issue_id), None)
+ if issue is None:
+ return None
+ name = f"{issue.name or 'Issue'}: {issue.description}" if issue.description else issue.name or "Issue"
+ return Candidate(
+ key=f"rage:{page.path}",
+ label=HighlightLabel.RISK,
+ family="replay",
+ importance=0.6,
+ relevance=1.0,
+ title=title,
+ detail=RAGE_PAGE_ERROR_DETAIL.format(pct=f"{share * 100:.0f}", issue=short(name, 50)),
+ url=f"/error_tracking/{issue_id}",
+ ref=issue_id,
+ )
+
+
+SOURCE = Source(name="rage", find=find)
diff --git a/products/today/backend/tests/test_highlight_rage.py b/products/today/backend/tests/test_highlight_rage.py
new file mode 100644
index 000000000000..6e792d4bf7e9
--- /dev/null
+++ b/products/today/backend/tests/test_highlight_rage.py
@@ -0,0 +1,132 @@
+import uuid
+from datetime import date
+from types import SimpleNamespace
+from typing import cast
+
+from unittest.mock import patch
+
+from django.test import SimpleTestCase
+
+from parameterized import parameterized
+
+from products.error_tracking.backend.facade.contracts import ErrorTrackingIssueBasics
+from products.today.backend.logic.highlight_registry import load_sources
+from products.today.backend.logic.highlight_sources import rage
+from products.today.backend.logic.highlight_types import Context
+from products.web_analytics.backend.facade.rage_pages import ErrorsBeforeRage, RagePage
+
+ISSUE_ID = uuid.UUID("01890000-0000-7000-8000-000000000002")
+PAGE = RagePage(path="/checkout/…", people=12, clicks=340)
+ELEMENT_ITEM = (
+ "12 users rage-clicked on /checkout/… in the last 7 days",
+ "340 rage clicks · Top element: ‘Pay now’",
+ "/replay/home",
+)
+ERROR_ITEM = (
+ "12 users rage-clicked on /checkout/… in the last 7 days",
+ "50% of sessions saw the error ‘TypeError: Cannot read properties of undefined’ in the 30 seconds before",
+ f"/error_tracking/{ISSUE_ID}",
+)
+
+
+class TestRage(SimpleTestCase):
+ @parameterized.expand(
+ [
+ (
+ "an error in half the sessions",
+ True,
+ PAGE,
+ "Pay now",
+ ErrorsBeforeRage(6, str(ISSUE_ID), 3),
+ True,
+ [ERROR_ITEM],
+ ),
+ (
+ "an error in too few sessions",
+ True,
+ PAGE,
+ "Pay now",
+ ErrorsBeforeRage(3, str(ISSUE_ID), 2),
+ True,
+ [ELEMENT_ITEM],
+ ),
+ (
+ "an error in too small a share of sessions",
+ True,
+ PAGE,
+ "Pay now",
+ ErrorsBeforeRage(11, str(ISSUE_ID), 3),
+ True,
+ [ELEMENT_ITEM],
+ ),
+ (
+ "a key that cannot read errors",
+ True,
+ PAGE,
+ "Pay now",
+ ErrorsBeforeRage(6, str(ISSUE_ID), 3),
+ False,
+ [ELEMENT_ITEM],
+ ),
+ (
+ "no element text",
+ True,
+ PAGE,
+ None,
+ ErrorsBeforeRage(0, None, 0),
+ True,
+ [(ELEMENT_ITEM[0], "340 rage clicks", "/replay/home")],
+ ),
+ ("no page", True, None, None, ErrorsBeforeRage(0, None, 0), True, []),
+ (
+ "a person who uses none of the rage products",
+ False,
+ PAGE,
+ "Pay now",
+ ErrorsBeforeRage(0, None, 0),
+ True,
+ [],
+ ),
+ ]
+ )
+ def test_the_top_rage_page_links_the_error_people_saw_before_they_clicked(
+ self,
+ _name: str,
+ relevant: bool,
+ page: RagePage | None,
+ element: str | None,
+ errors: ErrorsBeforeRage,
+ reads_errors: bool,
+ expected: list[tuple[str, str, str]],
+ ) -> None:
+ context = cast(
+ Context,
+ SimpleNamespace(
+ team=SimpleNamespace(id=1),
+ user=SimpleNamespace(id=2),
+ today=date(2026, 10, 13),
+ daily=lambda _name, compute: compute(),
+ failures=[],
+ allows=lambda *scopes: reads_errors,
+ guard=lambda _name, provider, _default: provider(),
+ ),
+ )
+ issue = ErrorTrackingIssueBasics(
+ id=ISSUE_ID,
+ name="TypeError",
+ description="Cannot read properties of undefined",
+ status="active",
+ severity=None,
+ )
+ registered = next(s for s in load_sources() if s.name == "rage")
+
+ with (
+ patch.object(rage, "uses_product", return_value=relevant),
+ patch.object(rage, "find_top_rage_page", return_value=page),
+ patch.object(rage, "find_top_rage_element", return_value=element),
+ patch.object(rage, "find_errors_before_rage", return_value=errors),
+ patch.object(rage, "get_issue_basics", return_value=issue),
+ ):
+ found = registered.find(context)
+
+ self.assertEqual([(c.title, c.detail, c.url) for c in found.items], expected)
diff --git a/products/web_analytics/backend/facade/rage_pages.py b/products/web_analytics/backend/facade/rage_pages.py
new file mode 100644
index 000000000000..bf3847dc0bfd
--- /dev/null
+++ b/products/web_analytics/backend/facade/rage_pages.py
@@ -0,0 +1,15 @@
+from products.web_analytics.backend.rage_pages import (
+ ErrorsBeforeRage,
+ RagePage,
+ find_errors_before_rage,
+ find_top_rage_element,
+ find_top_rage_page,
+)
+
+__all__ = [
+ "ErrorsBeforeRage",
+ "RagePage",
+ "find_errors_before_rage",
+ "find_top_rage_element",
+ "find_top_rage_page",
+]
diff --git a/products/web_analytics/backend/rage_pages.py b/products/web_analytics/backend/rage_pages.py
new file mode 100644
index 000000000000..6a09c17d0854
--- /dev/null
+++ b/products/web_analytics/backend/rage_pages.py
@@ -0,0 +1,103 @@
+from collections import Counter
+from dataclasses import dataclass
+from typing import TYPE_CHECKING, Any
+
+from posthog.hogql import ast
+from posthog.hogql.constants import HogQLGlobalSettings
+from posthog.hogql.query import execute_hogql_query
+
+if TYPE_CHECKING:
+ from posthog.models import Team, User
+
+QUERY_TIMEOUT_SECONDS = 10
+MIN_CLICKS_PER_PERSON = 10
+MIN_PEOPLE = 3
+ERROR_WINDOW_SECONDS = 30
+RAGE_PATH = "replaceRegexpAll(coalesce(properties.$pathname, ''), '/[0-9a-f-]{8,}|/[0-9]+', '/…')"
+TOP_RAGE_PAGE = f"""
+SELECT path, count() AS people, sum(clicks) AS clicks FROM (
+ SELECT person_id, {RAGE_PATH} AS path, count() AS clicks
+ FROM events WHERE timestamp >= now() - INTERVAL 7 DAY AND event = '$rageclick'
+ GROUP BY person_id, path HAVING clicks >= {MIN_CLICKS_PER_PERSON}
+)
+GROUP BY path HAVING people >= {MIN_PEOPLE} ORDER BY people DESC, clicks DESC
+LIMIT 1
+"""
+TOP_RAGE_ELEMENT = f"""
+SELECT arrayElement(elements_chain_texts, 1) AS text, count() AS clicks FROM events
+WHERE timestamp >= now() - INTERVAL 7 DAY AND event = '$rageclick'
+ AND {RAGE_PATH} = {{path}}
+ AND arrayElement(elements_chain_texts, 1) != ''
+GROUP BY text ORDER BY clicks DESC
+LIMIT 1
+"""
+QUALIFYING_PEOPLE = f"""
+SELECT person_id FROM events
+WHERE timestamp >= now() - INTERVAL 7 DAY AND event = '$rageclick' AND {RAGE_PATH} = {{path}}
+GROUP BY person_id HAVING count() >= {MIN_CLICKS_PER_PERSON}
+"""
+ERRORS_BEFORE_RAGE = f"""
+WITH qualifying_sessions AS MATERIALIZED (
+ SELECT `$session_id` AS session_id, min(timestamp) AS first_rage FROM events
+ WHERE timestamp >= now() - INTERVAL 7 DAY AND event = '$rageclick' AND `$session_id` != ''
+ AND {RAGE_PATH} = {{path}} AND person_id IN ({QUALIFYING_PEOPLE})
+ GROUP BY session_id
+)
+SELECT r.session_id AS session_id,
+ groupUniqArrayIf(
+ x.issue,
+ isNotNull(x.issue)
+ AND x.timestamp >= r.first_rage - INTERVAL {ERROR_WINDOW_SECONDS} SECOND
+ AND x.timestamp <= r.first_rage
+ ) AS issues
+FROM qualifying_sessions AS r
+LEFT JOIN (
+ SELECT `$session_id` AS session_id, timestamp, toString(issue_id) AS issue FROM events
+ WHERE timestamp >= now() - INTERVAL 7 DAY AND event = '$exception'
+ AND `$session_id` IN (SELECT session_id FROM qualifying_sessions)
+) AS x ON x.session_id = r.session_id
+GROUP BY r.session_id
+LIMIT 10000
+"""
+
+
+@dataclass(frozen=True)
+class RagePage:
+ path: str
+ people: int
+ clicks: int
+
+
+@dataclass(frozen=True)
+class ErrorsBeforeRage:
+ sessions: int
+ issue_id: str | None
+ issue_sessions: int
+
+
+def _rows(query: str, team: "Team", user: "User", name: str, path: str | None = None) -> list[Any]:
+ return execute_hogql_query(
+ query,
+ team,
+ query_type=name,
+ placeholders={"path": ast.Constant(value=path)} if path is not None else {},
+ user=user,
+ settings=HogQLGlobalSettings(max_execution_time=QUERY_TIMEOUT_SECONDS, timeout_overflow_mode="throw"),
+ ).results
+
+
+def find_top_rage_page(team: "Team", user: "User") -> RagePage | None:
+ rows = _rows(TOP_RAGE_PAGE, team, user, "web_analytics_top_rage_page")
+ return RagePage(path=rows[0][0], people=rows[0][1], clicks=rows[0][2]) if rows else None
+
+
+def find_top_rage_element(team: "Team", user: "User", path: str) -> str | None:
+ rows = _rows(TOP_RAGE_ELEMENT, team, user, "web_analytics_top_rage_element", path)
+ return rows[0][0] if rows else None
+
+
+def find_errors_before_rage(team: "Team", user: "User", path: str) -> ErrorsBeforeRage:
+ rows = _rows(ERRORS_BEFORE_RAGE, team, user, "web_analytics_errors_before_rage", path)
+ issues = Counter(issue for _, session_issues in rows for issue in session_issues)
+ issue_id, issue_sessions = issues.most_common(1)[0] if issues else (None, 0)
+ return ErrorsBeforeRage(sessions=len(rows), issue_id=issue_id, issue_sessions=issue_sessions)
diff --git a/products/web_analytics/backend/test/test_rage_pages.py b/products/web_analytics/backend/test/test_rage_pages.py
new file mode 100644
index 000000000000..abc9f8b0bc35
--- /dev/null
+++ b/products/web_analytics/backend/test/test_rage_pages.py
@@ -0,0 +1,91 @@
+from datetime import datetime, timedelta
+
+from posthog.test.base import BaseTest, ClickhouseTestMixin, _create_event, _create_person, flush_persons_and_events
+
+from django.utils import timezone
+
+from products.error_tracking.backend.facade.api import sync_issues_to_clickhouse
+from products.error_tracking.backend.facade.testing import create_issue, create_issue_fingerprint
+from products.web_analytics.backend.facade.rage_pages import (
+ ErrorsBeforeRage,
+ RagePage,
+ find_errors_before_rage,
+ find_top_rage_element,
+ find_top_rage_page,
+)
+
+
+class TestRagePages(ClickhouseTestMixin, BaseTest):
+ def _ago(self, hours: float) -> datetime:
+ return timezone.now() - timedelta(hours=hours)
+
+ def _rage_clicks(
+ self, distinct_id: str, path: str, count: int, *, session_id: str = "", at: datetime | None = None
+ ) -> None:
+ start = at or self._ago(24)
+ for i in range(count):
+ _create_event(
+ team=self.team,
+ event="$rageclick",
+ distinct_id=distinct_id,
+ properties={"$pathname": path, "$session_id": session_id},
+ elements_chain='button:text="Pay now"nth-child="0"nth-of-type="0"',
+ timestamp=start + timedelta(seconds=i),
+ )
+
+ def test_the_top_page_needs_three_people_with_ten_rage_clicks_each_and_ignores_ids_in_the_path(self) -> None:
+ for i, path in enumerate(["/checkout/123", "/checkout/456", "/checkout/789"]):
+ _create_person(team=self.team, distinct_ids=[f"buyer-{i}"])
+ self._rage_clicks(f"buyer-{i}", path, 10)
+ _create_person(team=self.team, distinct_ids=["light"])
+ self._rage_clicks("light", "/checkout/1", 9)
+ for i in range(2):
+ _create_person(team=self.team, distinct_ids=[f"home-{i}"])
+ self._rage_clicks(f"home-{i}", "/home", 20)
+ flush_persons_and_events()
+
+ page = find_top_rage_page(self.team, self.user)
+
+ self.assertEqual(
+ (page, find_top_rage_element(self.team, self.user, "/checkout/…")),
+ (RagePage(path="/checkout/…", people=3, clicks=30), "Pay now"),
+ )
+
+ def _exception(self, distinct_id: str, session: str, fingerprint: str, at: datetime) -> None:
+ _create_event(
+ team=self.team,
+ event="$exception",
+ distinct_id=distinct_id,
+ properties={"$session_id": session, "$exception_fingerprint": fingerprint},
+ timestamp=at,
+ )
+
+ def _issue(self, name: str, fingerprint: str) -> str:
+ issue_id = create_issue(team_id=self.team.id, name=name)
+ create_issue_fingerprint(team_id=self.team.id, issue_id=issue_id, fingerprint=fingerprint)
+ sync_issues_to_clickhouse(issue_ids=[issue_id], team_id=self.team.pk)
+ return str(issue_id)
+
+ def test_errors_before_rage_count_each_issue_once_per_session_of_the_qualifying_people(self) -> None:
+ shared = self._issue("TypeError", "fp-shared")
+ self._issue("KeyError", "fp-other")
+ first_click = self._ago(24)
+ sessions = [
+ ("buyer-1", "s1", 10, [("fp-shared", 20), ("fp-other", 5)]),
+ ("buyer-2", "s2", 10, [("fp-shared", 20)]),
+ ("buyer-3", "s3", 10, [("fp-shared", 60)]),
+ ("buyer-4", "s4a", 5, [("fp-shared", 10)]),
+ ("buyer-4", "s4b", 5, []),
+ ("browser", "s5", 9, [("fp-shared", 10)]),
+ ]
+ for distinct_id in {distinct_id for distinct_id, *_ in sessions}:
+ _create_person(team=self.team, distinct_ids=[distinct_id])
+ for distinct_id, session, clicks, exceptions in sessions:
+ for fingerprint, seconds_before in exceptions:
+ self._exception(distinct_id, session, fingerprint, first_click - timedelta(seconds=seconds_before))
+ self._rage_clicks(distinct_id, "/checkout", clicks, session_id=session, at=first_click)
+ flush_persons_and_events()
+
+ errors = find_errors_before_rage(self.team, self.user, "/checkout")
+
+ self.assertEqual(errors, ErrorsBeforeRage(sessions=5, issue_id=shared, issue_sessions=3))
diff --git a/tach.toml b/tach.toml
index 4b0e9631667d..b5eda2a0b6da 100644
--- a/tach.toml
+++ b/tach.toml
@@ -1647,9 +1647,11 @@ depends_on = [
"products.alerts",
"products.conversations",
"products.dashboards",
+ "products.error_tracking",
"products.product_analytics",
"products.signals",
"products.tasks",
+ "products.web_analytics",
]
layer = "modules"