|
| 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 | +} |
0 commit comments