Skip to content

Commit

Permalink
fix(create-gatsby): Respect telemetry disable
Browse files Browse the repository at this point in the history
  • Loading branch information
tyhopp committed Jan 14, 2022
1 parent 9f421a0 commit 860a54c
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
59 changes: 59 additions & 0 deletions packages/create-gatsby/src/__tests__/tracking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { getConfigStore } from "../get-config-store"

const mockGetFunction = jest.fn()
const mockSetFunction = jest.fn()

jest.mock(`../get-config-store`, () => {
return {
getConfigStore: (): unknown => {
return {
items: {},
set(key: string, value: unknown): void {
mockSetFunction(key, value)
;(this as any).items[key] = value
},
get(key: string): unknown {
mockGetFunction(key)
return (this as any).items[key]
},

__reset(): void {
;(this as any).items = {}
},
}
},
}
})

let isTrackingEnabled

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

afterEach(() => {
jest.resetAllMocks()
})

it(`is enabled by default`, async () => {
const enabled = isTrackingEnabled()
expect(enabled).toBe(true)
expect(mockGetFunction).toHaveBeenCalledWith(`telemetry.enabled`)
expect(mockSetFunction).toHaveBeenCalledWith(`telemetry.enabled`, true)
})

// TODO - Implement remaining tests

it.skip(`respects the setting of the config store`, async () => {
const store = getConfigStore()
store.set(`telemetry.enabled`, false)
const enabled = isTrackingEnabled()
expect(enabled).toBe(false)
})

it.skip(`respects the setting of the environment variable`, async () => {})

it.skip(`caches the setting for all calls in create-gatsby`, async () => {})
})
24 changes: 24 additions & 0 deletions packages/create-gatsby/src/is-truthy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// TODO - Check if we want to add core utils as a dependency or not,
// that's where this is taken from.

// 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
}

return enabled
}

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

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

0 comments on commit 860a54c

Please sign in to comment.