From 2891d4919877d71d89c79a8db5c758f46015d630 Mon Sep 17 00:00:00 2001 From: Bohdan Imiela Date: Fri, 29 Nov 2024 10:01:55 +0100 Subject: [PATCH] feat: track groups pushed --- .../manager/src/managers/telemetry/types.ts | 13 +++ .../sync/actions/trackPushChangesSuccess.ts | 80 +++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/packages/manager/src/managers/telemetry/types.ts b/packages/manager/src/managers/telemetry/types.ts index 9fe6aa8554..fc58e4bf7f 100644 --- a/packages/manager/src/managers/telemetry/types.ts +++ b/packages/manager/src/managers/telemetry/types.ts @@ -24,6 +24,7 @@ export const SegmentEventType = { legacySlice_converted: "legacy-slice:converted", screenshotTaken: "screenshot-taken", changes_pushed: "changes:pushed", + changes_groupPushed: "changes:group-pushed", changes_limitReach: "changes:limit-reach", editor_widgetUsed: "editor:widget-used", open_page_snippet: "page-type:open-snippet", @@ -71,6 +72,7 @@ export const HumanSegmentEventType = { "SliceMachine Legacy Slice Converted", [SegmentEventType.screenshotTaken]: "SliceMachine Screenshot Taken", [SegmentEventType.changes_pushed]: "SliceMachine Changes Pushed", + [SegmentEventType.changes_groupPushed]: "SliceMachine Group Field Pushed", [SegmentEventType.changes_limitReach]: "SliceMachine Changes Limit Reach", [SegmentEventType.editor_widgetUsed]: "SliceMachine Editor Widget Used", [SegmentEventType.open_page_snippet]: @@ -294,6 +296,16 @@ type ChangesPushedSegmentEvent = SegmentEvent< } >; +type ChangesGroupPushedSegmentEvent = SegmentEvent< + typeof SegmentEventType.changes_groupPushed, + { + isInStaticZone: boolean; + isInSlice: boolean; + } & { + [key in FieldType]?: number; + } +>; + type ChangesLimitReachSegmentEvent = SegmentEvent< typeof SegmentEventType.changes_limitReach, { limitType: PushChangesLimitType } @@ -410,6 +422,7 @@ export type SegmentEvents = | LegacySliceConvertedSegmentEvent | ScreenshotTakenSegmentEvent | ChangesPushedSegmentEvent + | ChangesGroupPushedSegmentEvent | ChangesLimitReachSegmentEvent | EditorWidgetUsedSegmentEvent | OpenPageSnippetSegmentEvent diff --git a/packages/slice-machine/src/features/sync/actions/trackPushChangesSuccess.ts b/packages/slice-machine/src/features/sync/actions/trackPushChangesSuccess.ts index 20c4432d80..4f3f7bb68b 100644 --- a/packages/slice-machine/src/features/sync/actions/trackPushChangesSuccess.ts +++ b/packages/slice-machine/src/features/sync/actions/trackPushChangesSuccess.ts @@ -1,3 +1,5 @@ +import { FieldType } from "@prismicio/types-internal/lib/customtypes"; + import { telemetry } from "@/apiClient"; import { countMissingScreenshots } from "@/domain/slice"; import { @@ -94,4 +96,82 @@ export function trackPushChangesSuccess(args: TrackPushChangesSuccessArgs) { hasDeletedDocuments, _includeEnvironmentKind: true, }); + + trackPushedGroups({ changedCustomTypes, changedSlices }); +} + +type TrackPushedGroupsArgs = { + changedSlices: ReadonlyArray; + changedCustomTypes: ReadonlyArray; +}; + +type FieldStats = { + isInStaticZone: boolean; + isInSlice: boolean; +} & { + [key in FieldType]?: number; +}; + +function trackPushedGroups(args: TrackPushedGroupsArgs) { + const { changedCustomTypes, changedSlices } = args; + + const groupsInStaticZone = changedCustomTypes.reduce( + (acc, customType) => { + if (customType.status !== ModelStatus.New) return acc; + + customType.customType.tabs.forEach((tab) => { + tab.value.forEach((field) => { + if (field.value.type === "Group" && field.value.config?.fields) { + const fieldStats: FieldStats = { + isInStaticZone: true, + isInSlice: false, + }; + field.value.config.fields.forEach((field) => { + if (typeof fieldStats[field.value.type] !== "number") { + fieldStats[field.value.type] = 1; + } else { + fieldStats[field.value.type]! += 1; + } + }); + acc.push(fieldStats); + } + }); + }); + + return acc; + }, + [], + ); + + const groupsInSlices = changedSlices.reduce((acc, slice) => { + if (slice.status !== ModelStatus.New) return acc; + + slice.slice.model.variations.forEach((variation) => { + variation.primary?.forEach((field) => { + if (field.value.type === "Group" && field.value.config?.fields) { + const fieldStats: FieldStats = { + isInStaticZone: false, + isInSlice: true, + }; + field.value.config.fields.forEach((field) => { + if (typeof fieldStats[field.value.type] !== "number") { + fieldStats[field.value.type] = 1; + } else { + fieldStats[field.value.type]! += 1; + } + }); + acc.push(fieldStats); + } + }); + }); + + return acc; + }, []); + + [...groupsInSlices, ...groupsInStaticZone].forEach((group) => { + void telemetry.track({ + event: "changes:group-pushed", + ...group, + }); + }); }