Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: add metric for feature click (#21157)
ecmadaof8d5789b8030
diff --git a/frontend/src/app/analytics/behavior.test.ts b/frontend/src/app/analytics/behavior.test.tsindex b952c736cf..7bcbc5c052 100644--- a/frontend/src/app/analytics/behavior.test.ts+++ b/frontend/src/app/analytics/behavior.test.ts@@ -208,6 +208,15 @@ describe("behavior analytics metrics", () => {test("defines metric names in one map", () => {expect([...behaviorMetricDefinitions.keys()]).toContain("page session");expect([...behaviorMetricDefinitions.keys()]).toContain("page navigated");+ expect([...behaviorMetricDefinitions.keys()]).toContain(+ "locked feature clicked"+ );+ expect([...behaviorMetricDefinitions.keys()]).not.toContain(+ "locked feature viewed"+ );+ expect([...behaviorMetricDefinitions.keys()]).not.toContain(+ "locked feature upgrade clicked"+ );});test("creates allowlisted page session metrics with route context", () => {@@ -276,6 +285,27 @@ describe("behavior analytics metrics", () => {});});+ test("creates locked feature click metrics with gate context", () => {+ expect(+ createBehaviorMetric("locked feature clicked", {+ routeId: "workspace.profile",+ properties: {+ feature: "FEATURE_TWO_FA",+ lock_reason: "subscription_plan",+ required_plan: "ENTERPRISE",+ },+ })+ ).toEqual({+ event: "locked feature clicked",+ properties: {+ route_id: "workspace.profile",+ feature: "FEATURE_TWO_FA",+ lock_reason: "subscription_plan",+ required_plan: "ENTERPRISE",+ },+ });+ });+test("drops unset metric properties", () => {const metric = createBehaviorMetric("instance connection test clicked", {routeId: "workspace.instance.detail",diff --git a/frontend/src/app/analytics/behavior.ts b/frontend/src/app/analytics/behavior.tsindex 861bc302a1..1a3183c6dc 100644--- a/frontend/src/app/analytics/behavior.ts+++ b/frontend/src/app/analytics/behavior.ts@@ -25,6 +25,7 @@ export const behaviorMetricDefinitions = new Map([["connect database clicked", {}],["instance connection test clicked", {}],["instance create clicked", {}],+ ["locked feature clicked", {}],["setup guide action clicked", {}],["setup guide dismissed", {}],["post sync first change clicked", {}],diff --git a/frontend/src/app/analytics/feature-gate.test.ts b/frontend/src/app/analytics/feature-gate.test.tsnew file mode 100644index 0000000000..4e7682b4f6--- /dev/null+++ b/frontend/src/app/analytics/feature-gate.test.ts@@ -0,0 +1,72 @@+import { beforeEach, describe, expect, test, vi } from "vitest";+import {+ PlanFeature,+ PlanType,+} from "@/types/proto-es/v1/subscription_service_pb";+import { captureFeatureGateMetric } from "./feature-gate";++const mocks = vi.hoisted(() => ({+ captureMetric: vi.fn(),+ instanceMissingLicense: vi.fn(),+ getMinimumRequiredPlan: vi.fn(),+}));++vi.mock("@/app/router", () => ({+ router: { currentRoute: { value: { name: "workspace.profile" } } },+}));++vi.mock("@/stores/app", () => ({+ useAppStore: {+ getState: () => ({+ instanceMissingLicense: mocks.instanceMissingLicense,+ getMinimumRequiredPlan: mocks.getMinimumRequiredPlan,+ }),+ },+}));++vi.mock("./provider", () => ({+ behaviorAnalytics: { captureMetric: mocks.captureMetric },+}));++describe("feature gate analytics", () => {+ beforeEach(() => {+ vi.clearAllMocks();+ mocks.instanceMissingLicense.mockReturnValue(false);+ mocks.getMinimumRequiredPlan.mockReturnValue(PlanType.TEAM);+ });++ test("captures safe subscription gate context", () => {+ captureFeatureGateMetric(+ "locked feature clicked",+ PlanFeature.FEATURE_TWO_FA+ );++ expect(mocks.captureMetric).toHaveBeenCalledWith({+ event: "locked feature clicked",+ properties: {+ route_id: "workspace.profile",+ feature: "FEATURE_TWO_FA",+ lock_reason: "subscription_plan",+ required_plan: "TEAM",+ },+ });+ });++ test("distinguishes an instance license gate", () => {+ mocks.instanceMissingLicense.mockReturnValue(true);++ captureFeatureGateMetric(+ "locked feature clicked",+ PlanFeature.FEATURE_DATA_MASKING,+ { name: "instances/test" } as never+ );++ expect(mocks.captureMetric).toHaveBeenCalledWith(+ expect.objectContaining({+ properties: expect.objectContaining({+ lock_reason: "instance_license",+ }),+ })+ );+ });+});diff --git a/frontend/src/app/analytics/feature-gate.ts b/frontend/src/app/analytics/feature-gate.tsnew file mode 100644index 0000000000..5a8a6b0019--- /dev/null+++ b/frontend/src/app/analytics/feature-gate.ts@@ -0,0 +1,43 @@+import { router } from "@/app/router";+import { useAppStore } from "@/stores/app";+import type {+ Instance,+ InstanceResource,+} from "@/types/proto-es/v1/instance_service_pb";+import {+ PlanFeature,+ PlanType,+} from "@/types/proto-es/v1/subscription_service_pb";+import { type BehaviorMetricName, createBehaviorMetric } from "./behavior";+import { behaviorAnalytics } from "./provider";++type FeatureGateMetricName = Extract<+ BehaviorMetricName,+ "locked feature clicked"+>;++export function captureFeatureGateMetric(+ event: FeatureGateMetricName,+ feature: PlanFeature,+ instance?: Instance | InstanceResource+): void {+ const store = useAppStore.getState();+ const instanceMissingLicense = store.instanceMissingLicense(+ feature,+ instance+ );+ const requiredPlan = store.getMinimumRequiredPlan(feature);++ behaviorAnalytics.captureMetric(+ createBehaviorMetric(event, {+ routeId: router.currentRoute.value.name?.toString(),+ properties: {+ feature: PlanFeature[feature],+ lock_reason: instanceMissingLicense+ ? "instance_license"+ : "subscription_plan",+ required_plan: PlanType[requiredPlan],+ },+ })+ );+}diff --git a/frontend/src/components/FeatureBadge.test.tsx b/frontend/src/components/FeatureBadge.test.tsxnew file mode 100644index 0000000000..852b9e7013--- /dev/null+++ b/frontend/src/components/FeatureBadge.test.tsx@@ -0,0 +1,80 @@+import { act, type ReactElement } from "react";+import { createRoot } from "react-dom/client";+import { beforeEach, describe, expect, test, vi } from "vitest";++(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT =+ true;++const mocks = vi.hoisted(() => ({+ captureFeatureGateMetric: vi.fn(),+ useAppStore: vi.fn(),+}));++vi.mock("react-i18next", () => ({+ useTranslation: () => ({ t: (key: string) => key }),+}));++vi.mock("@/hooks/useAppState", () => ({+ useSubscriptionState: () => ({}),+}));++vi.mock("@/stores/app", () => ({ useAppStore: mocks.useAppStore }));++vi.mock("@/app/analytics/feature-gate", () => ({+ captureFeatureGateMetric: mocks.captureFeatureGateMetric,+}));++vi.mock("@/components/RouterLink", () => ({+ RouterLink: ({ children, onClick }: React.ComponentProps<"a">) => (+ <a href="/setting/subscription" onClick={onClick}>+ {children}+ </a>+ ),+}));++vi.mock("./ui/tooltip", () => ({+ Tooltip: ({ children }: { children: React.ReactNode }) => children,+}));++let FeatureBadge: typeof import("./FeatureBadge").FeatureBadge;++const renderIntoContainer = (element: ReactElement) => {+ const container = document.createElement("div");+ const root = createRoot(container);+ act(() => root.render(element));+ return { container, unmount: () => act(() => root.unmount()) };+};++beforeEach(async () => {+ vi.clearAllMocks();+ mocks.useAppStore.mockImplementation(+ (selector: (state: Record<string, unknown>) => unknown) =>+ selector({+ hasInstanceFeature: () => false,+ instanceMissingLicense: () => false,+ getMinimumRequiredPlan: () => 2,+ })+ );+ ({ FeatureBadge } = await import("./FeatureBadge"));+});++describe("FeatureBadge", () => {+ test("captures clicks on a clickable locked badge", () => {+ const { container, unmount } = renderIntoContainer(+ <FeatureBadge feature={1} />+ );++ act(() => {+ container+ .querySelector("a")+ ?.dispatchEvent(new MouseEvent("click", { bubbles: true }));+ });++ expect(mocks.captureFeatureGateMetric).toHaveBeenCalledWith(+ "locked feature clicked",+ 1,+ undefined+ );+ unmount();+ });+});diff --git a/frontend/src/components/FeatureBadge.tsx b/frontend/src/components/FeatureBadge.tsxindex e2d8a888dd..3262acd042 100644--- a/frontend/src/components/FeatureBadge.tsx+++ b/frontend/src/components/FeatureBadge.tsx@@ -1,6 +1,7 @@import { Lock, Sparkles } from "lucide-react";import type { ReactNode } from "react";import { useTranslation } from "react-i18next";+import { captureFeatureGateMetric } from "@/app/analytics/feature-gate";import { RouterLink } from "@/components/RouterLink";import { useSubscriptionState } from "@/hooks/useAppState";import { useAppStore } from "@/stores/app";@@ -92,6 +93,13 @@ export function FeatureBadge({<RouterLinkto="/setting/subscription"className={className ?? "text-accent inline-flex"}+ onClick={() =>+ captureFeatureGateMetric(+ "locked feature clicked",+ feature,+ instance+ )+ }><Sparkles className="w-5 h-5" /></RouterLink>diff --git a/frontend/src/components/ui/feature-modal.test.tsx b/frontend/src/components/ui/feature-modal.test.tsxindex 6fd9bd00a3..605b7fa6c4 100644--- a/frontend/src/components/ui/feature-modal.test.tsx+++ b/frontend/src/components/ui/feature-modal.test.tsx@@ -7,6 +7,7 @@ import { beforeEach, describe, expect, test, vi } from "vitest";).IS_REACT_ACT_ENVIRONMENT = true;const mocks = vi.hoisted(() => ({+ captureFeatureGateMetric: vi.fn(),useSubscriptionState: vi.fn(),useAppStore: vi.fn(),instanceMissingLicense: false,@@ -30,7 +31,14 @@ vi.mock("@/stores/app", () => ({vi.mock("@/app/router", async (importOriginal) => ({...(await importOriginal<typeof import("@/app/router")>()),- router: { push: vi.fn() },+ router: {+ currentRoute: { value: { name: "workspace.profile" } },+ push: vi.fn(),+ },+}));++vi.mock("@/app/analytics/feature-gate", () => ({+ captureFeatureGateMetric: mocks.captureFeatureGateMetric,}));vi.mock("@/types", () => ({@@ -51,6 +59,9 @@ vi.mock("@/types/proto-es/v1/subscription_service_pb", () => ({FREE: 0,TEAM: 1,ENTERPRISE: 3,+ 0: "FREE",+ 1: "TEAM",+ 3: "ENTERPRISE",},}));@@ -119,9 +130,9 @@ const renderIntoContainer = (element: ReactElement) => {document.body.appendChild(container);return {container,- render: () => {+ render: (nextElement = element) => {act(() => {- root.render(element);+ root.render(nextElement);});},unmount: () => {@@ -181,6 +192,51 @@ describe("FeatureModal", () => {unmount();});+ test("captures one click metric for each locked feature modal opening", () => {+ const onOpenChange = vi.fn();+ const { render, unmount } = renderIntoContainer(+ <FeatureModal open feature={1} onOpenChange={onOpenChange} />+ );++ render();+ render(<FeatureModal open feature={1} onOpenChange={onOpenChange} />);++ expect(mocks.captureFeatureGateMetric).toHaveBeenCalledOnce();+ expect(mocks.captureFeatureGateMetric).toHaveBeenLastCalledWith(+ "locked feature clicked",+ 1,+ undefined+ );++ render(<FeatureModal open={false} feature={1} onOpenChange={onOpenChange} />);+ render(<FeatureModal open feature={1} onOpenChange={onOpenChange} />);++ expect(mocks.captureFeatureGateMetric).toHaveBeenCalledTimes(2);+ unmount();+ });++ test("includes instance context in the modal opening metric", () => {+ mocks.instanceMissingLicense = true;+ const instance = { name: "instances/test" } as never;+ const { render, unmount } = renderIntoContainer(+ <FeatureModal+ open+ feature={1}+ instance={instance}+ onOpenChange={() => {}}+ />+ );++ render();++ expect(mocks.captureFeatureGateMetric).toHaveBeenLastCalledWith(+ "locked feature clicked",+ 1,+ instance+ );+ unmount();+ });+test("renders OK-only button when the user lacks settings permission", () => {mocks.hasWorkspacePermissionV2.mockReturnValue(false);const onOpenChange = vi.fn();diff --git a/frontend/src/components/ui/feature-modal.tsx b/frontend/src/components/ui/feature-modal.tsxindex df65fd84f7..86f7ce199d 100644--- a/frontend/src/components/ui/feature-modal.tsx+++ b/frontend/src/components/ui/feature-modal.tsx@@ -1,5 +1,7 @@import { Lock, Sparkles, X } from "lucide-react";+import { useEffect, useRef } from "react";import { useTranslation } from "react-i18next";+import { captureFeatureGateMetric } from "@/app/analytics/feature-gate";import { router } from "@/app/router";import { INSTANCE_ROUTE_DASHBOARD } from "@/app/router/handles";import { Button } from "@/components/ui/button";@@ -46,6 +48,7 @@ export function FeatureModal({ open, feature, instance, onOpenChange }: Props) {const { t } = useTranslation();const { showTrial, trialingDays } = useSubscriptionState();const hasPermission = hasWorkspacePermissionV2("bb.settings.set");+ const wasOpenRef = useRef(false);const resolvedFeature = feature ?? PlanFeature.FEATURE_UNSPECIFIED;const instanceMissingLicense = useAppStore((state) =>@@ -55,6 +58,18 @@ export function FeatureModal({ open, feature, instance, onOpenChange }: Props) {state.getMinimumRequiredPlan(resolvedFeature));+ useEffect(() => {+ const isOpen = open && !!feature;+ if (isOpen && !wasOpenRef.current) {+ captureFeatureGateMetric(+ "locked feature clicked",+ resolvedFeature,+ instance+ );+ }+ wasOpenRef.current = isOpen;+ }, [feature, instance, open, resolvedFeature]);+if (!feature) {return null;}diff --git a/frontend/src/routes/project/ProjectMaskingExemptionPage.tsx b/frontend/src/routes/project/ProjectMaskingExemptionPage.tsxindex 72dcbe4f32..27cceeecfe 100644--- a/frontend/src/routes/project/ProjectMaskingExemptionPage.tsx+++ b/frontend/src/routes/project/ProjectMaskingExemptionPage.tsx@@ -403,6 +403,7 @@ export function ProjectMaskingExemptionPage({<Button onClick={handleGrantClick} disabled={!hasCreatePermission}><FeatureBadgefeature={PlanFeature.FEATURE_DATA_MASKING}+ clickable={false}className="text-white"fallback={<ShieldCheck className="size-4" />}/>