Merge pull request #2726 from balena-io/analytics-version-weight

Add sample property to Mixpanel events
This commit is contained in:
Lorenzo Alberto Maria Ambrosi 2019-04-08 15:25:39 +02:00 committed by GitHub
commit b73ebb6f92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 16 deletions

View File

@ -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

@ -1 +1 @@
Subproject commit 481cacb7f1533985507c84f28940cada0fb109e5
Subproject commit c80a0d906ffbfcac7d5490b8daf02d528d4a87aa