Skip to content

Commit 3a61b10

Browse files
committed
feat(telemetry): add option to opt-in to telemetry
This is opt-in for now whilst we set up and test integration with a backend server (mostly with our own projects) but will be an opt-out option in a future (non-breaking) version.
1 parent df48b5c commit 3a61b10

File tree

5 files changed

+25
-0
lines changed

5 files changed

+25
-0
lines changed

core/cli/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import type { Logger } from 'winston'
33
import util from 'util'
44
import { formatPluginTree } from './messages'
55
import { loadHookInstallations } from './install'
6+
import { enableTelemetry } from './telemetry'
7+
import type { RootOptions } from '@dotcom-tool-kit/plugin/src/root-schema'
68
import { TelemetryRecorder } from '@dotcom-tool-kit/telemetry'
79

810
export { runCommands } from './tasks'
@@ -25,6 +27,7 @@ export async function printConfig(logger: Logger): Promise<void> {
2527

2628
export async function printMergedOptions(logger: Logger, metrics: TelemetryRecorder): Promise<void> {
2729
const config = await loadConfig(logger, { validate: true, root: process.cwd() })
30+
enableTelemetry(metrics, config.pluginOptions['app root'].options as RootOptions)
2831
const hookInstallations = (await loadHookInstallations(logger, metrics, config)).unwrap('invalid hooks')
2932

3033
const mergedOptions = {

core/cli/src/install.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import { loadConfig } from './config'
77
import { hasConfigChanged, updateHashes } from './config/hash'
88
import type { ValidConfig } from '@dotcom-tool-kit/config'
99
import { Hook, HookClass } from '@dotcom-tool-kit/base'
10+
import type { RootOptions } from '@dotcom-tool-kit/plugin/src/root-schema'
1011
import { Validated, invalid, reduceValidated, valid } from '@dotcom-tool-kit/validated'
1112
import { HookModule, reducePluginHookInstallations } from './plugin/reduce-installations'
1213
import { findConflicts, withoutConflicts } from '@dotcom-tool-kit/conflict'
1314
import { formatUninstalledHooks } from './messages'
1415
import { importEntryPoint } from './plugin/entry-point'
1516
import { runInit } from './init'
1617
import { guessSystemCode } from './systemCode'
18+
import { enableTelemetry } from './telemetry'
1719
import { TelemetryRecorder } from '@dotcom-tool-kit/telemetry'
1820

1921
// implementation of the Array#every method that supports asynchronous predicates
@@ -125,6 +127,7 @@ export async function checkInstall(
125127

126128
export default async function installHooks(logger: Logger, metrics: TelemetryRecorder): Promise<ValidConfig> {
127129
const config = await loadConfig(logger, { root: process.cwd() })
130+
enableTelemetry(metrics, config.pluginOptions['app root'].options as RootOptions)
128131

129132
await runInit(logger, config)
130133

core/cli/src/tasks.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { shouldDisableNativeFetch } from './fetch'
1111
import { runInit } from './init'
1212
import { formatInvalidOption } from './messages'
1313
import { guessSystemCode } from './systemCode'
14+
import { enableTelemetry } from './telemetry'
1415
import { type TaskOptions, TaskSchemas } from '@dotcom-tool-kit/schemas'
1516
import { OptionsForTask } from '@dotcom-tool-kit/plugin'
1617
import type { RootOptions } from '@dotcom-tool-kit/plugin/src/root-schema'
@@ -168,6 +169,7 @@ export async function runCommands(
168169
files?: string[]
169170
): Promise<void> {
170171
const config = await loadConfig(logger, { root: process.cwd() })
172+
enableTelemetry(metrics, config.pluginOptions['app root'].options as RootOptions)
171173

172174
const systemCode = await guessSystemCode(config)
173175
let scoped = metrics

core/cli/src/telemetry.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { RootOptions } from '@dotcom-tool-kit/plugin/src/root-schema'
2+
import type { TelemetryProcess, TelemetryRecorder } from '@dotcom-tool-kit/telemetry'
3+
4+
// TODO:IM:20260113 in the future this type signature could be changed to allow
5+
// enabling telemetry even without a valid config (and the root options derived
6+
// from it,) so that we can track metrics related to invalid configurations.
7+
export function enableTelemetry(metrics: TelemetryProcess | TelemetryRecorder, options: RootOptions) {
8+
if (options.enableTelemetry) {
9+
metrics.enable()
10+
}
11+
}

lib/plugin/src/root-schema.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import * as z from 'zod'
22

33
export const RootSchema = z.object({
44
allowNativeFetch: z.boolean().default(false),
5+
enableTelemetry: z
6+
.boolean()
7+
.default(false)
8+
.describe(
9+
'Opt-in to send telemetry on your Tool Kit usage so we can track user patterns and friction points. This will be opt-out in a future version once the telemetry API has stabilised.'
10+
),
511
// TODO:IM:20251112 require this option in a future major version
612
systemCode: z
713
.string()

0 commit comments

Comments
 (0)