Skip to content

Commit

Permalink
Merge pull request #939 from zapier/PDE-5636_expand_analyticsBody
Browse files Browse the repository at this point in the history
PDE-5636 feat(cli): include extra fields for analytics
  • Loading branch information
Natay authored Dec 30, 2024
2 parents 4f83bfe + 89f4c45 commit eee9c32
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
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
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

0 comments on commit eee9c32

Please sign in to comment.