diff --git a/lib/gui/app/modules/analytics.js b/lib/gui/app/modules/analytics.js index 116a0f9d..340fec55 100644 --- a/lib/gui/app/modules/analytics.js +++ b/lib/gui/app/modules/analytics.js @@ -31,7 +31,7 @@ const mixpanelToken = settings.get('analyticsMixpanelToken') || const configUrl = settings.get('configUrl') || 'http://balena.io/etcher/static/config.json' -const DEFAULT_PROBABILITY = 1 +const DEFAULT_PROBABILITY = 0.1 const services = { sentry: sentryToken, @@ -48,12 +48,30 @@ resinCorvus.install({ } }) -getConfig(configUrl) - .then((config) => { - resinCorvus.setConfigs({ - mixpanel: getMixpanelConfig(config) - }) +let mixpanelSample = DEFAULT_PROBABILITY + +/** + * @summary Init analytics configurations + * @example initConfig() + */ +const initConfig = async () => { + let validatedConfig = null + try { + const config = await getConfig(configUrl) + const mixpanel = _.get(config, [ 'analytics', 'mixpanel' ], {}) + mixpanelSample = mixpanel.probability || DEFAULT_PROBABILITY + if (isClientEligible(mixpanelSample)) { + validatedConfig = validateMixpanelConfig(mixpanel) + } + } catch (err) { + resinCorvus.logException(err) + } + resinCorvus.setConfigs({ + mixpanel: validatedConfig }) +} + +initConfig() /** * @summary Get etcher configs stored online @@ -70,14 +88,8 @@ function getConfig(url) { * @param {Object} - config */ // eslint-disable-next-line -function getMixpanelConfig(config) { - const analytics = config.analytics || {} - const mixpanel = analytics.mixpanel || {} - const probability = mixpanel.probability || DEFAULT_PROBABILITY - if (Math.random() > probability) { - return null - } - return validateMixpanelConfig(mixpanel) +function isClientEligible(probability) { + return Math.random() > probability } /** @@ -128,7 +140,9 @@ exports.logDebug = resinCorvus.logDebug * image: '/dev/disk2' * }); */ -exports.logEvent = resinCorvus.logEvent +exports.logEvent = (message, data) => { + resinCorvus.logEvent(message, { ...data, sample: mixpanelSample }) +} /** * @summary Log an exception diff --git a/scripts/resin b/scripts/resin index 481cacb7..c80a0d90 160000 --- a/scripts/resin +++ b/scripts/resin @@ -1 +1 @@ -Subproject commit 481cacb7f1533985507c84f28940cada0fb109e5 +Subproject commit c80a0d906ffbfcac7d5490b8daf02d528d4a87aa