2018-11-05 05:06:39 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-19 05:02:56 -04:00
|
|
|
import moment from 'moment'
|
|
|
|
import App from '../base'
|
|
|
|
import '../modules/localStorage'
|
2021-08-11 04:50:38 -04:00
|
|
|
import { sendMB } from '../infrastructure/event-tracking'
|
2020-05-19 05:02:56 -04:00
|
|
|
const CACHE_KEY = 'mbEvents'
|
|
|
|
|
|
|
|
// keep track of how many heartbeats we've sent so we can calculate how
|
|
|
|
// long wait until the next one
|
|
|
|
let heartbeatsSent = 0
|
|
|
|
let nextHeartbeat = new Date()
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
App.factory('eventTracking', function ($http, localStorage) {
|
|
|
|
const _getEventCache = function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
let eventCache = localStorage(CACHE_KEY)
|
|
|
|
|
|
|
|
// Initialize as an empy object if the event cache is still empty.
|
|
|
|
if (eventCache == null) {
|
|
|
|
eventCache = {}
|
|
|
|
localStorage(CACHE_KEY, eventCache)
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
return eventCache
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
const _eventInCache = function (key) {
|
2020-05-19 05:02:56 -04:00
|
|
|
const curCache = _getEventCache()
|
|
|
|
return curCache[key] || false
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
const _addEventToCache = function (key) {
|
2020-05-19 05:02:56 -04:00
|
|
|
const curCache = _getEventCache()
|
|
|
|
curCache[key] = true
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
return localStorage(CACHE_KEY, curCache)
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2022-03-01 04:54:13 -05:00
|
|
|
const _sendEditingSessionHeartbeat = segmentation =>
|
2020-05-19 05:02:56 -04:00
|
|
|
$http({
|
|
|
|
url: `/editingSession/${window.project_id}`,
|
|
|
|
method: 'PUT',
|
2022-03-01 04:54:13 -05:00
|
|
|
data: { segmentation },
|
2020-05-19 05:02:56 -04:00
|
|
|
headers: {
|
2021-04-27 03:52:58 -04:00
|
|
|
'X-CSRF-Token': window.csrfToken,
|
|
|
|
},
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
return {
|
|
|
|
send(category, action, label, value) {
|
|
|
|
return ga('send', 'event', category, action, label, value)
|
|
|
|
},
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
sendGAOnce(category, action, label, value) {
|
|
|
|
if (!_eventInCache(action)) {
|
|
|
|
_addEventToCache(action)
|
|
|
|
return this.send(category, action, label, value)
|
|
|
|
}
|
|
|
|
},
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2022-03-01 04:54:13 -05:00
|
|
|
editingSessionHeartbeat(segmentation) {
|
|
|
|
sl_console.log('[Event] heartbeat trigger', segmentation)
|
2020-05-19 05:02:56 -04:00
|
|
|
if (!(nextHeartbeat <= new Date())) {
|
|
|
|
return
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2022-03-01 04:54:13 -05:00
|
|
|
sl_console.log('[Event] send heartbeat request', segmentation)
|
|
|
|
_sendEditingSessionHeartbeat(segmentation)
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
heartbeatsSent++
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
// send two first heartbeats at 0 and 30s then increase the backoff time
|
|
|
|
// 1min per call until we reach 5 min
|
|
|
|
const backoffSecs =
|
|
|
|
heartbeatsSent <= 2
|
|
|
|
? 30
|
|
|
|
: heartbeatsSent <= 6
|
2020-12-15 05:23:54 -05:00
|
|
|
? (heartbeatsSent - 2) * 60
|
|
|
|
: 300
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
return (nextHeartbeat = moment().add(backoffSecs, 'seconds').toDate())
|
2020-05-19 05:02:56 -04:00
|
|
|
},
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-08-11 04:50:38 -04:00
|
|
|
sendMB,
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-02-17 08:52:32 -05:00
|
|
|
sendMBSampled(key, segmentation, rate = 0.01) {
|
|
|
|
if (Math.random() < rate) {
|
2021-06-03 09:38:54 -04:00
|
|
|
this.sendMB(key, segmentation)
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
sendMBOnce(key, segmentation) {
|
|
|
|
if (!_eventInCache(key)) {
|
|
|
|
_addEventToCache(key)
|
2021-06-03 09:38:54 -04:00
|
|
|
this.sendMB(key, segmentation)
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
},
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
eventInCache(key) {
|
|
|
|
return _eventInCache(key)
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
export default $('.navbar a').on('click', function (e) {
|
2020-05-19 05:02:56 -04:00
|
|
|
const href = $(e.target).attr('href')
|
|
|
|
if (href != null) {
|
|
|
|
return ga('send', 'event', 'navigation', 'top menu bar', href)
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
})
|