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 19, 2023
1 parent 02f4cf2 commit 0edcc8b
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 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 @@ -150,6 +153,12 @@ export function createTelemetryStore<UserProperties>({
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 0edcc8b

Please sign in to comment.