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 11, 2023
1 parent c22938b commit 772ac82
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 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 {createClient, SanityClient} from '@sanity/client'
import {ConsentStatus, createBatchedStore, createSessionId, TelemetryEvent} from '@sanity/telemetry'
import {debug as baseDebug} from '../debug'
Expand All @@ -8,6 +9,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 @@ -57,6 +59,7 @@ function getCachedClient(token: string) {
interface Env {
DO_NOT_TRACK?: string
MOCK_CONSENT?: string
SANITY_TELEMETRY_INSPECT?: string
}

interface Options {
Expand Down Expand Up @@ -147,9 +150,17 @@ export function resolveConsent({env}: Options): Promise<ConsentInformation> {
export function createTelemetryStore<UserProperties>({env}: {env: Env}) {
debug('Initializing telemetry')

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}"`)
}

function sendEvents(entries: TelemetryEvent[]) {
debug('Pretend submitting events:', JSON.stringify(entries))
return Promise.resolve()
return inspectEvents
? appendFile(LOG_FILE_NAME, `${entries.map((entry) => JSON.stringify(entry)).join('\n')}\n`)
: Promise.resolve()
}
const sessionId = createSessionId()
debug('session id: %s', sessionId)
Expand Down

0 comments on commit 772ac82

Please sign in to comment.