Skip to content

Commit

Permalink
[Desktop] Add Mixpanel analytics for background process
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasGaubert committed Jun 29, 2016
1 parent 2b239cb commit 6d91c67
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
28 changes: 28 additions & 0 deletions desktop/app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const BrowserWindow = electron.BrowserWindow
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow

// Analytics
var mixpanel = require('mixpanel').init('6cae86bf1da092b800b30b27689bd665')

// Networking
var bodyParser = require('body-parser')
var web = require('express')()
Expand Down Expand Up @@ -50,9 +53,11 @@ function createWindow () {
if (!conf.has('firstRun') || conf.get('firstRun')) {
log.info('Opening onboarding window...')
mainWindow.loadURL('file://' + __dirname + '/welcome.html')
mixpanel.track('first-run')
} else {
log.info('Opening main window...')
mainWindow.loadURL('file://' + __dirname + '/index.html')
mixpanel.track('repeat-run')
}

// Emitted when the window is closed.
Expand Down Expand Up @@ -102,6 +107,7 @@ app.on('will-quit', function () {
message: 'Server is shutting down...'
}
}))
mixpanel.track('quitting')
log.info('Quitting Zephyr v' + app.getVersion())
log.info('------------------------------------')
})
Expand All @@ -112,28 +118,33 @@ function startServer() {
web.post('/api/notification', function(req, res) {
res.setHeader('Content-Type', 'application/json')
res.send(JSON.stringify(handleNotification(JSON.stringify(req.body))))
mixpanel.track('api-http-notification', req.body.payload)
})

web.get('/api/version', function(req, res) {
res.setHeader('Content-Type', 'application/json')
res.send(JSON.stringify(handleVersionRequest(JSON.stringify(req.body))))
mixpanel.track('api-http-version', req.body.payload)
})

web.get('*', function(req, res) {
res.status(404).sendFile(__dirname + '/404.html')
mixpanel.track('api-http-404', req.body.payload)
})

io.on('connection', function(socket) {
socket.on('notification', function(msg) {
var n = JSON.parse(msg)
io.emit(n.metadata.from, JSON.stringify(handleNotification(msg)))
mixpanel.track('api-ws-notification', n.payload)
})

socket.on('version', function(msg) {
var v = JSON.parse(msg)
io.emit(v.metadata.from, JSON.stringify(handleVersionRequest(msg)))

broadcastPing()
mixpanel.track('api-ws-version', v.payload)
})

socket.on('broadcast', function(msg) {
Expand All @@ -152,12 +163,14 @@ function startServer() {
name: b.metadata.from
}
}))
mixpanel.track('api-ws-pong')
}
})

socket.on('disconnect', function() {
if (!quitting) {
log.info('Client disconnected, checking on other clients...')
mixpanel.track('api-ws-disconnect')
broadcastPing()
}
})
Expand All @@ -177,10 +190,16 @@ function startOverlay() {
log.info('Overlay stdout: ' + stdout)
log.info('Overlay stderr: ' + stderr)
broadcastOverlayNotRunning(error)
mixpanel.track('overlay-error', {
error: error
})
})
} else {
log.info('Overlay not running! (requires win32)')
broadcastOverlayNotRunning('Windows is required.')
mixpanel.track('overlay-error-win32', {
platform: process.platform
})
}
}

Expand Down Expand Up @@ -288,6 +307,7 @@ function setupAutoUpdater() {
},
payload: {}
}))
mixpanel.track('update-available')
})

autoUpdater.addListener("update-downloaded", function(event, releaseNotes, releaseName, releaseDate, updateURL) {
Expand All @@ -305,6 +325,9 @@ function setupAutoUpdater() {
changelog: releaseNotes
}
}))
mixpanel.track('update-downloaded', {
releaseName: releaseName
})
})

autoUpdater.addListener("error", function(err) {
Expand All @@ -320,6 +343,9 @@ function setupAutoUpdater() {
error: err
}
}))
mixpanel.track('update-error', {
error: error
})
})

autoUpdater.addListener("checking-for-update", function(event) {
Expand All @@ -333,6 +359,7 @@ function setupAutoUpdater() {
},
payload: {}
}))
mixpanel.track('update-checking')
})

autoUpdater.addListener("update-not-available", function(event) {
Expand All @@ -346,6 +373,7 @@ function setupAutoUpdater() {
},
payload: {}
}))
mixpanel.track('update-not-available')
})

const feedURL = 'https://zephyr-updates.herokuapp.com/update/' + process.platform + '/' + app.getVersion()
Expand Down
1 change: 1 addition & 0 deletions desktop/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"electron-log": "^1.0.14",
"express": "^4.10.2",
"markdown-it": "^7.0.0",
"mixpanel": "^0.4.0",
"socket.io": "^1.4.6"
},
"devDependencies": {
Expand Down

0 comments on commit 6d91c67

Please sign in to comment.