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

PDE-5636 feat(cli): include extra fields for analytics #939

Merged
merged 10 commits into from
Dec 30, 2024
2 changes: 1 addition & 1 deletion packages/cli/src/oclif/ZapierBaseCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ class ZapierBaseCommand extends Command {
if (!this.args) {
throw new Error('unable to record analytics until args are parsed');
}
return recordAnalytics(this.id, true, Object.keys(this.args), this.flags);
return recordAnalytics(this.id, true, this.args, this.flags);
}
}

Expand Down
21 changes: 18 additions & 3 deletions packages/cli/src/utils/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { callAPI } = require('./api');
// const { readFile } = require('./files');
const debug = require('debug')('zapier:analytics');
const pkg = require('../../package.json');
const { getLinkedAppConfig } = require('../utils/api');
const { ANALYTICS_KEY, ANALYTICS_MODES, IS_TESTING } = require('../constants');
const { readUserConfig, writeUserConfig } = require('./userConfig');

Expand All @@ -20,24 +21,38 @@ const shouldSkipAnalytics = (mode) =>
process.env.DISABLE_ZAPIER_ANALYTICS ||
mode === ANALYTICS_MODES.disabled;

const recordAnalytics = async (command, isValidCommand, argNames, flags) => {
const recordAnalytics = async (command, isValidCommand, args, flags) => {
const analyticsMode = await currentAnalyticsMode();

if (shouldSkipAnalytics(analyticsMode)) {
debug('skipping analytics');
return;
}
const argKeys = Object.keys(args);

const shouldRecordAnonymously = analyticsMode === ANALYTICS_MODES.anonymous;

const integrationIDKey = argKeys.find(
(key) => key.toLowerCase() === 'integrationid',
);
const integrationID = integrationIDKey ? args[integrationIDKey] : undefined;

// Some commands ( like "zapier convert" ) won't have an app directory when called.
// Instead, having the app ID in the arguments.
// In this case, we fallback to using "integrationid" in arguments ( if it's there )
// and don't want to "explode" if appID is missing
Comment on lines +40 to +43
Copy link
Member

Choose a reason for hiding this comment

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

Nice catch!

const linkedAppId =
(await getLinkedAppConfig(undefined, false))?.id || integrationID;

// to make this more testable, we should split this out into its own function
const analyticsBody = {
command,
isValidCommand,
numArgs: argNames.length,
numArgs: argKeys.length,
appId: linkedAppId,
flags: {
...flags,
...(command === 'help' ? { helpCommand: argNames[0] } : {}), // include the beginning of args so we know what they want help on
...(command === 'help' ? { helpCommand: argKeys[0] } : {}), // include the beginning of args so we know what they want help on
},
cliVersion: pkg.version,
os: shouldRecordAnonymously ? undefined : process.platform,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const callAPI = async (
}
}

debug(`>> ${requestOptions.method} ${requestOptions.url}`);
debug(`>> ${requestOptions.method} ${requestOptions.url || res.url}`);

if (requestOptions.body) {
const replacementStr = 'raw zip removed in logs';
Expand Down
Loading