Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

fixed translation issue with funnels (#71810)

Sébastienf01f753ff24f
diff --git a/e2e/test/scenarios/admin/i18n/content-translation/dashboards.cy.spec.ts b/e2e/test/scenarios/admin/i18n/content-translation/dashboards.cy.spec.ts
index 29cf0bc5686..0611fbb6bfd 100644
--- a/e2e/test/scenarios/admin/i18n/content-translation/dashboards.cy.spec.ts
+++ b/e2e/test/scenarios/admin/i18n/content-translation/dashboards.cy.spec.ts
@@ -988,4 +988,78 @@ describe("scenarios > content translation > static embeds > dashboards", () => {
});
describe("Boolean content", () => {});
+
+ describe("funnel chart with translated dimension values (metabase#71488)", () => {
+ beforeEach(() => {
+ H.restore();
+ cy.signInAsAdmin();
+ H.activateToken("bleeding-edge");
+
+ uploadTranslationDictionaryViaAPI([
+ { locale: "fr", msgid: "Gadget", msgstr: "Le gadget" },
+ { locale: "fr", msgid: "Doohickey", msgstr: "Le doohickey" },
+ { locale: "fr", msgid: "Gizmo", msgstr: "Le gizmo" },
+ { locale: "fr", msgid: "Widget", msgstr: "Le widget" },
+ ]);
+
+ cy.intercept("GET", "/api/embed/dashboard/*").as("dashboard");
+ cy.intercept("POST", "/api/card/*/query").as("cardQuery");
+
+ cy.signInAsAdmin();
+ });
+
+ it("should render funnel with translated dimension labels in a static embed", () => {
+ H.createQuestion({
+ name: "Products Funnel",
+ display: "funnel",
+ query: {
+ "source-table": PRODUCTS_ID,
+ aggregation: [["count"]],
+ breakout: [["field", PRODUCTS.CATEGORY, null]],
+ },
+ visualization_settings: {
+ "funnel.metric": "count",
+ "funnel.dimension": "CATEGORY",
+ },
+ }).then(({ body: card }) => {
+ H.createDashboard({
+ name: "funnel_dashboard",
+ }).then(({ body: { id: dashboardId } }) => {
+ // Add the funnel card to the dashboard
+ H.addOrUpdateDashboardCard({
+ dashboard_id: dashboardId,
+ card_id: card.id,
+ card: { size_x: 16, size_y: 8 },
+ });
+ H.visitDashboard(dashboardId);
+ H.openLegacyStaticEmbeddingModal({
+ resource: "dashboard",
+ resourceId: dashboardId,
+ });
+ H.publishChanges("dashboard", () => {});
+ H.visitEmbeddedPage(
+ {
+ resource: { dashboard: dashboardId as number },
+ params: {},
+ },
+ {
+ additionalHashOptions: {
+ locale: "fr",
+ },
+ },
+ );
+ // The funnel should render without crashing
+ cy.findByTestId("funnel-chart", { timeout: 10_000 }).should("exist");
+ // Dimension labels should be translated
+ cy.findAllByTestId("funnel-chart-header").should("have.length", 4);
+ cy.findByTestId("funnel-chart").within(() => {
+ cy.findByText("Le gadget").should("exist");
+ cy.findByText("Le doohickey").should("exist");
+ cy.findByText("Le gizmo").should("exist");
+ cy.findByText("Le widget").should("exist");
+ });
+ });
+ });
+ });
+ });
});
diff --git a/frontend/src/metabase/visualizations/components/FunnelNormal.tsx b/frontend/src/metabase/visualizations/components/FunnelNormal.tsx
index 48be8c3078e..04f3bc5075b 100644
--- a/frontend/src/metabase/visualizations/components/FunnelNormal.tsx
+++ b/frontend/src/metabase/visualizations/components/FunnelNormal.tsx
@@ -30,7 +30,8 @@ import type {
HoveredObject,
VisualizationProps,
} from "metabase/visualizations/types";
-import type { RowValue } from "metabase-types/api";
+import type { RowValue, RowValues } from "metabase-types/api";
+import { getRowsForStableKeys } from "metabase-types/api";
import { computeChange } from "../lib/numeric";
@@ -48,6 +49,27 @@ type FunnelStepInfo = {
clicked?: ClickObject;
};
+export function getSortedRows(
+ rows: RowValues[],
+ rowsForKeys: RowValues[],
+ dimensionIndex: number,
+ funnelRows: { key: string | number; enabled: boolean }[] | undefined,
+) {
+ if (!funnelRows) {
+ return rows;
+ }
+
+ return funnelRows
+ .filter((fr) => fr.enabled)
+ .map((fr) => {
+ const idx = rowsForKeys.findIndex(
+ (row) => formatNullable(row[dimensionIndex]) === fr.key,
+ );
+ return idx !== -1 ? rows[idx] : undefined;
+ })
+ .filter(isNotNull);
+}
+
export function FunnelNormal({
className,
rawSeries,
@@ -71,14 +93,18 @@ export function FunnelNormal({
(col) => col.name === settings["funnel.metric"],
);
- const sortedRows = settings["funnel.rows"]
- ? settings["funnel.rows"]
- .filter((fr) => fr.enabled)
- .map((fr) =>
- rows.find((row) => formatNullable(row[dimensionIndex]) === fr.key),
- )
- .filter(isNotNull)
- : rows;
+ // funnel.rows keys are generated from untranslated rows (via
+ // getRowsForStableKeys in Funnel.tsx) so that they stay stable across
+ // locales. We must match against the same untranslated values here,
+ // otherwise content-translated dimension values won't match their keys
+ // and the funnel renders empty (metabase#71488).
+ const rowsForKeys = getRowsForStableKeys(series.data);
+ const sortedRows = getSortedRows(
+ rows,
+ rowsForKeys,
+ dimensionIndex,
+ settings["funnel.rows"],
+ );
const isNarrow = Boolean(gridSize && gridSize.width < 7);
const isShort = Boolean(gridSize && gridSize.height <= 5);
diff --git a/frontend/src/metabase/visualizations/components/FunnelNormal.unit.spec.ts b/frontend/src/metabase/visualizations/components/FunnelNormal.unit.spec.ts
new file mode 100644
index 00000000000..a50ca59d296
--- /dev/null
+++ b/frontend/src/metabase/visualizations/components/FunnelNormal.unit.spec.ts
@@ -0,0 +1,94 @@
+import { calculateFunnelSteps } from "metabase/visualizations/lib/funnel/utils";
+import type { RowValues } from "metabase-types/api";
+import { getRowsForStableKeys } from "metabase-types/api";
+
+import { getSortedRows } from "./FunnelNormal";
+
+/**
+ * These tests replicate the data flow inside FunnelNormal to demonstrate the
+ * bug in https://github.com/metabase/metabase/issues/71488.
+ *
+ * When content translation is active, `data.rows` contains translated values
+ * while `data.untranslatedRows` contains the originals. The `funnel.rows`
+ * setting keys are generated from `getRowsForStableKeys` (which prefers
+ * untranslatedRows), but FunnelNormal must match those keys against the
+ * untranslated rows and then return the *translated* rows for display.
+ */
+describe("FunnelNormal row matching (metabase#71488)", () => {
+ const untranslatedRows: RowValues[] = [
+ ["Awareness", 1000],
+ ["Consideration", 600],
+ ["Purchase", 200],
+ ];
+
+ const translatedRows: RowValues[] = [
+ ["Povědomí", 1000],
+ ["Zvažování", 600],
+ ["Nákup", 200],
+ ];
+
+ // Keys generated from untranslatedRows via getRowsForStableKeys,
+ // matching how Funnel.tsx getValue computes funnel.rows
+ const funnelRowsSettings = [
+ { key: "Awareness", name: "Awareness", enabled: true },
+ { key: "Consideration", name: "Consideration", enabled: true },
+ { key: "Purchase", name: "Purchase", enabled: true },
+ ];
+
+ const dimensionIndex = 0;
+
+ it("matches rows when there is no content translation", () => {
+ const sortedRows = getSortedRows(
+ untranslatedRows,
+ untranslatedRows,
+ dimensionIndex,
+ funnelRowsSettings,
+ );
+ expect(sortedRows).toHaveLength(3);
+ });
+
+ it("returns translated rows for display while matching on untranslated keys", () => {
+ const data = { rows: translatedRows, untranslatedRows };
+ const rowsForKeys = getRowsForStableKeys(data);
+ const sortedRows = getSortedRows(
+ data.rows,
+ rowsForKeys,
+ dimensionIndex,
+ funnelRowsSettings,
+ );
+
+ expect(sortedRows).toHaveLength(3);
+
+ // The displayed dimension values should be the translated ones
+ expect(sortedRows[0][dimensionIndex]).toBe("Povědomí");
+ expect(sortedRows[1][dimensionIndex]).toBe("Zvažování");
+ expect(sortedRows[2][dimensionIndex]).toBe("Nákup");
+
+ // Metrics should still be correct
+ const metrics = sortedRows.map((row) => row[1]) as number[];
+ const funnelData = metrics.map(
+ (metric, i) => [i, metric] as [number, number],
+ );
+ expect(() => calculateFunnelSteps(funnelData, 1, 1)).not.toThrow();
+ });
+
+ it("handles disabled rows correctly", () => {
+ const data = { rows: translatedRows, untranslatedRows };
+ const rowsForKeys = getRowsForStableKeys(data);
+ const settingsWithDisabled = [
+ { key: "Awareness", name: "Awareness", enabled: true },
+ { key: "Consideration", name: "Consideration", enabled: false },
+ { key: "Purchase", name: "Purchase", enabled: true },
+ ];
+ const sortedRows = getSortedRows(
+ data.rows,
+ rowsForKeys,
+ dimensionIndex,
+ settingsWithDisabled,
+ );
+
+ expect(sortedRows).toHaveLength(2);
+ expect(sortedRows[0][dimensionIndex]).toBe("Povědomí");
+ expect(sortedRows[1][dimensionIndex]).toBe("Nákup");
+ });
+});