-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Expand file tree
/
Copy pathamr-attribution.ts
More file actions
353 lines (331 loc) · 12.6 KB
/
Copy pathamr-attribution.ts
File metadata and controls
353 lines (331 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import type {
AmrEntryAttribution,
TrackingAmrEntrySource,
TrackingPageName,
} from '@open-design/contracts/analytics';
import {
readOnboardingProfile,
type OnboardingProfile,
} from '../state/onboarding-profile';
import { trackAmrEntryClick } from './events';
type Track = (
event: string,
properties: Record<string, unknown>,
options?: { requestId?: string; insertId?: string },
) => void;
interface RecordAmrEntryOptions {
metricsConsent?: boolean;
reuseExistingFrom?: readonly TrackingAmrEntrySource[];
}
interface SyncAmrProfileOptions {
metricsConsent?: boolean;
odDeviceId?: string | null;
now?: Date;
}
const AMR_ATTRIBUTION_STORAGE_KEY = 'open-design:amr-entry-attribution:v1';
const AMR_ATTRIBUTION_TTL_MS = 7 * 24 * 60 * 60 * 1000;
const ENTRY_PAGE_BY_SOURCE: Record<TrackingAmrEntrySource, TrackingPageName> = {
onboarding_amr_card: 'onboarding',
onboarding_amr_sign_in_continue: 'onboarding',
inline_model_switcher_amr_row: 'chat_panel',
settings_amr_agent_card: 'settings',
settings_amr_authorize: 'settings',
settings_amr_console: 'settings',
settings_amr_install: 'settings',
avatar_amr_console: 'chat_panel',
handoff_amr_website: 'artifact',
chat_error_authorize_retry: 'chat_panel',
chat_error_recharge: 'chat_panel',
chat_error_upgrade: 'chat_panel',
chat_balance_gate_upgrade: 'chat_panel',
home_balance_gate_upgrade: 'home',
chat_low_balance_warn_recharge: 'chat_panel',
home_low_balance_warn_recharge: 'home',
chat_balance_gate_sign_in: 'chat_panel',
home_balance_gate_sign_in: 'home',
chat_error_switch_retry_card: 'chat_panel',
generation_preview_authorize_retry: 'file_manager',
generation_preview_recharge: 'file_manager',
generation_preview_switch_retry_card: 'file_manager',
settings_amr_upgrade: 'settings',
inline_amr_upgrade: 'chat_panel',
avatar_amr_upgrade: 'chat_panel',
avatar_amr_agent_card: 'chat_panel',
};
const ONBOARDING_PROFILE_SYNC_SOURCES: readonly TrackingAmrEntrySource[] = [
'onboarding_amr_card',
'onboarding_amr_sign_in_continue',
];
export type { AmrEntryAttribution, TrackingAmrEntrySource };
// Where an amr_entry source surfaces in the product. amr-auth.ts reuses
// this to stamp `page_name` on amr_auth_result from the attribution alone.
export function amrEntryPageForSource(
source: TrackingAmrEntrySource,
): TrackingPageName {
return ENTRY_PAGE_BY_SOURCE[source];
}
export function recordAmrEntry(
track: Track,
sourceDetail: TrackingAmrEntrySource,
now: Date = new Date(),
options: RecordAmrEntryOptions = {},
): AmrEntryAttribution {
const existing = readReusableAmrAttribution(now, options.reuseExistingFrom);
if (existing) return existing;
const profile = readOnboardingProfile();
const attribution: AmrEntryAttribution = {
entryId: `od-amr-${randomId()}`,
sourceProduct: 'open_design',
sourceDetail,
occurredAt: now.toISOString(),
...(profile?.role ? { odRole: profile.role } : {}),
...(profile?.orgSize ? { odOrgSize: profile.orgSize } : {}),
...(profile?.useCase && profile.useCase.length > 0
? { odUseCase: profile.useCase }
: {}),
...(profile?.source ? { odSource: profile.source } : {}),
};
writeAmrAttribution(attribution);
trackAmrEntryClick(track, {
page_name: ENTRY_PAGE_BY_SOURCE[sourceDetail],
area: 'amr_entry',
element: sourceDetail,
action: 'click_amr_entry',
entry_id: attribution.entryId,
source_product: attribution.sourceProduct,
source_detail: attribution.sourceDetail,
entry_occurred_at: attribution.occurredAt,
});
if (options.metricsConsent === true) {
void mirrorAmrEntryToAmrAnalytics(attribution);
}
return attribution;
}
export function readAmrAttribution(now: Date = new Date()): AmrEntryAttribution | null {
if (typeof window === 'undefined') return null;
try {
const raw = window.localStorage.getItem(AMR_ATTRIBUTION_STORAGE_KEY);
if (!raw) return null;
const parsed = JSON.parse(raw) as Partial<AmrEntryAttribution>;
if (!isValidAmrAttribution(parsed)) return null;
if (now.getTime() - Date.parse(parsed.occurredAt) > AMR_ATTRIBUTION_TTL_MS) {
window.localStorage.removeItem(AMR_ATTRIBUTION_STORAGE_KEY);
return null;
}
return parsed;
} catch {
return null;
}
}
export function syncAmrAttributionWithOnboardingProfile(
profile: OnboardingProfile,
options: SyncAmrProfileOptions = {},
): AmrEntryAttribution | null {
const now = options.now ?? new Date();
const existing = readAmrAttribution(now);
if (!existing) return null;
if (!ONBOARDING_PROFILE_SYNC_SOURCES.includes(existing.sourceDetail)) {
return null;
}
const fields = amrProfileFields(profile);
if (!fields) return null;
const next: AmrEntryAttribution = {
...existing,
...fields,
...(options.odDeviceId
? { odDeviceId: options.odDeviceId }
: existing.odDeviceId
? { odDeviceId: existing.odDeviceId }
: {}),
};
writeAmrAttribution(next);
if (options.metricsConsent === true) {
void mirrorAmrOnboardingProfileToAmrAnalytics(next, now);
}
return next;
}
// Resolves the device id to forward to AMR on a handoff, ONLY when the user has
// opted into metrics; otherwise null. Prefers `config.installationId` from the
// current render, falling back to the resolved telemetry device id, then null.
//
// In steady state these two are the same value (the analytics client seeds its
// resolved id from `cfg.installationId`), so the AMR join key still matches the
// telemetry / PostHog / Langfuse device identity. The precedence matters only
// during a `Delete my data` rotation: `config.installationId` is the fresh
// source-of-truth in the current render, while `resolvedDeviceId` (a module
// global in the analytics client) only catches up later when the App-level
// `setIdentity(...)` effect runs `applyIdentity()`. Reading `installationId`
// first forwards the rotated id immediately instead of the stale pre-rotation
// one, so the cross-product join never points at a deleted identity. Neither
// input is the mount-time bootstrap UUID, so this never regresses to that.
export function amrHandoffDeviceId(input: {
metricsConsent: boolean;
resolvedDeviceId: string | null;
installationId: string | null | undefined;
}): string | null {
if (!input.metricsConsent) return null;
return input.installationId ?? input.resolvedDeviceId ?? null;
}
// Builds the AMR handoff URL with Open Design attribution params. When
// `deviceId` is provided it is added as `od_device_id`, so AMR can link the
// landing/registration directly back to this Open Design install instead of
// only through the one-shot entry id. The caller passes it ONLY when the user
// has consented to metrics: AMR is Open Design's official model service, so
// this is a same-owner cross-product link, but it still respects the telemetry
// opt-in. Pass null/undefined to omit it.
export function attributedAmrUrl(
baseUrl: string,
attribution: AmrEntryAttribution,
deviceId?: string | null,
): string {
const params: Record<string, string> = {
od_origin: attribution.sourceProduct,
od_entry_id: attribution.entryId,
od_entry_source: attribution.sourceDetail,
od_entry_at: attribution.occurredAt,
};
if (deviceId) params.od_device_id = deviceId;
try {
const url = new URL(baseUrl);
for (const [key, value] of Object.entries(params)) {
url.searchParams.set(key, value);
}
return url.toString();
} catch {
const separator = baseUrl.includes('?') ? '&' : '?';
return `${baseUrl}${separator}${new URLSearchParams(params).toString()}`;
}
}
function writeAmrAttribution(attribution: AmrEntryAttribution): void {
if (typeof window === 'undefined') return;
try {
window.localStorage.setItem(AMR_ATTRIBUTION_STORAGE_KEY, JSON.stringify(attribution));
} catch {
// Analytics persistence must never block the primary action.
}
}
function amrProfileFields(
profile: OnboardingProfile,
): Pick<
AmrEntryAttribution,
'odRole' | 'odOrgSize' | 'odUseCase' | 'odSource'
> | null {
const role = cleanProfileValue(profile.role);
const orgSize = cleanProfileValue(profile.orgSize);
const source = cleanProfileValue(profile.source);
const useCase = Array.isArray(profile.useCase)
? profile.useCase
.map(cleanProfileValue)
.filter((value): value is string => Boolean(value))
: [];
if (!role && !orgSize && useCase.length === 0 && !source) return null;
return {
...(role ? { odRole: role } : {}),
...(orgSize ? { odOrgSize: orgSize } : {}),
...(useCase.length > 0 ? { odUseCase: useCase } : {}),
...(source ? { odSource: source } : {}),
};
}
function cleanProfileValue(value: unknown): string | null {
if (typeof value !== 'string') return null;
const trimmed = value.trim();
if (!trimmed || trimmed === 'unknown') return null;
return trimmed;
}
function readReusableAmrAttribution(
now: Date,
reuseExistingFrom: readonly TrackingAmrEntrySource[] | undefined,
): AmrEntryAttribution | null {
if (!reuseExistingFrom || reuseExistingFrom.length === 0) return null;
const existing = readAmrAttribution(now);
if (!existing) return null;
return reuseExistingFrom.includes(existing.sourceDetail) ? existing : null;
}
async function mirrorAmrEntryToAmrAnalytics(
attribution: AmrEntryAttribution,
): Promise<void> {
if (typeof fetch !== 'function') return;
const sourcePageName = ENTRY_PAGE_BY_SOURCE[attribution.sourceDetail];
try {
await fetch('/api/integrations/vela/analytics-entry', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
payload: {
pageName: 'open_design',
sourcePageName,
area: 'amr_entry',
element: attribution.sourceDetail,
action: 'click_amr_entry',
entryId: attribution.entryId,
sourceProduct: attribution.sourceProduct,
sourceDetail: attribution.sourceDetail,
entryOccurredAt: attribution.occurredAt,
// Self-reported onboarding profile (optional). Anchored to entryId on
// the AMR side for paid-conversion segmentation. Not added to the
// redirect URL — kept to the consent-gated mirror channel only.
...(attribution.odRole ? { odRole: attribution.odRole } : {}),
...(attribution.odOrgSize ? { odOrgSize: attribution.odOrgSize } : {}),
...(attribution.odUseCase && attribution.odUseCase.length > 0
? { odUseCase: attribution.odUseCase }
: {}),
...(attribution.odSource ? { odSource: attribution.odSource } : {}),
},
}),
});
} catch {
// AMR analytics mirroring must never block the primary Open Design action.
}
}
async function mirrorAmrOnboardingProfileToAmrAnalytics(
attribution: AmrEntryAttribution,
now: Date,
): Promise<void> {
if (typeof fetch !== 'function') return;
try {
await fetch('/api/integrations/vela/analytics-profile', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
payload: {
pageName: 'open_design',
sourcePageName: 'onboarding',
area: 'onboarding',
element: 'about_you_submit',
action: 'submit_profile',
entryId: attribution.entryId,
sourceProduct: attribution.sourceProduct,
sourceDetail: attribution.sourceDetail,
entryOccurredAt: attribution.occurredAt,
profileOccurredAt: now.toISOString(),
...(attribution.odDeviceId
? { odDeviceId: attribution.odDeviceId }
: {}),
...(attribution.odRole ? { odRole: attribution.odRole } : {}),
...(attribution.odOrgSize ? { odOrgSize: attribution.odOrgSize } : {}),
...(attribution.odUseCase && attribution.odUseCase.length > 0
? { odUseCase: attribution.odUseCase }
: {}),
...(attribution.odSource ? { odSource: attribution.odSource } : {}),
},
}),
});
} catch {
// AMR analytics mirroring must never block onboarding completion.
}
}
function isValidAmrAttribution(value: Partial<AmrEntryAttribution>): value is AmrEntryAttribution {
return value.sourceProduct === 'open_design'
&& typeof value.entryId === 'string'
&& value.entryId.length > 0
&& typeof value.sourceDetail === 'string'
&& value.sourceDetail in ENTRY_PAGE_BY_SOURCE
&& typeof value.occurredAt === 'string'
&& Number.isFinite(Date.parse(value.occurredAt));
}
function randomId(): string {
if (typeof crypto !== 'undefined' && 'randomUUID' in crypto) {
return crypto.randomUUID();
}
return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
}