From 56efe78fce83626d07fc6324369c815c6beec466 Mon Sep 17 00:00:00 2001 From: Bohdan Imiela Date: Wed, 27 Nov 2024 14:20:04 +0100 Subject: [PATCH] refactor: field added tracking --- .../CustomTypeBuilder/TabZone/index.tsx | 16 ++--------- .../SliceBuilder/FieldZones/index.tsx | 19 ++----------- .../legacy/lib/builders/common/Zone/index.jsx | 2 +- .../common/widgets/Group/ListItem/index.jsx | 16 ++--------- .../getContentTypeForTracking.ts | 0 .../src/utils/tracking/trackFieldAdded.ts | 28 +++++++++++++++++++ 6 files changed, 37 insertions(+), 44 deletions(-) rename packages/slice-machine/src/utils/{ => tracking}/getContentTypeForTracking.ts (100%) create mode 100644 packages/slice-machine/src/utils/tracking/trackFieldAdded.ts diff --git a/packages/slice-machine/src/legacy/lib/builders/CustomTypeBuilder/TabZone/index.tsx b/packages/slice-machine/src/legacy/lib/builders/CustomTypeBuilder/TabZone/index.tsx index 596f619525..da26e43c55 100644 --- a/packages/slice-machine/src/legacy/lib/builders/CustomTypeBuilder/TabZone/index.tsx +++ b/packages/slice-machine/src/legacy/lib/builders/CustomTypeBuilder/TabZone/index.tsx @@ -9,7 +9,6 @@ import type { DropResult } from "react-beautiful-dnd"; import { flushSync } from "react-dom"; import { toast } from "react-toastify"; -import { telemetry } from "@/apiClient"; import { List } from "@/components/List"; import { addField, @@ -36,7 +35,7 @@ import { ensureWidgetTypeExistence, } from "@/legacy/lib/utils"; import { transformKeyAccessor } from "@/legacy/lib/utils/str"; -import { getContentTypeForTracking } from "@/utils/getContentTypeForTracking"; +import { trackFieldAdded } from "@/utils/tracking/trackFieldAdded"; import EditModal from "../../common/EditModal"; import Zone from "../../common/Zone"; @@ -140,18 +139,7 @@ const TabZone: FC = ({ tabId }) => { toast.success(`${field.type === "Group" ? "Group" : "Field"} added`); }); - void telemetry.track({ - event: "field:added", - id, - name: label, - type: newField.type, - isInAGroup: false, - contentType: getContentTypeForTracking(window.location.pathname), - ...(newField.type === "Link" && { - allowText: newField.config?.allowText, - repeat: newField.config?.repeat, - }), - }); + trackFieldAdded(id, newField); }; const onDragEnd = (result: DropResult) => { diff --git a/packages/slice-machine/src/legacy/lib/builders/SliceBuilder/FieldZones/index.tsx b/packages/slice-machine/src/legacy/lib/builders/SliceBuilder/FieldZones/index.tsx index 43e9023492..8dbdd1d339 100644 --- a/packages/slice-machine/src/legacy/lib/builders/SliceBuilder/FieldZones/index.tsx +++ b/packages/slice-machine/src/legacy/lib/builders/SliceBuilder/FieldZones/index.tsx @@ -16,7 +16,6 @@ import { DropResult } from "react-beautiful-dnd"; import { flushSync } from "react-dom"; import { toast } from "react-toastify"; -import { telemetry } from "@/apiClient"; import { List } from "@/components/List"; import { addField, @@ -36,7 +35,7 @@ import { import { Widgets } from "@/legacy/lib/models/common/widgets"; import { ensureDnDDestination } from "@/legacy/lib/utils"; import { transformKeyAccessor } from "@/legacy/lib/utils/str"; -import { getContentTypeForTracking } from "@/utils/getContentTypeForTracking"; +import { trackFieldAdded } from "@/utils/tracking/trackFieldAdded"; const dataTipText = ` The non-repeatable zone is for fields
that should appear once, like a
@@ -128,8 +127,7 @@ const FieldZones: FC = () => { widgetArea: WidgetsArea, { apiId: id, value: newField }: OnSaveFieldProps, ) => { - const { type: widgetTypeName, config } = newField; - const label = config?.label ?? ""; + const { type: widgetTypeName } = newField; const widget = primaryWidgetsArray.find( (sliceBuilderWidget) => @@ -159,18 +157,7 @@ const FieldZones: FC = () => { toast.success(`${widgetTypeName === "Group" ? "Group" : "Field"} added`); }); - void telemetry.track({ - event: "field:added", - id, - name: label, - type: newField.type, - isInAGroup: false, - contentType: getContentTypeForTracking(window.location.pathname), - ...(newField.type === "Link" && { - allowText: newField.config?.allowText, - repeat: newField.config?.repeat, - }), - }); + trackFieldAdded(id, newField); }; const _onCreateOrSave = (widgetArea: WidgetsArea) => { diff --git a/packages/slice-machine/src/legacy/lib/builders/common/Zone/index.jsx b/packages/slice-machine/src/legacy/lib/builders/common/Zone/index.jsx index f3b939e668..35f745194e 100644 --- a/packages/slice-machine/src/legacy/lib/builders/common/Zone/index.jsx +++ b/packages/slice-machine/src/legacy/lib/builders/common/Zone/index.jsx @@ -9,7 +9,7 @@ import { fields as allFields } from "@/domain/fields"; import { AddFieldDropdown } from "@/features/builder/AddFieldDropdown"; import { AddStaticFieldDropdown } from "@/features/builder/AddStaticFieldDropdown"; import { Widgets } from "@/legacy/lib/models/common/widgets"; -import { getContentTypeForTracking } from "@/utils/getContentTypeForTracking"; +import { getContentTypeForTracking } from "@/utils/tracking/getContentTypeForTracking"; import Card from "./Card"; import { ZoneEmptyState } from "./components/ZoneEmptyState"; diff --git a/packages/slice-machine/src/legacy/lib/models/common/widgets/Group/ListItem/index.jsx b/packages/slice-machine/src/legacy/lib/models/common/widgets/Group/ListItem/index.jsx index 0bcb3dae99..34951ee8d5 100644 --- a/packages/slice-machine/src/legacy/lib/models/common/widgets/Group/ListItem/index.jsx +++ b/packages/slice-machine/src/legacy/lib/models/common/widgets/Group/ListItem/index.jsx @@ -20,7 +20,8 @@ import { findWidgetByConfigOrType } from "@/legacy/lib/builders/utils"; import { Groups } from "@/legacy/lib/models/common/Group"; import { ensureDnDDestination } from "@/legacy/lib/utils"; import { transformKeyAccessor } from "@/legacy/lib/utils/str"; -import { getContentTypeForTracking } from "@/utils/getContentTypeForTracking"; +import { getContentTypeForTracking } from "@/utils/tracking/getContentTypeForTracking"; +import { trackFieldAdded } from "@/utils/tracking/trackFieldAdded"; /* eslint-disable */ export const CustomListItem = ({ @@ -69,18 +70,7 @@ export const CustomListItem = ({ isNewGroupField: true, }); - void telemetry.track({ - event: "field:added", - id, - name: label, - type: newField.type, - isInAGroup: true, - contentType: getContentTypeForTracking(window.location.pathname), - ...(newField.type === "Link" && { - allowText: newField.config?.allowText, - repeat: newField.config?.repeat, - }), - }); + trackFieldAdded(id, newField); }; const onSaveField = ({ apiId: previousKey, newKey, value }) => { diff --git a/packages/slice-machine/src/utils/getContentTypeForTracking.ts b/packages/slice-machine/src/utils/tracking/getContentTypeForTracking.ts similarity index 100% rename from packages/slice-machine/src/utils/getContentTypeForTracking.ts rename to packages/slice-machine/src/utils/tracking/getContentTypeForTracking.ts diff --git a/packages/slice-machine/src/utils/tracking/trackFieldAdded.ts b/packages/slice-machine/src/utils/tracking/trackFieldAdded.ts new file mode 100644 index 0000000000..5367e285f5 --- /dev/null +++ b/packages/slice-machine/src/utils/tracking/trackFieldAdded.ts @@ -0,0 +1,28 @@ +import { + Group, + NestableWidget, + UID, +} from "@prismicio/types-internal/lib/customtypes"; + +import { telemetry } from "@/apiClient"; +import { SlicePrimaryFieldSM } from "@/legacy/lib/models/common/Slice"; + +import { getContentTypeForTracking } from "./getContentTypeForTracking"; + +export function trackFieldAdded( + id: string, + field: SlicePrimaryFieldSM | NestableWidget | UID | Group, +) { + void telemetry.track({ + event: "field:added", + id, + name: field.config?.label ?? "", + type: field.type, + isInAGroup: false, + contentType: getContentTypeForTracking(window.location.pathname), + ...(field.type === "Link" && { + allowText: field.config?.allowText, + repeat: field.config?.repeat, + }), + }); +}