Skip to content

Commit

Permalink
feat: track groups pushed (#1493)
Browse files Browse the repository at this point in the history
  • Loading branch information
BohdanOne authored Dec 5, 2024
1 parent 384bded commit 2f67adb
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/manager/src/managers/telemetry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -410,6 +422,7 @@ export type SegmentEvents =
| LegacySliceConvertedSegmentEvent
| ScreenshotTakenSegmentEvent
| ChangesPushedSegmentEvent
| ChangesGroupPushedSegmentEvent
| ChangesLimitReachSegmentEvent
| EditorWidgetUsedSegmentEvent
| OpenPageSnippetSegmentEvent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import {
FieldType,
NestableWidget,
UID,
} from "@prismicio/types-internal/lib/customtypes";

import { telemetry } from "@/apiClient";
import { countMissingScreenshots } from "@/domain/slice";
import { GroupSM } from "@/legacy/lib/models/common/Group";
import {
ChangedCustomType,
ChangedSlice,
Expand Down Expand Up @@ -94,4 +101,85 @@ export function trackPushChangesSuccess(args: TrackPushChangesSuccessArgs) {
hasDeletedDocuments,
_includeEnvironmentKind: true,
});

trackPushedGroups({ changedCustomTypes, changedSlices });
}

type TrackPushedGroupsArgs = {
changedSlices: ReadonlyArray<ChangedSlice>;
changedCustomTypes: ReadonlyArray<ChangedCustomType>;
};
type FieldsCount = {
[key in FieldType]?: number;
};
type FieldStats = {
isInStaticZone: boolean;
isInSlice: boolean;
} & FieldsCount;

function trackPushedGroups(args: TrackPushedGroupsArgs) {
const { changedCustomTypes, changedSlices } = args;

const groupsInStaticZone = changedCustomTypes.reduce<FieldStats[]>(
(acc, customType) => {
if (customType.status !== ModelStatus.New) return acc;

customType.customType.tabs.forEach((tab) => {
const fieldsCount = countGroupFields(acc, tab.value, "custom-type");
acc.push(...fieldsCount);
});

return acc;
},
[],
);

const groupsInSlices = changedSlices.reduce<FieldStats[]>((acc, slice) => {
if (slice.status !== ModelStatus.New) return acc;

slice.slice.model.variations.forEach((variation) => {
const fieldsCount = countGroupFields(acc, variation.primary, "slice");
acc.push(...fieldsCount);
});

return acc;
}, []);
[...groupsInSlices, ...groupsInStaticZone].forEach((group) => {
void telemetry.track({
event: "changes:group-pushed",
...group,
});
});
}

interface Field {
key: string;
value: GroupSM | UID | NestableWidget;
}
function countGroupFields<TField extends Field>(
fieldStats: FieldStats[],
fields: TField[] | undefined,
groupParent: "custom-type" | "slice",
) {
if (!fields) return fieldStats;

const newFieldState = fieldStats.slice();

fields.forEach((field) => {
if (field.value.type === "Group" && field.value.config?.fields) {
const fieldsCount: FieldsCount = {};
field.value.config.fields.forEach(({ value: fieldValue }) => {
const value = fieldsCount[fieldValue.type] ?? 0;
fieldsCount[fieldValue.type] = value + 1;
});

newFieldState.push({
isInStaticZone: groupParent === "custom-type",
isInSlice: groupParent === "slice",
...fieldsCount,
});
}
});

return newFieldState;
}

0 comments on commit 2f67adb

Please sign in to comment.