Skip to content

Commit e17fa6d

Browse files
committed
mixpanel
1 parent 9091976 commit e17fa6d

6 files changed

Lines changed: 105 additions & 23 deletions

File tree

browser/lib/clientKey.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

browser/main/Main.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import _ from 'lodash'
1212
import ConfigManager from 'browser/main/lib/ConfigManager'
1313
import modal from 'browser/main/lib/modal'
1414
import InitModal from 'browser/main/modals/InitModal'
15+
import mixpanel from 'browser/main/lib/mixpanel'
16+
17+
function focused () {
18+
mixpanel.track('MAIN_FOCUSED')
19+
}
1520

1621
class Main extends React.Component {
1722
constructor (props) {
@@ -49,6 +54,12 @@ class Main extends React.Component {
4954
modal.open(InitModal)
5055
}
5156
})
57+
58+
window.addEventListener('focus', focused)
59+
}
60+
61+
componentWillUnmount () {
62+
window.removeEventListener('focus', focused)
5263
}
5364

5465
handleLeftSlideMouseDown (e) {

browser/main/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require('!!style!css!stylus?sourceMap!./global.styl')
77
import { Router, Route, IndexRoute, IndexRedirect, hashHistory } from 'react-router'
88
import { syncHistoryWithStore } from 'react-router-redux'
99
require('./lib/ipcClient')
10+
1011
const electron = require('electron')
1112
const ipc = electron.ipcRenderer
1213

browser/main/lib/mixpanel.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
const _ = require('lodash')
2+
const keygen = require('browser/lib/keygen')
3+
const Mixpanel = require('mixpanel')
4+
const mixpanel = Mixpanel.init('7a0aca437d72dfd07cbcbf58d3b61f27', {key: 'fde4fd23f4d550f1b646bcd7d4374b1f'})
5+
const moment = require('moment')
6+
7+
function _getClientKey () {
8+
let clientKey = localStorage.getItem('clientKey')
9+
if (!_.isString(clientKey) || clientKey.length !== 40) {
10+
clientKey = keygen(20)
11+
_setClientKey(clientKey)
12+
}
13+
14+
return clientKey
15+
}
16+
17+
function _setClientKey (newKey) {
18+
localStorage.setItem('clientKey', newKey)
19+
}
20+
21+
function _fetch () {
22+
let events
23+
try {
24+
events = JSON.parse(localStorage.getItem('events'))
25+
if (!_.isArray(events)) throw new Error('events is not an array.')
26+
} catch (err) {
27+
console.warn(err)
28+
events = []
29+
localStorage.setItem('events', JSON.stringify(events))
30+
console.info('Events cache initialzed')
31+
}
32+
return events
33+
}
34+
35+
function _keep (name, properties) {
36+
let events = _fetch()
37+
properties.time = new Date()
38+
events.push({
39+
name,
40+
properties
41+
})
42+
localStorage.setItem('events', JSON.stringify(events))
43+
}
44+
45+
function _flush () {
46+
let events = _fetch()
47+
let spliced = events.splice(0, 50)
48+
localStorage.setItem('events', JSON.stringify(events))
49+
50+
if (spliced.length > 0) {
51+
let parsedEvents = spliced
52+
.filter((event) => {
53+
if (!_.isObject(event)) return false
54+
if (!_.isString(event.name)) return false
55+
if (!_.isObject(event.properties)) return false
56+
if (!moment(event.properties.time).isValid()) return false
57+
if (new Date() - moment(event.properties.time).toDate() > 1000 * 3600 * 24 * 3) return false
58+
return true
59+
})
60+
.map((event) => {
61+
return {
62+
event: event.name,
63+
properties: event.properties
64+
}
65+
})
66+
67+
mixpanel.import_batch(parsedEvents, {}, (errs) => {
68+
if (errs.length > 0) {
69+
let events = _fetch()
70+
events = events.concat(spliced)
71+
localStorage.setItem('events', JSON.stringify(events))
72+
}
73+
console.log('batched ', errs.length)
74+
_flush()
75+
})
76+
}
77+
}
78+
79+
setInterval(_flush, 1000 * 60 * 10)
80+
81+
function track (name, properties) {
82+
properties = Object.assign({}, properties, {
83+
distinct_id: _getClientKey()
84+
})
85+
_keep(name, properties)
86+
}
87+
88+
module.exports = {
89+
_mp: mixpanel,
90+
track
91+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"markdown-it-emoji": "^1.1.1",
5656
"markdown-it-footnote": "^3.0.0",
5757
"md5": "^2.0.0",
58+
"mixpanel": "^0.4.1",
5859
"moment": "^2.10.3",
5960
"node-ipc": "^8.1.0",
6061
"sander": "^0.5.1",

webpack-skeleton.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var config = {
4242
'markdown-it-checkbox',
4343
'season',
4444
'devtron',
45+
'mixpanel',
4546
{
4647
react: 'var React',
4748
'react-dom': 'var ReactDOM',

0 commit comments

Comments
 (0)