Skip to content

Commit

Permalink
feat: opt in defaults to true and improved messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
field123 committed Jan 30, 2024
1 parent f9c01ea commit 9e4110a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 48 deletions.
53 changes: 50 additions & 3 deletions packages/composable-cli/src/commands/insights/insights-command.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import {
InsightsCommandData,
InsightsCommandError,
} from "./insights.types"
import { promptOptInProductInsights } from "../../lib/insights/opt-in-product-insights-middleware"
import { optInsights } from "../../util/has-opted-insights"
import { trackCommandHandler } from "../../util/track-command-handler"
import { isTTY } from "../../util/is-tty"
import Conf from "conf"
import { renderInfo, renderSuccess } from "../ui"
import { outputContent } from "../output"
import inquirer from "inquirer"

export function createInsightsCommand(
ctx: CommandContext,
Expand Down Expand Up @@ -42,7 +45,7 @@ export function createInsightsCommandHandler(
InsightsCommandError,
InsightsCommandArguments
> {
return async function configCommandHandler(args) {
return async function insightsCommandHandler(args) {
if (!args.interactive || !isTTY()) {
console.warn("When not interactive, the opt-in flag must be provided.")
return {
Expand All @@ -55,7 +58,7 @@ export function createInsightsCommandHandler(
}

if (args.optIn === undefined) {
await promptOptInProductInsights(ctx.store, ctx.logger)
await promptOptInProductInsights(ctx.store)
return {
success: true,
data: {},
Expand All @@ -69,3 +72,47 @@ export function createInsightsCommandHandler(
}
}
}

const optInQuestion: inquirer.QuestionCollection = [
{
type: "confirm",
name: "optIn",
message: `To help us improve, would you like to opt-in to error tracking?`,
default: true,
},
]

export async function promptOptInProductInsights(store: Conf): Promise<void> {
renderInfo({
body: outputContent`This is a new tool. If you opt-in you will help us improve user experience.
Your data will be used solely for improving our tools and user experiences and will always be kept private.`
.value,
customSections: [
{
title: "When you opt-in, we'll collect:\n",
body: {
list: {
items: [
["Commands you run with composable cli"],
["Error messages"],
],
},
},
},
],
})
const answers = await inquirer.prompt(optInQuestion)

if (answers.optIn) {
renderSuccess({
body: "Thank you for opting in. Your data will be kept private.",
})
} else {
renderSuccess({
body: "You have chosen not to opt-in. No data will be collected.",
})
}

optInsights(store, answers.optIn)
}
9 changes: 8 additions & 1 deletion packages/composable-cli/src/commands/login/login-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,14 @@ export function createAuthenticationMiddleware(
(arg) => arg === "login" || arg === "logout",
)

if (isAuthenticated(store) || !args.interactive || isAuthCommand) {
const isInsightsCommand = args._.some((arg) => arg === "insights")

if (
isAuthenticated(store) ||
!args.interactive ||
isAuthCommand ||
isInsightsCommand
) {
return
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,70 +1,42 @@
import { CommandContext, RootCommandArguments } from "../../types/command"
import { MiddlewareFunction } from "yargs"
import { isOptedInsights, optInsights } from "../../util/has-opted-insights"
import inquirer from "inquirer"
import Conf from "conf"
import { logging } from "@angular-devkit/core"

const optInQuestion: inquirer.QuestionCollection = [
{
type: "confirm",
name: "optIn",
message: `
Welcome to Elastic Paths Composable CLI!
This is a new tool. To help us improve, would you like to opt-in to error tracking?
When you opt-in, we'll collect:
- Commands you run with composable cli
- Error messages
Your data will be used solely for improving our tool.
(Your data will be kept private)
`,
default: true, // You can set the default value to true or false as needed
},
]
import { renderInfo } from "../../commands/ui"
import { outputContent, outputToken } from "../../commands/output"

export function createOptInProductInsightsMiddleware(
ctx: CommandContext,
): MiddlewareFunction<RootCommandArguments> {
return async function optInProductInsightsMiddleware(
args: RootCommandArguments,
) {
const { store, logger } = ctx
const { store } = ctx

if (isOptedInsights(store) || isInsightsCommand(args)) {
return
}

if (!args.interactive) {
optInsights(store, false)
optInsights(store, true)
return
}

await promptOptInProductInsights(store, logger)
renderInfo({
headline: "Improving our tools with your help",
body: outputContent`This is a new tool. To help improve the experience we collect:
- Commands you run with composable cli
- Error messages
Your data will be used solely for improving our tools and user experiences and will always be kept private.
if you wish to opt out run ${outputToken.genericShellCommand("ep insights")}
`.value,
})
optInsights(store, true)
return
}
}

function isInsightsCommand(argv: any): boolean {
return argv._[0] === "insights"
}

export async function promptOptInProductInsights(
store: Conf,
logger: logging.Logger,
): Promise<void> {
const answers = await inquirer.prompt(optInQuestion)

if (answers.optIn) {
logger.info("Thank you for opting in. Your data will be kept private.")
} else {
logger.info(
"You have chosen not to opt-in. Your privacy will be fully respected.",
)
}

optInsights(store, answers.optIn)
}

0 comments on commit 9e4110a

Please sign in to comment.