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.

3.6 KB
Raw Blame
import hashlib

from products.error_tracking.backend.facade.api import get_issue_basics
from products.today.backend.facade.contracts import HighlightCandidate, HighlightContext, HighlightFindings
from products.today.backend.facade.enums import HighlightLabel
from products.today.backend.facade.highlight_helpers import human, short, users, uses_product
from products.web_analytics.backend.facade.rage_pages import (
    ErrorsBeforeRage,
    RagePage,
    find_errors_before_rage,
    find_top_rage_element,
    find_top_rage_page,
)

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 _error_item(
    context: HighlightContext, page: RagePage, errors: ErrorsBeforeRage, title: str
) -> HighlightCandidate | 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 HighlightCandidate(
        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,
    )


def find_rage_page(context: HighlightContext) -> HighlightFindings:
    if not uses_product(context, *RAGE_PRODUCTS):
        return HighlightFindings()
    page = context.daily("rage_page", lambda: find_top_rage_page(context.team, context.user))
    if page is None:
        return HighlightFindings()
    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 HighlightFindings(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 HighlightFindings(
        items=(
            HighlightCandidate(
                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",
            ),
        )
    )