Skip to content

Commit

Permalink
feat(cli): add env var for logging telemetry events to file
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Dec 18, 2023
1 parent 45a9c33 commit c53737a
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/@sanity/cli/src/util/createTelemetryStore.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'

Expand Down Expand Up @@ -47,6 +49,7 @@ function getCachedClient(token: string) {

interface Env {
DO_NOT_TRACK?: string
SANITY_TELEMETRY_INSPECT?: string
}

interface Options {
Expand Down Expand Up @@ -132,7 +135,6 @@ export function resolveConsent({env}: Options): Promise<ConsentInformation> {
}
})
}

export function createTelemetryStore<UserProperties>({env}: {env: Env}) {
debug('Initializing telemetry')

Expand All @@ -145,6 +147,12 @@ export function createTelemetryStore<UserProperties>({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 {
Expand Down

0 comments on commit c53737a

Please sign in to comment.