Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add env var for logging telemetry events to file #5347

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading