Skip to content

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

Move search term hashing into the analytics module (#79124)

Frasere491dcef5cf7
diff --git a/frontend/src/metabase/analytics/index.ts b/frontend/src/metabase/analytics/index.ts
index dbdccbd59ee..e4b95ba2021 100644
--- a/frontend/src/metabase/analytics/index.ts
+++ b/frontend/src/metabase/analytics/index.ts
@@ -1,3 +1,4 @@
export { createSnowplowTracker } from "./snowplow";
export { trackPageView } from "./page-view";
export { trackSchemaEvent, trackSimpleEvent } from "./event";
+export { hashSearchTerm, shouldReportSearchTerm } from "./search-term";
diff --git a/frontend/src/metabase/common/search/term.ts b/frontend/src/metabase/analytics/search-term.ts
similarity index 100%
rename from frontend/src/metabase/common/search/term.ts
rename to frontend/src/metabase/analytics/search-term.ts
diff --git a/frontend/src/metabase/api/analytics.ts b/frontend/src/metabase/api/analytics.ts
index 874aacb91bb..57c2ce16e5c 100644
--- a/frontend/src/metabase/api/analytics.ts
+++ b/frontend/src/metabase/api/analytics.ts
@@ -1,7 +1,10 @@
import _ from "underscore";
-import { trackSchemaEvent } from "metabase/analytics";
-import { hashSearchTerm, shouldReportSearchTerm } from "metabase/common/search";
+import {
+ hashSearchTerm,
+ shouldReportSearchTerm,
+ trackSchemaEvent,
+} from "metabase/analytics";
import { openSaveDialog } from "metabase/utils/dom";
import {
toSnowplowContentTypes,
diff --git a/frontend/src/metabase/api/analytics.unit.spec.ts b/frontend/src/metabase/api/analytics.unit.spec.ts
index 0f39a2394ae..865835f11a3 100644
--- a/frontend/src/metabase/api/analytics.unit.spec.ts
+++ b/frontend/src/metabase/api/analytics.unit.spec.ts
@@ -3,9 +3,13 @@ import { createMockSearchResults } from "metabase-types/api/mocks";
import { registerSearchStarted, trackFulfilledSearch } from "./analytics";
+// hashSearchTerm/shouldReportSearchTerm are stubbed to avoid crypto.subtle / Settings in the hashing
+// path; we only care about the tracking lifecycle here.
jest.mock("metabase/analytics", () => ({
trackSchemaEvent: jest.fn(),
trackSimpleEvent: jest.fn(),
+ hashSearchTerm: jest.fn(async () => "hashed"),
+ shouldReportSearchTerm: jest.fn(() => false),
}));
// Reached through requireMock rather than a top-level import: the analytics-import lint rule forbids
@@ -14,12 +18,6 @@ const { trackSchemaEvent: mockTrackSchemaEvent } = jest.requireMock<{
trackSchemaEvent: jest.Mock;
}>("metabase/analytics");
-// Avoid crypto.subtle / Settings in the hashing path; we only care about the tracking lifecycle here.
-jest.mock("metabase/common/search/term", () => ({
- hashSearchTerm: jest.fn(async () => "hashed"),
- shouldReportSearchTerm: jest.fn(() => false),
-}));
-
// Real timers, not fake ones: underscore's `_.debounce` binds `Date.now` at import, before jest could
// install fake timers, so `advanceTimersByTime` never moves the clock it reads. Wait past the 300ms
// window (see DOCUMENT_SEARCH_TRACK_DEBOUNCE_MS in analytics.ts) instead.
diff --git a/frontend/src/metabase/common/search/analytics.ts b/frontend/src/metabase/common/search/analytics.ts
index 2133a037665..0ccea0928e2 100644
--- a/frontend/src/metabase/common/search/analytics.ts
+++ b/frontend/src/metabase/common/search/analytics.ts
@@ -1,9 +1,11 @@
-import { trackSchemaEvent } from "metabase/analytics";
+import {
+ hashSearchTerm,
+ shouldReportSearchTerm,
+ trackSchemaEvent,
+} from "metabase/analytics";
import { toSnowplowContext } from "metabase-types/analytics";
import type { SearchContext } from "metabase-types/api";
-import { hashSearchTerm, shouldReportSearchTerm } from "./term";
-
type TrackSearchClickParams = {
itemType: "item" | "view_more";
position: number;
diff --git a/frontend/src/metabase/common/search/index.ts b/frontend/src/metabase/common/search/index.ts
index 7580101a20d..36e901fa43c 100644
--- a/frontend/src/metabase/common/search/index.ts
+++ b/frontend/src/metabase/common/search/index.ts
@@ -1,4 +1,3 @@
-export * from "./term";
export * from "./analytics";
export * from "./constants";
export * from "./types";