diff --git a/packages/@sanity/cli/src/util/createTelemetryStore.ts b/packages/@sanity/cli/src/util/createTelemetryStore.ts index 96dfb30368f..393fd878c23 100644 --- a/packages/@sanity/cli/src/util/createTelemetryStore.ts +++ b/packages/@sanity/cli/src/util/createTelemetryStore.ts @@ -1,3 +1,4 @@ +import {appendFile} from 'fs/promises' import {SanityClient} from '@sanity/client' import {ConsentStatus, createBatchedStore, createSessionId, TelemetryEvent} from '@sanity/telemetry' import {debug as baseDebug} from '../debug' @@ -10,6 +11,7 @@ import {createExpiringConfig} from './createExpiringConfig' const debug = baseDebug.extend('telemetry') const FIVE_MINUTES = 1000 * 60 * 5 +const LOG_FILE_NAME = 'telemetry-events.ndjson' export const TELEMETRY_CONSENT_CONFIG_KEY = 'telemetryConsent' @@ -47,6 +49,7 @@ function getCachedClient(token: string) { interface Env { DO_NOT_TRACK?: string + SANITY_TELEMETRY_INSPECT?: string } interface Options { @@ -132,7 +135,6 @@ export function resolveConsent({env}: Options): Promise { } }) } - export function createTelemetryStore({env}: {env: Env}) { debug('Initializing telemetry') @@ -145,6 +147,12 @@ export function createTelemetryStore({env}: {env: Env}) { debug('No user token found. Something is not quite right') return Promise.reject(new Error('User is not logged in')) } + const inspectEvents = isTrueish(env.SANITY_TELEMETRY_INSPECT) + if (inspectEvents) { + // eslint-disable-next-line no-console + console.info(`SANITY_TELEMETRY_INSPECT is set, appending events to "${LOG_FILE_NAME}"`) + await appendFile(LOG_FILE_NAME, `${batch.map((entry) => JSON.stringify(entry)).join('\n')}\n`) + } const client = getCachedClient(token) debug('Submitting %s telemetry events', batch.length) try {