diff --git a/packages/cli/src/oclif/ZapierBaseCommand.js b/packages/cli/src/oclif/ZapierBaseCommand.js index cae912ab1..24c23cbfa 100644 --- a/packages/cli/src/oclif/ZapierBaseCommand.js +++ b/packages/cli/src/oclif/ZapierBaseCommand.js @@ -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); } } diff --git a/packages/cli/src/utils/analytics.js b/packages/cli/src/utils/analytics.js index 9fc4d99bd..79f0c50d5 100644 --- a/packages/cli/src/utils/analytics.js +++ b/packages/cli/src/utils/analytics.js @@ -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'); @@ -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, diff --git a/packages/cli/src/utils/api.js b/packages/cli/src/utils/api.js index 5b7287a63..a0691b52f 100644 --- a/packages/cli/src/utils/api.js +++ b/packages/cli/src/utils/api.js @@ -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';