Skip to content

Commit

Permalink
feat(create-gatsby): add telemetry tracking (#28107)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marvin Frachet committed Nov 17, 2020
1 parent 23b4137 commit f9838f7
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 5 deletions.
4 changes: 3 additions & 1 deletion packages/create-gatsby/package.json
Expand Up @@ -30,11 +30,13 @@
"gatsby-plugin-utils": "^0.4.0-next.0",
"joi": "^17.2.1",
"microbundle": "^0.12.4",
"node-fetch": "^2.6.1",
"prettier": "^2.1.2",
"string-length": "^4.0.1",
"terminal-link": "^2.1.1",
"tiny-spin": "^1.0.2",
"typescript": "^4.0.5"
"typescript": "^4.0.5",
"uuid": "3.4.0"
},
"repository": {
"type": "git",
Expand Down
47 changes: 44 additions & 3 deletions packages/create-gatsby/src/index.ts
Expand Up @@ -11,6 +11,11 @@ import { plugin } from "./components/plugin"
import { makePluginConfigQuestions } from "./plugin-options-form"
import { center, rule, wrap } from "./components/utils"
import { stripIndent } from "common-tags"
import { trackCli } from "./tracking"
import crypto from "crypto"

const sha256 = (str: string): string =>
crypto.createHash(`sha256`).update(str).digest(`hex`)

/**
* Hide string on windows (for emojis)
Expand Down Expand Up @@ -128,6 +133,8 @@ export type PluginConfigMap = Record<string, Record<string, unknown>>
const removeKey = (plugin: string): string => plugin.split(`:`)[0]

export async function run(): Promise<void> {
trackCli(`CREATE_GATSBY_START`)

const { version } = require(`../package.json`)

console.log(c.grey(`create-gatsby version ${version}`))
Expand All @@ -137,8 +144,8 @@ export async function run(): Promise<void> {
${center(c.blueBright.bold.underline(`Welcome to Gatsby!`))}
`
)

Expand All @@ -160,6 +167,23 @@ ${center(c.blueBright.bold.underline(`Welcome to Gatsby!`))}

const data = await enquirer.prompt(questions)

trackCli(`CREATE_GATSBY_SELECT_OPTION`, {
name: `project_name`,
valueString: sha256(data.project),
})
trackCli(`CREATE_GATSBY_SELECT_OPTION`, {
name: `CMS`,
valueString: data.cms || `none`,
})
trackCli(`CREATE_GATSBY_SELECT_OPTION`, {
name: `CSS_TOOLS`,
valueString: data.styling || `none`,
})
trackCli(`CREATE_GATSBY_SELECT_OPTION`, {
name: `PLUGIN`,
valueStringArray: data.features || [],
})

const messages: Array<string> = [
`${w(`🛠 `)}Create a new Gatsby site in the folder ${c.magenta(
data.project
Expand Down Expand Up @@ -238,9 +262,14 @@ ${center(c.blueBright.bold.underline(`Welcome to Gatsby!`))}
`\nGreat! A few of the selections you made need to be configured. Please fill in the options for each plugin now:\n`
)

trackCli(`CREATE_GATSBY_SET_PLUGINS_START`)

const enquirer = new Enquirer<Record<string, {}>>()
enquirer.use(plugin)

pluginConfig = { ...pluginConfig, ...(await enquirer.prompt(config)) }

trackCli(`CREATE_GATSBY_SET_PLUGINS_STOP`)
}

console.log(`
Expand All @@ -259,6 +288,8 @@ ${c.bold(`Thanks! Here's what we'll now do:`)}
})

if (!confirm) {
trackCli(`CREATE_GATSBY_CANCEL`)

console.log(`OK, bye!`)
return
}
Expand All @@ -282,7 +313,7 @@ ${c.bold(`Thanks! Here's what we'll now do:`)}
stripIndent`
${w(`🎉 `)}Your new Gatsby site ${c.bold(
data.project
)} has been successfully bootstrapped
)} has been successfully bootstrapped
at ${c.bold(path.resolve(data.project))}.
`
)
Expand All @@ -297,4 +328,14 @@ ${c.bold(`Thanks! Here's what we'll now do:`)}
console.log(`See all commands at\n
${c.blueBright(`https://www.gatsbyjs.com/docs/gatsby-cli/`)}
`)

trackCli(`CREATE_GATSBY_SUCCESS`)
}

process.on(`exit`, exitCode => {
trackCli(`CREATE_GATSBY_END`, { exitCode })

if (exitCode === -1) {
trackCli(`CREATE_GATSBY_ERROR`)
}
})
46 changes: 46 additions & 0 deletions packages/create-gatsby/src/tracking.ts
@@ -0,0 +1,46 @@
import fetch from "node-fetch"
import uuidv4 from "uuid/v4"
import { getConfigStore } from "./get-config-store"

const store = getConfigStore()
const gatsbyCliVersion = require(`../package.json`).version
const analyticsApi =
process.env.GATSBY_TELEMETRY_API || `https://analytics.gatsbyjs.com/events`

const getMachineId = (): string => {
let machineId = store.get(`telemetry.machineId`)

if (typeof machineId !== `string`) {
machineId = uuidv4()
store.set(`telemetry.machineId`, machineId)
}

return machineId
}

export interface ITrackCliArgs {
name?: string
valueString?: string
exitCode?: number
valueStringArray?: Array<string>
}

export const trackCli = (eventType: string, args?: ITrackCliArgs): void => {
fetch(analyticsApi, {
method: `POST`,
headers: {
"content-type": `application/json`,
"user-agent": `create-gatsby:${gatsbyCliVersion}`,
},
body: JSON.stringify({
eventType,
time: new Date(),
sessionId: uuidv4(),
machineId: getMachineId(),
componentId: `create-gatsby`,
componentVersion: 1,
gatsbyCliVersion,
...args,
}),
}).catch(() => {}) /* do nothing, it's telemetry */
}
1 change: 1 addition & 0 deletions packages/create-gatsby/src/types.d.ts
@@ -1,2 +1,3 @@
declare module "stream-filter"
declare module "ansi-wordwrap"
declare module 'uuid/v4';
2 changes: 1 addition & 1 deletion packages/gatsby-telemetry/src/in-memory-store.ts
@@ -1,4 +1,4 @@
import uuidv4 from "uuid"
import uuidv4 from "uuid/v4"
import os from "os"
import { join } from "path"

Expand Down

0 comments on commit f9838f7

Please sign in to comment.