: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(dashboards): record feature_enabled on dashboard widget added (#62779)
Matt Pua1aec0ec41721
diff --git a/products/dashboards/backend/api/dashboard.py b/products/dashboards/backend/api/dashboard.pyindex cea8cc3ab17c..5c4b5413c635 100644--- a/products/dashboards/backend/api/dashboard.py+++ b/products/dashboards/backend/api/dashboard.py@@ -87,6 +87,7 @@ from products.dashboards.backend.widget_access import (get_widget_api_scope_error,get_widget_product_access_error,)+from products.dashboards.backend.widget_availability import get_widget_feature_enabledfrom products.dashboards.backend.widget_catalog import get_widget_catalog_entriesfrom products.dashboards.backend.widget_create import prepare_widget_tile_createfrom products.dashboards.backend.widget_layouts import (@@ -988,6 +989,10 @@ def _report_dashboard_tile_added("widget_type": widget_type,"dashboard_id": dashboard.id,}+ feature_enabled = get_widget_feature_enabled(widget_type, dashboard.team)+ if feature_enabled is not None:+ # False means the user lands on the widget's setup/custom view rather than real data.+ widget_properties["feature_enabled"] = feature_enabledif tile is not None:widget_properties["tile_id"] = tile.idif tile.widget_id is not None:diff --git a/products/dashboards/backend/api/test/test_dashboard_widgets.py b/products/dashboards/backend/api/test/test_dashboard_widgets.pyindex ed9261115bfd..373f6d3805cc 100644--- a/products/dashboards/backend/api/test/test_dashboard_widgets.py+++ b/products/dashboards/backend/api/test/test_dashboard_widgets.py@@ -7,6 +7,7 @@ from unittest.mock import ANY, patchfrom django.test import override_settingsfrom drf_spectacular.generators import SchemaGenerator+from parameterized import parameterizedfrom rest_framework import statusfrom posthog.api.test.dashboards import DashboardAPI@@ -724,6 +725,37 @@ class TestDashboardWidgets(APIBaseTest):assert len(widget_added_calls) == 1assert widget_added_calls[0][0][2]["widget_type"] == "session_replay_list"+ @parameterized.expand(+ [+ ("session_replay_off", "session_replay_list", "session_recording_opt_in", False),+ ("session_replay_on", "session_replay_list", "session_recording_opt_in", True),+ ("error_tracking_off", "error_tracking_list", "autocapture_exceptions_opt_in", False),+ ("error_tracking_on", "error_tracking_list", "autocapture_exceptions_opt_in", True),+ ]+ )+ @override_settings(IN_UNIT_TESTING=True)+ @patch("products.dashboards.backend.api.dashboard.report_user_action")+ def test_dashboard_widget_added_records_feature_enabled(+ self, _name: str, widget_type: str, team_field: str, expected: bool, mock_report_user_action+ ) -> None:+ setattr(self.team, team_field, expected)+ self.team.save()+ dashboard_id, _ = self.dashboard_api.create_dashboard({"name": "dashboard"})+ mock_report_user_action.reset_mock()++ response = self.client.post(+ f"/api/projects/{self.team.id}/dashboards/{dashboard_id}/widgets/batch/",+ {"widgets": [{"widget_type": widget_type, "config": {"limit": 5}}]},+ )+ assert response.status_code == status.HTTP_201_CREATED++ widget_added_calls = [+ call for call in mock_report_user_action.call_args_list if call[0][1] == "dashboard widget added"+ ]+ assert len(widget_added_calls) == 1+ assert widget_added_calls[0][0][2]["widget_type"] == widget_type+ assert widget_added_calls[0][0][2]["feature_enabled"] is expected+@override_settings(IN_UNIT_TESTING=True)def test_can_batch_create_widget_tiles(self) -> None:dashboard_id, _ = self.dashboard_api.create_dashboard({"name": "dashboard"})diff --git a/products/dashboards/backend/widget_availability.py b/products/dashboards/backend/widget_availability.pynew file mode 100644index 000000000000..a62b3f596700--- /dev/null+++ b/products/dashboards/backend/widget_availability.py@@ -0,0 +1,44 @@+from __future__ import annotations++from typing import TYPE_CHECKING++from products.dashboards.backend.widget_specs.registry import get_widget_spec++if TYPE_CHECKING:+ from posthog.models.team import Team+++def is_widget_availability_requirement_met(requirement: str, team: Team) -> bool | None:+ """Whether a single widget availability requirement is satisfied for the team.++ Mirrors the frontend `isWidgetAvailabilityRequirementMet`. Returns None for an+ unrecognized requirement so callers can treat it as "unknown" rather than "unmet".+ """+ if requirement == "session_replay_enabled":+ return bool(team.session_recording_opt_in)+ if requirement == "exception_autocapture":+ # Reflects the team opt-in only. The widget UI additionally treats "has received+ # exceptions" as enabled, so a team ingesting exceptions without the opt-in flag+ # reads as not-enabled here. We avoid the events-received check to keep this off+ # the hot widget-add path (no ClickHouse query).+ return bool(team.autocapture_exceptions_opt_in)+ return None+++def get_widget_feature_enabled(widget_type: str, team: Team) -> bool | None:+ """Whether the feature(s) a widget depends on are enabled for the team.++ Recorded at widget-add time so we can tell whether the user saw real data or the+ setup/custom view. Returns None when the widget declares no availability+ requirements or any requirement is unrecognized.+ """+ spec = get_widget_spec(widget_type)+ if spec is None or not spec.availability_requirements:+ return None++ results = [is_widget_availability_requirement_met(req, team) for req in spec.availability_requirements]+ # An unrecognized requirement is "unknown", not "met" — we can't confirm the feature is on, so+ # don't silently drop it and risk emitting feature_enabled=True. Report the whole widget as unknown.+ if any(result is None for result in results):+ return None+ return all(results)diff --git a/products/dashboards/frontend/components/DashboardWidgetItem/DashboardWidgetItem.tsx b/products/dashboards/frontend/components/DashboardWidgetItem/DashboardWidgetItem.tsxindex 2920d1ef43c1..7cea737dea63 100644--- a/products/dashboards/frontend/components/DashboardWidgetItem/DashboardWidgetItem.tsx+++ b/products/dashboards/frontend/components/DashboardWidgetItem/DashboardWidgetItem.tsx@@ -94,12 +94,14 @@ type DashboardWidgetItemBodyProps = {widget: NonNullable<DashboardTile<QueryBasedInsightModel>['widget']>definition: DashboardWidgetDefinition | undefinedcomponentProps: DashboardWidgetComponentProps+ dashboardId?: number | null}function DashboardWidgetItemBody({widget,definition,componentProps,+ dashboardId,}: DashboardWidgetItemBodyProps): JSX.Element | null {const catalogEntry = getDashboardWidgetCatalogEntry(widget.widget_type)const WidgetComponent = definition?.Component@@ -113,6 +115,9 @@ function DashboardWidgetItemBody({<WidgetRuntimeAvailabilityGuardavailability={catalogEntry.availability}unavailableContentFallback={definition?.unavailableContentFallback}+ widgetType={widget.widget_type}+ widgetId={widget.id}+ dashboardId={dashboardId}><WidgetComponent {...componentProps} /></WidgetRuntimeAvailabilityGuard>@@ -126,6 +131,7 @@ function DashboardWidgetItemContent({definition,headerCatalogEntry,isUnknownWidgetType,+ dashboardId,result,loading,error,@@ -311,6 +317,7 @@ function DashboardWidgetItemContent({widget={widget}definition={definition}componentProps={componentProps}+ dashboardId={dashboardId}/></ErrorBoundary></WidgetCardBody>@@ -413,6 +420,7 @@ export const DashboardWidgetItem = React.forwardRef<HTMLDivElement, DashboardWiddefinition={definition}headerCatalogEntry={headerCatalogEntry}isUnknownWidgetType={isUnknownWidgetType}+ dashboardId={dashboardId}result={result}loading={loading}error={error}diff --git a/products/dashboards/frontend/components/WidgetAvailabilitySetupPrompt/WidgetAvailabilitySetupPrompt.tsx b/products/dashboards/frontend/components/WidgetAvailabilitySetupPrompt/WidgetAvailabilitySetupPrompt.tsxindex 937fe7718be1..792e053dee6b 100644--- a/products/dashboards/frontend/components/WidgetAvailabilitySetupPrompt/WidgetAvailabilitySetupPrompt.tsx+++ b/products/dashboards/frontend/components/WidgetAvailabilitySetupPrompt/WidgetAvailabilitySetupPrompt.tsx@@ -1,3 +1,5 @@+import posthog from 'posthog-js'+import { LemonButton } from '@posthog/lemon-ui'import type { WidgetAvailabilityConfig } from '../../widget_types/widgetAvailability'@@ -8,11 +10,17 @@ import { WidgetCardProductIntroduction } from '../WidgetCardProductIntroduction/type WidgetAvailabilitySetupPromptProps = {availability: WidgetAvailabilityConfigclassName?: string+ widgetType?: string+ widgetId?: string+ dashboardId?: number | null}export function WidgetAvailabilitySetupPrompt({availability,className,+ widgetType,+ widgetId,+ dashboardId,}: WidgetAvailabilitySetupPromptProps): JSX.Element {const presentation = WIDGET_AVAILABILITY_PRESENTATION[availability.requirement]@@ -30,7 +38,18 @@ export function WidgetAvailabilitySetupPrompt({docsURL={availability.docsHref}actionElementOverride={<div className="flex flex-col items-center gap-4">- <LemonButton type="primary" to={presentation.settingsUrl}>+ <LemonButton+ type="primary"+ to={presentation.settingsUrl}+ onClick={() => {+ posthog.capture('dashboard widget cross product activated', {+ widget_type: widgetType,+ widget_id: widgetId,+ dashboard_id: dashboardId,+ cta: availability.requirement,+ })+ }}+ >{availability.setupActionLabel}</LemonButton></div>diff --git a/products/dashboards/frontend/components/WidgetRuntimeAvailabilityGuard/WidgetRuntimeAvailabilityGuard.tsx b/products/dashboards/frontend/components/WidgetRuntimeAvailabilityGuard/WidgetRuntimeAvailabilityGuard.tsxindex 1f8f0e75355c..e0935e428ef5 100644--- a/products/dashboards/frontend/components/WidgetRuntimeAvailabilityGuard/WidgetRuntimeAvailabilityGuard.tsx+++ b/products/dashboards/frontend/components/WidgetRuntimeAvailabilityGuard/WidgetRuntimeAvailabilityGuard.tsx@@ -6,11 +6,17 @@ import { WidgetAvailabilitySetupPrompt } from '../WidgetAvailabilitySetupPrompt/export type WidgetUnavailableContentFallbackProps = {availability: WidgetAvailabilityConfig+ widgetType?: string+ widgetId?: string+ dashboardId?: number | null}type WidgetRuntimeAvailabilityGuardProps = {availability: WidgetAvailabilityConfig | undefinedunavailableContentFallback?: ComponentType<WidgetUnavailableContentFallbackProps>+ widgetType?: string+ widgetId?: string+ dashboardId?: number | nullchildren: ReactNode}@@ -18,6 +24,9 @@ type WidgetRuntimeAvailabilityGuardProps = {export function WidgetRuntimeAvailabilityGuard({availability,unavailableContentFallback,+ widgetType,+ widgetId,+ dashboardId,children,}: WidgetRuntimeAvailabilityGuardProps): JSX.Element {const { isAvailable, config } = useWidgetAvailability(availability)@@ -27,5 +36,5 @@ export function WidgetRuntimeAvailabilityGuard({}const Fallback = unavailableContentFallback ?? WidgetAvailabilitySetupPrompt- return <Fallback availability={config} />+ return <Fallback availability={config} widgetType={widgetType} widgetId={widgetId} dashboardId={dashboardId} />}