Skip to content

Commit

Permalink
feat: track field update (#1530)
Browse files Browse the repository at this point in the history
  • Loading branch information
xrutayisire authored Jan 16, 2025
1 parent 66352b6 commit 0619073
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 11 deletions.
19 changes: 19 additions & 0 deletions packages/manager/src/managers/telemetry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const SegmentEventType = {
pageView: "page-view",
openVideoTutorials: "open-video-tutorials",
field_added: "field:added",
field_updated: "field:updated",
field_settingsOpened: "field:settings-opened",
customType_created: "custom-type:created",
customType_sliceZoneUpdated: "custom-type:slice-zone-updated",
Expand Down Expand Up @@ -58,6 +59,7 @@ export const HumanSegmentEventType = {
[SegmentEventType.pageView]: "SliceMachine Page View",
[SegmentEventType.openVideoTutorials]: "SliceMachine Open Video Tutorials",
[SegmentEventType.field_added]: "SliceMachine Field Added",
[SegmentEventType.field_updated]: "SliceMachine Field Updated",
[SegmentEventType.field_settingsOpened]: "SliceMachine Field Settings Opened",
[SegmentEventType.customType_created]: "SliceMachine Custom Type Created",
[SegmentEventType.customType_sliceZoneUpdated]:
Expand Down Expand Up @@ -209,6 +211,22 @@ type FieldAddedSegmentEvent = SegmentEvent<
}
>;

type FieldUpdatedSegmentEvent = SegmentEvent<
typeof SegmentEventType.field_updated,
{
previousId: string;
id: string;
idUpdated: boolean;
name: string;
type: FieldType;
isInAGroup: boolean;
contentType: "page type" | "custom type" | "slice";
allowText?: boolean;
repeat?: boolean;
variants?: string[];
}
>;

type FieldSettingsOpenedSegmentEvent = SegmentEvent<
typeof SegmentEventType.field_settingsOpened,
{
Expand Down Expand Up @@ -401,6 +419,7 @@ export type SegmentEvents =
| PageViewSegmentEvent
| OpenVideoTutorialsSegmentEvent
| FieldAddedSegmentEvent
| FieldUpdatedSegmentEvent
| FieldSettingsOpenedSegmentEvent
| CustomTypeCreatedSegmentEvent
| CustomTypeSliceZoneUpdatedSegmentEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
} from "@/legacy/lib/utils";
import { transformKeyAccessor } from "@/legacy/lib/utils/str";
import { trackFieldAdded } from "@/utils/tracking/trackFieldAdded";
import { trackFieldUpdated } from "@/utils/tracking/trackFieldUpdated";

import EditModal from "../../common/EditModal";
import Zone from "../../common/Zone";
Expand Down Expand Up @@ -71,7 +72,7 @@ type OnSaveFieldProps = {
apiId: string;
newKey: string;
value: TabField;
isNewGroupField?: boolean;
inGroupFieldAction?: "add" | "update";
};

const TabZone: FC<TabZoneProps> = ({ tabId }) => {
Expand Down Expand Up @@ -169,7 +170,7 @@ const TabZone: FC<TabZoneProps> = ({ tabId }) => {
apiId: previousKey,
newKey,
value,
isNewGroupField,
inGroupFieldAction,
}: OnSaveFieldProps) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
if (ensureWidgetTypeExistence(Widgets, value.type)) {
Expand All @@ -186,10 +187,22 @@ const TabZone: FC<TabZoneProps> = ({ tabId }) => {
});

setCustomType(newCustomType, () => {
if (isNewGroupField === true) {
if (inGroupFieldAction === "add") {
toast.success("Field added");
}
});

// We don't want to track the group field update when it's for the management of a
// field within a group (add or update).
// It would result in double tracking, the one for the field touched within the group
// and the group itself.
if (!inGroupFieldAction) {
trackFieldUpdated({
previousId: previousKey,
id: newKey,
field: newField,
});
}
};

const onCreateOrSave = (props: OnSaveFieldProps) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { Widgets } from "@/legacy/lib/models/common/widgets";
import { ensureDnDDestination } from "@/legacy/lib/utils";
import { transformKeyAccessor } from "@/legacy/lib/utils/str";
import { trackFieldAdded } from "@/utils/tracking/trackFieldAdded";
import { trackFieldUpdated } from "@/utils/tracking/trackFieldUpdated";

const dataTipText = ` The non-repeatable zone
is for fields<br/> that should appear once, like a<br/>
Expand Down Expand Up @@ -68,7 +69,7 @@ type OnSaveFieldProps = {
apiId: string;
newKey: string;
value: SlicePrimaryFieldSM;
isNewGroupField?: boolean;
inGroupFieldAction?: "add" | "update";
};

const FieldZones: FC = () => {
Expand Down Expand Up @@ -105,7 +106,7 @@ const FieldZones: FC = () => {

const _onSave = (
widgetArea: WidgetsArea,
{ apiId: previousKey, newKey, value, isNewGroupField }: OnSaveFieldProps,
{ apiId: previousKey, newKey, value, inGroupFieldAction }: OnSaveFieldProps,
) => {
const newSlice = updateField({
slice,
Expand All @@ -117,10 +118,18 @@ const FieldZones: FC = () => {
});

setSlice(newSlice, () => {
if (isNewGroupField === true) {
toast.success("Group added");
if (inGroupFieldAction === "add") {
toast.success("Field added");
}
});

// We don't want to track the group field update when it's for the management of a
// field within a group (add or update).
// It would result in double tracking, the one for the field touched within the group
// and the group itself.
if (!inGroupFieldAction) {
trackFieldUpdated({ previousId: previousKey, id: newKey, field: value });
}
};

const _onSaveNewField = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { ensureDnDDestination } from "@/legacy/lib/utils";
import { transformKeyAccessor } from "@/legacy/lib/utils/str";
import { getContentTypeForTracking } from "@/utils/tracking/getContentTypeForTracking";
import { trackFieldAdded } from "@/utils/tracking/trackFieldAdded";
import { trackFieldUpdated } from "@/utils/tracking/trackFieldUpdated";

/* eslint-disable */
export const CustomListItem = ({
Expand Down Expand Up @@ -67,24 +68,34 @@ export const CustomListItem = ({
apiId: groupItem.key,
newKey: groupItem.key,
value: Groups.toSM(newGroupValue),
isNewGroupField: true,
inGroupFieldAction: "add",
});

trackFieldAdded({ id, field: newField, isInAGroup: true });
};

const onSaveField = ({ apiId: previousKey, newKey, value }) => {
const updatedField =
value.type === GroupFieldType ? Groups.fromSM(value) : value;
const newGroupValue = updateFieldInGroup({
group: Groups.fromSM(groupItem.value),
previousFieldId: previousKey,
newFieldId: newKey,
field: value.type === GroupFieldType ? Groups.fromSM(value) : value,
field: updatedField,
});

saveItem({
apiId: groupItem.key,
newKey: groupItem.key,
value: Groups.toSM(newGroupValue),
inGroupFieldAction: "update",
});

trackFieldUpdated({
previousId: previousKey,
id: newKey,
field: updatedField,
isInAGroup: true,
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ export interface GroupListItemProps<F extends TabField> {
apiId,
newKey,
value,
isNewGroupField,
inGroupFieldAction,
}: {
apiId: string;
newKey: string;
value: F;
isNewGroupField?: boolean;
inGroupFieldAction?: "add" | "update";
}) => void;
HintElement: JSX.Element;
}
Expand Down
37 changes: 37 additions & 0 deletions packages/slice-machine/src/utils/tracking/trackFieldUpdated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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";

type TrackFieldUpdatedArgs = {
id: string;
previousId: string;
field: SlicePrimaryFieldSM | NestableWidget | UID | Group;
isInAGroup?: boolean;
};

export function trackFieldUpdated(args: TrackFieldUpdatedArgs) {
const { id, previousId = id, field, isInAGroup = false } = args;

void telemetry.track({
event: "field:updated",
previousId: previousId,
id,
idUpdated: previousId !== id,
name: field.config?.label ?? "",
type: field.type,
isInAGroup,
contentType: getContentTypeForTracking(window.location.pathname),
...(field.type === "Link" && {
allowText: field.config?.allowText,
repeat: field.config?.repeat,
variants: field.config?.variants,
}),
});
}

0 comments on commit 0619073

Please sign in to comment.