Add sample property to Mixpanel events

Change-type: patch
Signed-off-by: Lorenzo Alberto Maria Ambrosi <lorenzoa@balena.io>
This commit is contained in:
Lorenzo Alberto Maria Ambrosi 2019-04-03 12:18:15 +02:00
parent af462b3486
commit fc1c1b402b

View File

@ -31,7 +31,7 @@ const mixpanelToken = settings.get('analyticsMixpanelToken') ||
const configUrl = settings.get('configUrl') || 'http://balena.io/etcher/static/config.json' const configUrl = settings.get('configUrl') || 'http://balena.io/etcher/static/config.json'
const DEFAULT_PROBABILITY = 1 const DEFAULT_PROBABILITY = 0.1
const services = { const services = {
sentry: sentryToken, sentry: sentryToken,
@ -48,10 +48,16 @@ resinCorvus.install({
} }
}) })
let mixpanelSample = DEFAULT_PROBABILITY
getConfig(configUrl) getConfig(configUrl)
.then((config) => { .then((config) => {
const mixpanel = _.get(config, [ 'analytics', 'mixpanel' ], {})
const probability = mixpanel.probability || DEFAULT_PROBABILITY
mixpanelSample = probability
const validatedConfig = isClientEligible(probability) ? validateMixpanelConfig(mixpanel) : null
resinCorvus.setConfigs({ resinCorvus.setConfigs({
mixpanel: getMixpanelConfig(config) mixpanel: validatedConfig
}) })
}) })
@ -70,14 +76,8 @@ function getConfig(url) {
* @param {Object} - config * @param {Object} - config
*/ */
// eslint-disable-next-line // eslint-disable-next-line
function getMixpanelConfig(config) { function isClientEligible(probability) {
const analytics = config.analytics || {} return Math.random() > probability
const mixpanel = analytics.mixpanel || {}
const probability = mixpanel.probability || DEFAULT_PROBABILITY
if (Math.random() > probability) {
return null
}
return validateMixpanelConfig(mixpanel)
} }
/** /**
@ -128,7 +128,12 @@ exports.logDebug = resinCorvus.logDebug
* image: '/dev/disk2' * image: '/dev/disk2'
* }); * });
*/ */
exports.logEvent = resinCorvus.logEvent exports.logEvent = (message, data) => {
const sessionData = {
mixpanelSample
}
resinCorvus.logEvent(message, { ...data, sessionData })
}
/** /**
* @summary Log an exception * @summary Log an exception