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: don't record insight/dashboard view tracking during staff impersonation (#88608)

danazou4d382624d89c
diff --git a/posthog/api/test/dashboards/test_dashboard.py b/posthog/api/test/dashboards/test_dashboard.py
index 59de60545765..c3746336051e 100644
--- a/posthog/api/test/dashboards/test_dashboard.py
+++ b/posthog/api/test/dashboards/test_dashboard.py
@@ -962,6 +962,16 @@ class TestDashboard(APIBaseTest, QueryMatchingTest):
)
self.assertEqual(response["tiles"][0]["insight"]["result"][0]["count"], 0)
+ def test_impersonated_view_does_not_bump_last_accessed_at(self) -> None:
+ dashboard = Dashboard.objects.create(team=self.team, name="dashboard", created_by=self.user)
+
+ with patch("products.dashboards.backend.api.dashboard.is_impersonated", return_value=True):
+ response = self.client.get(f"/api/projects/{self.team.id}/dashboards/{dashboard.pk}")
+
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ dashboard.refresh_from_db()
+ self.assertIsNone(dashboard.last_accessed_at)
+
# :KLUDGE: avoid making extra queries that are explicitly not cached in tests. Avoids false N+1-s.
@override_settings(PERSON_ON_EVENTS_OVERRIDE=False, PERSON_ON_EVENTS_V2_OVERRIDE=False)
@snapshot_postgres_queries
diff --git a/products/dashboards/backend/api/dashboard.py b/products/dashboards/backend/api/dashboard.py
index cd18be43c2a0..0f239ecda37c 100644
--- a/products/dashboards/backend/api/dashboard.py
+++ b/products/dashboards/backend/api/dashboard.py
@@ -64,6 +64,7 @@ from posthog.event_usage import EventSource, get_event_source, report_user_actio
from posthog.exceptions_capture import capture_exception
from posthog.helpers import create_dashboard_from_template
from posthog.helpers.dashboard_templates import create_from_template, dashboard_template_from_creation_payload
+from posthog.helpers.impersonation import is_impersonated
from posthog.helpers.trigram_search import (
DESCRIPTION_FIELD,
MAX_SEARCH_LENGTH,
@@ -2634,7 +2635,10 @@ class DashboardsViewSet(
dashboard = self.get_object()
access_method = dashboard_access_method(request)
- record_dashboard_view(dashboard, access_method)
+ # Views during staff impersonation aren't the team's own activity - skip the write
+ # so support sessions don't bump the team-facing "Last accessed" (it also feeds cache warming).
+ if not is_impersonated(request):
+ record_dashboard_view(dashboard, access_method)
serializer_context = self.get_serializer_context()
serializer_context["dashboard_access_method"] = access_method
serializer = DashboardSerializer(dashboard, context=serializer_context)
@@ -2680,7 +2684,9 @@ class DashboardsViewSet(
# Do all database operations and data loading synchronously first
access_method = dashboard_access_method(request)
- record_dashboard_view(dashboard, access_method)
+ # Skip the "Last accessed" bump during staff impersonation (see retrieve)
+ if not is_impersonated(request):
+ record_dashboard_view(dashboard, access_method)
context = self.get_serializer_context()
diff --git a/products/product_analytics/backend/presentation/insight.py b/products/product_analytics/backend/presentation/insight.py
index 1adca256c40b..8089704ae032 100644
--- a/products/product_analytics/backend/presentation/insight.py
+++ b/products/product_analytics/backend/presentation/insight.py
@@ -2532,7 +2532,8 @@ When set, the specified dashboard's filters and date range override will be appl
description=(
"Record that the current user has just viewed one or more insights. "
"Submitted ids that do not belong to the current project or that point at deleted insights "
- "are silently dropped. Returns 201 on success regardless of how many ids were retained."
+ "are silently dropped, as are views from impersonated staff-support sessions. "
+ "Returns 201 on success regardless of how many ids were retained."
),
)
@action(methods=["POST"], detail=False, required_scopes=["insight:read"])
@@ -2541,6 +2542,11 @@ When set, the specified dashboard's filters and date range override will be appl
Update insight view timestamps in bulk.
Expects: {"insight_ids": [1, 2, 3, ...]}
"""
+ # Views during staff impersonation aren't the team's own activity - skip the write
+ # so support sessions don't bump the team-facing "Last viewed" column.
+ if is_impersonated(request):
+ return Response(status=status.HTTP_201_CREATED)
+
insight_ids: list[int] = request.validated_data["insight_ids"]
visible_insight_ids = list(
diff --git a/products/product_analytics/backend/tests/api/test_insight.py b/products/product_analytics/backend/tests/api/test_insight.py
index 0ae5df2b97dd..76e4e18e7fba 100644
--- a/products/product_analytics/backend/tests/api/test_insight.py
+++ b/products/product_analytics/backend/tests/api/test_insight.py
@@ -2955,6 +2955,19 @@ class TestInsight(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):
datetime(2022, 3, 22, 0, 0, tzinfo=ZoneInfo("UTC")),
)
+ def test_insight_viewed_not_recorded_during_impersonation(self) -> None:
+ filter_dict = {"events": [{"id": "$pageview"}]}
+ insight = Insight.objects.create(filters=Filter(data=filter_dict).to_dict(), team=self.team, short_id="viewed0")
+
+ with patch("products.product_analytics.backend.presentation.insight.is_impersonated", return_value=True):
+ response = self.client.post(
+ f"/api/projects/{self.team.id}/insights/viewed",
+ {"insight_ids": [insight.id]},
+ )
+
+ self.assertEqual(response.status_code, status.HTTP_201_CREATED)
+ self.assertEqual(InsightViewed.objects.count(), 0)
+
def test_update_insight_viewed(self) -> None:
filter_dict = {"events": [{"id": "$pageview"}]}
insight = Insight.objects.create(
diff --git a/products/product_analytics/frontend/generated/api.ts b/products/product_analytics/frontend/generated/api.ts
index 029c79090536..c355f77e3ea1 100644
--- a/products/product_analytics/frontend/generated/api.ts
+++ b/products/product_analytics/frontend/generated/api.ts
@@ -1061,7 +1061,7 @@ export const getInsightsViewedCreateUrl = (projectId: string, params?: InsightsV
}
/**
- * Record that the current user has just viewed one or more insights. Submitted ids that do not belong to the current project or that point at deleted insights are silently dropped. Returns 201 on success regardless of how many ids were retained.
+ * Record that the current user has just viewed one or more insights. Submitted ids that do not belong to the current project or that point at deleted insights are silently dropped, as are views from impersonated staff-support sessions. Returns 201 on success regardless of how many ids were retained.
*/
export const insightsViewedCreate = async (
projectId: string,
diff --git a/products/product_analytics/frontend/generated/api.zod.ts b/products/product_analytics/frontend/generated/api.zod.ts
index 9a213d9965cf..b674febc8736 100644
--- a/products/product_analytics/frontend/generated/api.zod.ts
+++ b/products/product_analytics/frontend/generated/api.zod.ts
@@ -331,7 +331,7 @@ export const InsightsGenerateMetadataCreateBody = /* @__PURE__ */ zod
.describe('Deep\/recursive schema (opaque in Zod — use TypeScript types for full shape)')
/**
- * Record that the current user has just viewed one or more insights. Submitted ids that do not belong to the current project or that point at deleted insights are silently dropped. Returns 201 on success regardless of how many ids were retained.
+ * Record that the current user has just viewed one or more insights. Submitted ids that do not belong to the current project or that point at deleted insights are silently dropped, as are views from impersonated staff-support sessions. Returns 201 on success regardless of how many ids were retained.
*/
export const insightsViewedCreateBodyInsightIdsMax = 2500