-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegrations.ts
More file actions
251 lines (242 loc) · 7.88 KB
/
Copy pathintegrations.ts
File metadata and controls
251 lines (242 loc) · 7.88 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
import fs from 'node:fs';
import path from 'node:path';
import * as p from '@clack/prompts';
import type { WizardOptions } from './config.js';
export interface Integration {
id: string;
label: string;
/** npm packages whose presence indicates the tool is installed. */
packages: string[];
/** window globals for script-tag installs (no npm package). */
globals?: string[];
/** Step 6 example: attach the Subtext URL to this tool's user metadata. */
linkageExample: string;
}
export const INTEGRATIONS: Integration[] = [
{
id: 'posthog',
label: 'PostHog',
packages: ['posthog-js', 'posthog-js-lite'],
globals: ['posthog'],
linkageExample: `// PostHog
posthog.identify(user.id, { subtext_url: subtextUrl });`,
},
{
id: 'amplitude',
label: 'Amplitude',
packages: ['@amplitude/analytics-browser', 'amplitude-js'],
globals: ['amplitude'],
linkageExample: `// Amplitude
const identifyEvent = new amplitude.Identify().set('subtext_url', subtextUrl);
amplitude.identify(identifyEvent);`,
},
{
id: 'mixpanel',
label: 'Mixpanel',
packages: ['mixpanel-browser'],
globals: ['mixpanel'],
linkageExample: `// Mixpanel
mixpanel.people.set({ subtext_url: subtextUrl });`,
},
{
id: 'statsig',
label: 'Statsig',
packages: ['@statsig/js-client', '@statsig/react-bindings', 'statsig-js', 'statsig-react'],
linkageExample: `// Statsig — include in the StatsigUser custom fields wherever the user object is built
statsigClient.updateUserSync({ ...statsigUser, custom: { ...statsigUser.custom, subtext_url: subtextUrl } });`,
},
{
id: 'sentry',
label: 'Sentry',
packages: ['@sentry/browser', '@sentry/react', '@sentry/nextjs', '@sentry/vue', '@sentry/svelte'],
globals: ['Sentry'],
linkageExample: `// Sentry — set as a tag so every captured error event carries the URL.
// Tags are indexable and searchable in the Sentry UI; cap is 200 chars.
Sentry.setTag('subtext_url', subtextUrl);`,
},
{
id: 'logrocket',
label: 'LogRocket',
packages: ['logrocket'],
globals: ['LogRocket'],
linkageExample: `// LogRocket
LogRocket.identify(user.id, { subtextUrl });`,
},
{
id: 'datadog',
label: 'Datadog',
packages: ['@datadog/browser-rum', '@datadog/browser-logs'],
globals: ['DD_RUM'],
linkageExample: `// Datadog RUM
datadogRum.setGlobalContextProperty('subtext_url', subtextUrl);`,
},
{
id: 'launchdarkly',
label: 'LaunchDarkly',
packages: ['launchdarkly-js-client-sdk', 'launchdarkly-react-client-sdk'],
linkageExample: `// LaunchDarkly — add to the context used when identifying
ldClient.identify({ ...context, subtextUrl });`,
},
{
id: 'growthbook',
label: 'GrowthBook',
packages: ['@growthbook/growthbook', '@growthbook/growthbook-react'],
linkageExample: `// GrowthBook
growthbook.setAttributes({ ...growthbook.getAttributes(), subtext_url: subtextUrl });`,
},
{
id: 'intercom',
label: 'Intercom',
packages: ['@intercom/messenger-js-sdk', 'react-use-intercom'],
globals: ['Intercom'],
linkageExample: `// Intercom
Intercom('update', { subtext_url: subtextUrl });`,
},
{
id: 'pendo',
label: 'Pendo',
packages: ['@pendo/agent'],
globals: ['pendo'],
linkageExample: `// Pendo
pendo.updateOptions({ visitor: { subtext_url: subtextUrl } });`,
},
{
id: 'appcues',
label: 'Appcues',
packages: [],
globals: ['Appcues'],
linkageExample: `// Appcues
Appcues.identify(user.id, { subtextUrl });`,
},
{
id: 'userpilot',
label: 'Userpilot',
packages: ['user-pilot', 'userpilot'],
globals: ['userpilot'],
linkageExample: `// Userpilot
userpilot.identify(user.id, { subtext_url: subtextUrl });`,
},
{
id: 'sprig',
label: 'Sprig',
packages: ['@sprig-technologies/sprig-browser'],
globals: ['Sprig'],
linkageExample: `// Sprig
Sprig('setAttributes', { subtext_url: subtextUrl });`,
},
{
id: 'segment',
label: 'Segment',
packages: ['@segment/analytics-next', 'analytics-node'],
globals: ['analytics'],
linkageExample: `// Segment
analytics.identify(user.id, { subtextUrl });`,
},
];
export interface IntegrationSelection {
integrations: Integration[];
/** Free-text tool names entered via "Other". */
other: string[];
}
/**
* Best-effort detection of which catalog tools this app already uses, by
* matching each integration's known npm package names against `<dir>`'s
* `package.json` (dependencies + devDependencies). Used only to PRE-SELECT the
* picker — it's a convenience, not a source of truth: the agent still detects
* analytics SDKs itself during the install, so anything missed here (script-tag
* / CDN installs, monorepo layouts) is caught then. Framework-agnostic — every
* JS project has a package.json. Never throws: a missing/unparseable file just
* means "detected nothing".
*/
export function detectInstalledIntegrations(dir: string): Integration[] {
let deps: Record<string, unknown> = {};
try {
const pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8')) as {
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
};
deps = { ...pkg.dependencies, ...pkg.devDependencies };
} catch {
return [];
}
const installed = new Set(Object.keys(deps));
return INTEGRATIONS.filter((integration) =>
integration.packages.some((name) => installed.has(name)),
);
}
/**
* Ask which analytics/product tools the app uses so the agent knows what to
* look for during the install. Pre-seeded via --integrations for CI runs.
*/
export async function selectIntegrations(
options: WizardOptions,
): Promise<IntegrationSelection> {
if (options.integrations) {
const known = new Map(INTEGRATIONS.map((i) => [i.id, i]));
const integrations: Integration[] = [];
const other: string[] = [];
for (const raw of options.integrations) {
const id = raw.trim().toLowerCase();
if (!id) continue;
const match = known.get(id);
if (match) integrations.push(match);
else other.push(raw.trim());
}
return { integrations, other };
}
const OTHER = '__other__';
// Pre-select tools we can see in package.json; the user can still uncheck
// them or add others. When nothing is detected the picker opens with nothing
// checked — same as before — and leaving it empty hands detection to the
// agent (its Step 2 explore).
const detected = detectInstalledIntegrations(options.dir);
const detectedIds = new Set(detected.map((i) => i.id));
if (detected.length > 0) {
p.log.info(
`Found in package.json: ${detected
.map((i) => i.label)
.join(', ')} — pre-selected below. Add any others we should link.`,
);
}
const picked = await p.multiselect({
message:
'Which analytics or product tools does this app use? Subtext will link session URLs into each one. (space to toggle, enter to confirm)',
options: [
...INTEGRATIONS.map((i) => ({
value: i.id,
label: i.label,
hint: detectedIds.has(i.id) ? 'detected' : undefined,
})),
{ value: OTHER, label: 'Other', hint: 'name a tool not listed' },
],
initialValues: detected.length > 0 ? detected.map((i) => i.id) : undefined,
required: false,
});
if (p.isCancel(picked)) {
throw new CancelledError();
}
const ids = picked as string[];
const other: string[] = [];
if (ids.includes(OTHER)) {
const typed = await p.text({
message: 'Which other tools? (comma-separated)',
placeholder: 'e.g. Heap, FullStory Anywhere',
});
if (p.isCancel(typed)) throw new CancelledError();
other.push(
...String(typed ?? '')
.split(',')
.map((s) => s.trim())
.filter(Boolean),
);
}
return {
integrations: INTEGRATIONS.filter((i) => ids.includes(i.id)),
other,
};
}
export class CancelledError extends Error {
constructor() {
super('cancelled');
}
}