diff --git a/lib/submit-analytics.js b/lib/submit-analytics.js index a3000f32..ac9fd9d7 100644 --- a/lib/submit-analytics.js +++ b/lib/submit-analytics.js @@ -27,55 +27,53 @@ if ('REALM_DISABLE_ANALYTICS' in process.env) { module.exports = function(){}; - return; -} +} else { + const os = require('os'); + const crypto = require('crypto'); + const fs = require('fs'); + const request = require('request'); -const os = require('os'); -const crypto = require('crypto'); -const fs = require('fs'); -const request = require('request'); - -function sha256(data) { - let hash = crypto.createHash('sha256'); - hash.update(data); - return hash.digest('hex'); -} - -function getDarwinIdentifier() { - const interfaces = os.networkInterfaces(); - const iface = interfaces["en0"] || interfaces["en1"]; - if (!iface) { - return Buffer.from('unknown', 'utf8'); + function sha256(data) { + let hash = crypto.createHash('sha256'); + hash.update(data); + return hash.digest('hex'); } - const mac = iface[0].mac.replace(/:/g, ''); - return new Buffer(mac, 'hex'); -} + function getDarwinIdentifier() { + const interfaces = os.networkInterfaces(); + const iface = interfaces["en0"] || interfaces["en1"]; + if (!iface) { + return Buffer.from('unknown', 'utf8'); + } -function getLinuxIdentifier() { - if (fs.existsSync('/var/lib/dbus/machine-id')) { + const mac = iface[0].mac.replace(/:/g, ''); + return new Buffer(mac, 'hex'); + } + + function getLinuxIdentifier() { + if (fs.existsSync('/var/lib/dbus/machine-id')) { return fs.readFileSync('/var/lib/dbus/machine-id'); - } else if (fs.existsSync('/etc/machine-id')) { + } else if (fs.existsSync('/etc/machine-id')) { return fs.readFileSync('/etc/machine-id'); - } else { + } else { return Buffer.from('unknown', 'utf8'); + } } -} -function getAnonymizedMachineIdentifier() { - switch (os.platform()) { + function getAnonymizedMachineIdentifier() { + switch (os.platform()) { case 'darwin': return sha256(getDarwinIdentifier()); case 'linux': return sha256(getLinuxIdentifier()); default: return null; + } } -} -module.exports = function(eventName) { - const identifier = getAnonymizedMachineIdentifier(); - const payload = { + module.exports = function(eventName) { + const identifier = getAnonymizedMachineIdentifier(); + const payload = { 'event': eventName, 'properties': { 'token': 'aab85907a13e1ff44a95be539d9942a9', @@ -89,12 +87,13 @@ module.exports = function(eventName) { 'OS Version': os.release(), 'Node.js versions': process.versions } - }; + }; - request(`https://api.mixpanel.com/track/?data=${new Buffer(JSON.stringify(payload), 'utf8').toString('base64')}&ip=1`, + request(`https://api.mixpanel.com/track/?data=${new Buffer(JSON.stringify(payload), 'utf8').toString('base64')}&ip=1`, () => { /* Analytics failed. Do nothing. */ }); -} + } -if (require.main === module) { - module.exports('Install'); + if (require.main === module) { + module.exports('Install'); + } }