Skip to content

Commit

Permalink
fix: make gatsby-telemetry no-op
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Oct 17, 2024
1 parent 02b7aac commit 24ee1ab
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 645 deletions.
10 changes: 1 addition & 9 deletions packages/gatsby-telemetry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,9 @@
"dependencies": {
"@babel/code-frame": "^7.18.6",
"@babel/runtime": "^7.20.13",
"@turist/fetch": "^7.2.0",
"@turist/time": "^0.0.2",
"boxen": "^5.1.2",
"configstore": "^5.0.1",
"fs-extra": "^11.2.0",
"gatsby-core-utils": "^4.14.0-next.2",
"git-up": "^7.0.0",
"is-docker": "^2.2.1",
"lodash": "^4.17.21",
"node-fetch": "^2.6.11"
"git-up": "^7.0.0"
},
"devDependencies": {
"@babel/cli": "^7.20.7",
Expand Down Expand Up @@ -46,7 +39,6 @@
"scripts": {
"build": "babel src --out-dir lib --ignore \"**/__tests__\",\"**/__mocks__\" --extensions \".ts,.js\"",
"prepare": "cross-env NODE_ENV=production npm run build && npm run typegen",
"postinstall": "node src/postinstall.js || true",
"typegen": "rimraf --glob \"lib/**/*.d.ts\" && tsc --emitDeclarationOnly --declaration --declarationDir lib/",
"watch": "babel -w src --out-dir lib --ignore \"**/__tests__\",\"**/__mocks__\" --extensions \".ts,.js\""
},
Expand Down
182 changes: 0 additions & 182 deletions packages/gatsby-telemetry/src/__tests__/telemetry.ts

This file was deleted.

27 changes: 1 addition & 26 deletions packages/gatsby-telemetry/src/create-flush.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,3 @@
import { isCI } from "gatsby-core-utils"
const { join } = require(`path`)
const { fork, spawnSync } = require(`child_process`)
import time, { TimeUnit } from "@turist/time"

export function createFlush(isTrackingEnabled: boolean): () => Promise<void> {
return async function flush(): Promise<void> {
if (!isTrackingEnabled) {
return
}

if (isCI()) {
spawnSync(process.execPath, [join(__dirname, `send.js`)], {
execArgv: [],
timeout: time(1, TimeUnit.Minute),
})
return
}
// Submit events on background with out blocking the main process
// nor relying on it's life cycle
const forked = fork(join(__dirname, `send.js`), {
detached: true,
stdio: `ignore`,
execArgv: [],
})
forked.unref()
}
return async function flush(): Promise<void> {}
}
65 changes: 10 additions & 55 deletions packages/gatsby-telemetry/src/event-storage.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import path from "path"
import Configstore from "configstore"
import createFetch from "@turist/fetch"
import { Store } from "./store"
import { ensureDirSync } from "fs-extra"
import { isTruthy } from "gatsby-core-utils"
import { InMemoryConfigStore } from "./in-memory-store"

const fetch = createFetch()
import { InMemoryConfigStore } from "./in-memory-store"

/* The events data collection is a spooled process that
* buffers events to a local fs based buffer
Expand All @@ -15,20 +11,15 @@ const fetch = createFetch()
* to continue even when working offline.
*/
export class EventStorage {
analyticsApi =
process.env.GATSBY_TELEMETRY_API || `https://analytics.gatsbyjs.com/events`
config: Configstore | InMemoryConfigStore
analyticsApi = process.env.GATSBY_TELEMETRY_API
config: InMemoryConfigStore
store: Store
verbose: boolean
debugEvents: boolean
disabled: boolean

constructor() {
try {
this.config = new Configstore(`gatsby`, {}, { globalConfigPath: true })
} catch (e) {
this.config = new InMemoryConfigStore()
}
this.config = new InMemoryConfigStore()

const baseDir = path.dirname(this.config.path)

Expand All @@ -39,59 +30,23 @@ export class EventStorage {
}

this.store = new Store(baseDir)
this.verbose = isTruthy(process.env.GATSBY_TELEMETRY_VERBOSE)
this.debugEvents = isTruthy(process.env.GATSBY_TELEMETRY_DEBUG)
this.disabled = isTruthy(process.env.GATSBY_TELEMETRY_DISABLED)
this.verbose = false
this.debugEvents = false
this.disabled = true
}

isTrackingDisabled(): boolean {
return this.disabled
}

addEvent(event: unknown): void {
if (this.disabled) {
return
}

const eventString = JSON.stringify(event)

if (this.debugEvents || this.verbose) {
console.log(`Captured event:`, JSON.parse(eventString))

if (this.debugEvents) {
// Bail because we don't want to send debug events
return
}
}

this.store.appendToBuffer(eventString + `\n`)
}
addEvent(event: unknown): void {}

async sendEvents(): Promise<boolean> {
return this.store.startFlushEvents(async (eventsData: string) => {
const events = eventsData
.split(`\n`)
.filter(e => e && e.length > 2) // drop empty lines
.map(e => JSON.parse(e))

return this.submitEvents(events)
})
return true
}

async submitEvents(events: unknown): Promise<boolean> {
try {
const res = await fetch(this.analyticsApi, {
method: `POST`,
headers: {
"content-type": `application/json`,
"user-agent": this.getUserAgent(),
},
body: JSON.stringify(events),
})
return res.ok
} catch (e) {
return false
}
return true
}

getUserAgent(): string {
Expand Down
Loading

0 comments on commit 24ee1ab

Please sign in to comment.