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.

fix(toolbar): keep the web vitals action field for pinned toolbars (#129537)

Rafael Audibertfb06a7e558fa
diff --git a/posthog/api/test/test_web_vitals.py b/posthog/api/test/test_web_vitals.py
index c98884fd5a67..561b8d4e9bb8 100644
--- a/posthog/api/test/test_web_vitals.py
+++ b/posthog/api/test/test_web_vitals.py
@@ -14,6 +14,8 @@ class TestWebVitalsAPI(ClickhouseTestMixin, APIBaseTest):
for result in results:
self.assertEqual(result["data"][-1], expected_values[metrics[result["order"]]])
+ # Toolbars pinned to older posthog-js releases read the metric from `action.custom_name`
+ self.assertEqual(result["action"], {"custom_name": metrics[result["order"]]})
def test_web_vitals_missing_pathname(self):
response = self.client.get(f"/api/environments/{self.team.pk}/web_vitals/")
diff --git a/posthog/api/web_vitals.py b/posthog/api/web_vitals.py
index 9c0d06da3e30..93f0db0e46b5 100644
--- a/posthog/api/web_vitals.py
+++ b/posthog/api/web_vitals.py
@@ -11,6 +11,8 @@ from posthog.hogql_queries.query_runner import ExecutionMode, get_query_runner
from products.access_control.backend.facade.user_access_control import UserAccessControlError
+WEB_VITALS_METRICS = ["INP", "LCP", "CLS", "FCP"]
+
class WebVitalsViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):
"""
@@ -48,35 +50,12 @@ class WebVitalsViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):
{
"kind": "EventsNode",
"event": "$web_vitals",
- "name": "INP",
- "custom_name": "INP",
- "math": "p90",
- "math_property": "$web_vitals_INP_value",
- },
- {
- "kind": "EventsNode",
- "event": "$web_vitals",
- "name": "LCP",
- "custom_name": "LCP",
- "math": "p90",
- "math_property": "$web_vitals_LCP_value",
- },
- {
- "kind": "EventsNode",
- "event": "$web_vitals",
- "name": "CLS",
- "custom_name": "CLS",
+ "name": metric,
+ "custom_name": metric,
"math": "p90",
- "math_property": "$web_vitals_CLS_value",
- },
- {
- "kind": "EventsNode",
- "event": "$web_vitals",
- "name": "FCP",
- "custom_name": "FCP",
- "math": "p90",
- "math_property": "$web_vitals_FCP_value",
- },
+ "math_property": f"$web_vitals_{metric}_value",
+ }
+ for metric in WEB_VITALS_METRICS
],
"trendsFilter": {
"display": "ActionsLineGraph",
@@ -96,4 +75,11 @@ class WebVitalsViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):
if result is None:
return Response({"error": "Failed to calculate web vitals"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
- return Response(result.model_dump(mode="json"), status=status.HTTP_200_OK)
+ response = result.model_dump(mode="json")
+ # posthog-js pins the toolbar to the SDK release, so customer sites keep running old toolbar code
+ # until they upgrade the SDK. Older toolbars find each metric by `action.custom_name` and throw
+ # when `action` is missing, so this field must stay while those SDK versions are in use.
+ for row in response["results"]:
+ row["action"] = {"custom_name": WEB_VITALS_METRICS[row["order"]]}
+
+ return Response(response, status=status.HTTP_200_OK)