|
| 1 | +import { |
| 2 | + trackUsageEvent, |
| 3 | + USAGE_EVENT_NAMES, |
| 4 | + type GuidedEditSurface, |
| 5 | +} from "./analytics.js"; |
| 6 | +import { findAngleTargets } from "./direct-manipulation.js"; |
| 7 | + |
| 8 | +export const GUIDED_FIRST_EDIT_EXPERIMENT = "superhero_first_edit_v1"; |
| 9 | + |
| 10 | +export const GUIDED_FIRST_EDIT_TARGET = { |
| 11 | + joint: "knee_right", |
| 12 | + action: "flex", |
| 13 | + initialDegrees: 123, |
| 14 | + suggestedDegrees: 115, |
| 15 | +} as const; |
| 16 | + |
| 17 | +export type GuidedFirstEditStage = |
| 18 | + | "idle" |
| 19 | + | "edit" |
| 20 | + | "success" |
| 21 | + | "dismissed"; |
| 22 | + |
| 23 | +export interface GuidedEditFocusRequest { |
| 24 | + joint: string; |
| 25 | + action: string; |
| 26 | + switchToEditor: boolean; |
| 27 | +} |
| 28 | + |
| 29 | +function targetDegrees(source: string): number | null { |
| 30 | + const target = findAngleTargets(source).find( |
| 31 | + ({ joint, action }) => |
| 32 | + joint === GUIDED_FIRST_EDIT_TARGET.joint && |
| 33 | + action === GUIDED_FIRST_EDIT_TARGET.action, |
| 34 | + ); |
| 35 | + return target?.degrees ?? null; |
| 36 | +} |
| 37 | + |
| 38 | +/** Keep the experiment on the single intended entry point and known source. */ |
| 39 | +export function shouldOfferGuidedFirstEdit( |
| 40 | + pathname: string, |
| 41 | + hash: string, |
| 42 | + source: string, |
| 43 | +): boolean { |
| 44 | + return ( |
| 45 | + pathname === "/play/superhero-landing" && |
| 46 | + hash === "" && |
| 47 | + targetDegrees(source) === GUIDED_FIRST_EDIT_TARGET.initialDegrees |
| 48 | + ); |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Small state machine for the guided activation experiment. Source is inspected |
| 53 | + * only in the browser and is never included in analytics properties. |
| 54 | + */ |
| 55 | +export class GuidedFirstEditSession { |
| 56 | + stage: GuidedFirstEditStage = "idle"; |
| 57 | + private targetWasEdited = false; |
| 58 | + private started = false; |
| 59 | + |
| 60 | + offer(): boolean { |
| 61 | + if (this.stage !== "idle") return false; |
| 62 | + this.stage = "edit"; |
| 63 | + trackUsageEvent(USAGE_EVENT_NAMES.guidedEditShown, { |
| 64 | + experiment: GUIDED_FIRST_EDIT_EXPERIMENT, |
| 65 | + }); |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + begin(surface: GuidedEditSurface): GuidedEditFocusRequest | null { |
| 70 | + if (this.stage !== "edit") return null; |
| 71 | + if (!this.started) { |
| 72 | + this.started = true; |
| 73 | + trackUsageEvent(USAGE_EVENT_NAMES.guidedEditStarted, { |
| 74 | + experiment: GUIDED_FIRST_EDIT_EXPERIMENT, |
| 75 | + surface, |
| 76 | + }); |
| 77 | + } |
| 78 | + return { |
| 79 | + joint: GUIDED_FIRST_EDIT_TARGET.joint, |
| 80 | + action: GUIDED_FIRST_EDIT_TARGET.action, |
| 81 | + switchToEditor: surface === "mobile", |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + noteUserEdit(source: string, userInitiated: boolean): boolean { |
| 86 | + if (this.stage !== "edit" || !userInitiated) return false; |
| 87 | + const degrees = targetDegrees(source); |
| 88 | + this.targetWasEdited = |
| 89 | + degrees !== null && degrees !== GUIDED_FIRST_EDIT_TARGET.initialDegrees; |
| 90 | + return this.targetWasEdited; |
| 91 | + } |
| 92 | + |
| 93 | + /** Call only after the parser has accepted and the viewer has loaded source. */ |
| 94 | + confirmValidCustomRender(source: string): boolean { |
| 95 | + if (this.stage !== "edit" || !this.targetWasEdited) return false; |
| 96 | + const degrees = targetDegrees(source); |
| 97 | + if ( |
| 98 | + degrees === null || |
| 99 | + degrees === GUIDED_FIRST_EDIT_TARGET.initialDegrees |
| 100 | + ) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + this.stage = "success"; |
| 104 | + trackUsageEvent(USAGE_EVENT_NAMES.guidedEditCompleted, { |
| 105 | + experiment: GUIDED_FIRST_EDIT_EXPERIMENT, |
| 106 | + }); |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + dismiss(): boolean { |
| 111 | + if (this.stage !== "edit" && this.stage !== "success") return false; |
| 112 | + const stage = this.stage; |
| 113 | + this.stage = "dismissed"; |
| 114 | + trackUsageEvent(USAGE_EVENT_NAMES.guidedEditDismissed, { |
| 115 | + experiment: GUIDED_FIRST_EDIT_EXPERIMENT, |
| 116 | + stage, |
| 117 | + }); |
| 118 | + return true; |
| 119 | + } |
| 120 | +} |
0 commit comments