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

fix(create-gatsby): Respect telemetry disable #34495

Merged
merged 6 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion integration-tests/ssr/__tests__/ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe(`SSR`, () => {
expect(String(childProcess.stdout)).toContain(
`testing these paths for differences between dev & prod outputs`
)
}, 30000)
}, 60000)
tyhopp marked this conversation as resolved.
Show resolved Hide resolved

test(`it generates an error page correctly`, async () => {
const src = path.join(__dirname, `/fixtures/bad-page.js`)
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/ssr/test-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ async function run() {
// Fetch once to trigger re-compilation.
await fetch(`${devSiteBasePath}/${path}`)

// Then wait for a second to ensure it's ready to go.
// Then wait for 6 seconds to ensure it's ready to go.
// Otherwise, tests are flaky depending on the speed of the testing machine.
await new Promise(resolve => {
setTimeout(() => resolve(), 1000)
setTimeout(() => resolve(), 6000) // Revert to original time to see if it makes a difference
})

let devStatus = 200
Expand Down
61 changes: 61 additions & 0 deletions packages/create-gatsby/src/__tests__/tracking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
let isTrackingEnabled: () => boolean

const get = jest.fn()
const set = jest.fn()

jest.doMock(`../get-config-store`, () => {
return {
getConfigStore: (): unknown => {
return {
get,
set,
}
},
}
})

describe(`isTrackingEnabled`, () => {
beforeEach(() => {
jest.resetModules()
isTrackingEnabled = require(`../tracking`).isTrackingEnabled
})

it(`is enabled by default`, async () => {
const enabled = isTrackingEnabled()
expect(enabled).toBeTrue()
})

it(`respects the setting of the config store`, async () => {
get.mockImplementationOnce(key => {
if (key === `telemetry.enabled`) {
return false
} else {
return true
}
})

const enabled = isTrackingEnabled()
expect(enabled).toBeFalse()

const cachedEnabled = isTrackingEnabled()
expect(cachedEnabled).toBeFalse()
})

describe(`isTrackingEnabled / process.env.GATSBY_TELEMETRY_DISABLED`, () => {
tyhopp marked this conversation as resolved.
Show resolved Hide resolved
beforeAll(() => {
process.env.GATSBY_TELEMETRY_DISABLED = `true`
})

it(`respects the setting of the environment variable`, async () => {
tyhopp marked this conversation as resolved.
Show resolved Hide resolved
const enabled = isTrackingEnabled()
expect(enabled).toBeFalse()

const cachedEnabled = isTrackingEnabled()
expect(cachedEnabled).toBeFalse()
})

afterAll(() => {
process.env.GATSBY_TELEMETRY_DISABLED = undefined
})
})
})
23 changes: 23 additions & 0 deletions packages/create-gatsby/src/is-truthy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copied from gatsby-core-utils to avoid depending on it, similar to get-config-store
//
// Returns true for `true`, true, positive numbers
// Returns false for `false`, false, 0, negative integers and anything else
export function isTruthy(value: any): boolean {
// Return if Boolean
if (typeof value === `boolean`) return value

// Return false if null or undefined
if (value === undefined || value === null) return false

// If the String is true or false
if (value.toLowerCase() === `true`) return true
if (value.toLowerCase() === `false`) return false

// Now check if it's a number
const number = parseInt(value, 10)
if (isNaN(number)) return false
if (number > 0) return true

// Default to false
return false
}
32 changes: 32 additions & 0 deletions packages/create-gatsby/src/tracking.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import fetch from "node-fetch"
import { v4 as uuidv4 } from "@lukeed/uuid"
import { getConfigStore } from "./get-config-store"
import { isTruthy } from "./is-truthy"

const store = getConfigStore()
const gatsbyCliVersion = require(`../package.json`).version
const analyticsApi =
process.env.GATSBY_TELEMETRY_API || `https://analytics.gatsbyjs.com/events`
let trackingEnabled: boolean | undefined
const trackingDisabledFromEnvVar: boolean | undefined = isTruthy(
process.env.GATSBY_TELEMETRY_DISABLED
)

const getMachineId = (): string => {
let machineId = store.get(`telemetry.machineId`)
Expand All @@ -28,7 +33,34 @@ export interface ITrackCliArgs {

const sessionId = uuidv4()

// Adapted from gatsby-telemetry
export function isTrackingEnabled(): boolean {
// Cache the result
if (trackingEnabled !== undefined) {
return trackingEnabled
}

let enabled = store.get(`telemetry.enabled`) as boolean | null

if (enabled === undefined || enabled === null) {
enabled = true
store.set(`telemetry.enabled`, enabled)
}
trackingEnabled = enabled

if (trackingDisabledFromEnvVar) {
enabled = false
trackingEnabled = enabled
}
tyhopp marked this conversation as resolved.
Show resolved Hide resolved

return enabled
}

export const trackCli = (eventType: string, args?: ITrackCliArgs): void => {
if (!isTrackingEnabled()) {
return
}

fetch(analyticsApi, {
method: `POST`,
headers: {
Expand Down