Skip to content

Commit

Permalink
refactor: field added tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
BohdanOne committed Nov 27, 2024
1 parent f4298ab commit 56efe78
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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";
Expand Down Expand Up @@ -140,18 +139,7 @@ const TabZone: FC<TabZoneProps> = ({ 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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<br/> that should appear once, like a<br/>
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ({
Expand Down Expand Up @@ -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 }) => {
Expand Down
28 changes: 28 additions & 0 deletions packages/slice-machine/src/utils/tracking/trackFieldAdded.ts
Original file line number Diff line number Diff line change
@@ -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,
}),
});
}

0 comments on commit 56efe78

Please sign in to comment.