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(create-gatsby): add telemetry tracking #28107

Merged
merged 7 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion packages/create-gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@types/configstore": "^4.0.0",
"@types/fs-extra": "^9.0.2",
"@types/node": "^14.14.5",
"@types/uuid": "^8.3.0",
"ansi-wordwrap": "^1.0.2",
"common-tags": "^1.8.0",
"enquirer": "^2.3.6",
Expand All @@ -30,11 +31,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": "^8.3.1"
},
"repository": {
"type": "git",
Expand Down
47 changes: 44 additions & 3 deletions packages/create-gatsby/src/index.ts
Original file line number Diff line number Diff line change
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!`))}


`
)
console.log(c.red(rule()))
Expand Down Expand Up @@ -167,6 +174,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),
mfrachet marked this conversation as resolved.
Show resolved Hide resolved
})
trackCli(`CREATE_GATSBY_SELECT_OPTION`, {
mfrachet marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -245,9 +269,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 @@ -266,6 +295,8 @@ ${c.bold(`Thanks! Here's what we'll now do:`)}
})

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

console.log(`OK, bye!`)
return
}
Expand All @@ -289,7 +320,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 @@ -304,4 +335,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import fetch from "node-fetch"
import { v4 as uuidv4 } from "uuid"
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 => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sweet! Didn't realise it was so simple.

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