2014-07-21 10:33:10 +00:00
|
|
|
define [
|
2018-01-24 15:15:18 +00:00
|
|
|
"moment"
|
2014-07-21 10:33:10 +00:00
|
|
|
"base"
|
2016-07-27 15:53:44 +00:00
|
|
|
"modules/localStorage"
|
2018-01-24 15:15:18 +00:00
|
|
|
|
|
|
|
], (moment, App) ->
|
2016-08-10 16:28:13 +00:00
|
|
|
CACHE_KEY = "mbEvents"
|
|
|
|
|
2018-01-24 16:03:43 +00:00
|
|
|
# keep track of how many heartbeats we've sent so we can calculate how
|
|
|
|
# long wait until the next one
|
|
|
|
heartbeatsSent = 0
|
2018-01-24 15:15:18 +00:00
|
|
|
nextHeartbeat = new Date()
|
|
|
|
|
2016-08-10 15:42:56 +00:00
|
|
|
send = (category, action, attributes = {})->
|
|
|
|
ga('send', 'event', category, action)
|
|
|
|
event_name = "#{action}-#{category}"
|
|
|
|
Intercom?("trackEvent", event_name, attributes)
|
2018-01-22 15:00:56 +00:00
|
|
|
|
2016-08-10 16:28:13 +00:00
|
|
|
App.factory "event_tracking", ($http, localStorage) ->
|
2018-01-22 15:00:56 +00:00
|
|
|
_getEventCache = () ->
|
2016-07-27 15:53:44 +00:00
|
|
|
eventCache = localStorage CACHE_KEY
|
2016-07-27 14:53:04 +00:00
|
|
|
|
2016-07-27 15:53:44 +00:00
|
|
|
# Initialize as an empy object if the event cache is still empty.
|
|
|
|
if !eventCache?
|
|
|
|
eventCache = {}
|
2018-01-22 15:00:56 +00:00
|
|
|
localStorage CACHE_KEY, eventCache
|
2016-07-27 14:53:04 +00:00
|
|
|
|
2016-07-27 15:53:44 +00:00
|
|
|
return eventCache
|
2016-07-27 15:17:16 +00:00
|
|
|
|
2016-07-27 15:53:44 +00:00
|
|
|
_eventInCache = (key) ->
|
|
|
|
curCache = _getEventCache()
|
2016-07-27 16:08:15 +00:00
|
|
|
curCache[key] || false
|
2016-07-27 14:53:04 +00:00
|
|
|
|
2016-07-27 15:53:44 +00:00
|
|
|
_addEventToCache = (key) ->
|
|
|
|
curCache = _getEventCache()
|
|
|
|
curCache[key] = true
|
2016-07-27 14:53:04 +00:00
|
|
|
|
2016-07-27 15:53:44 +00:00
|
|
|
localStorage CACHE_KEY, curCache
|
2016-07-27 15:17:16 +00:00
|
|
|
|
2018-02-05 13:48:52 +00:00
|
|
|
_sendEditingSessionHeartbeat = () ->
|
2018-01-24 17:20:30 +00:00
|
|
|
$http({
|
|
|
|
url: "/editingSession/#{window.project_id}",
|
|
|
|
method: "PUT",
|
|
|
|
headers: {
|
|
|
|
"X-CSRF-Token": window.csrfToken
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2014-07-21 10:33:10 +00:00
|
|
|
return {
|
|
|
|
send: (category, action, label, value)->
|
|
|
|
ga('send', 'event', category, action, label, value)
|
2016-07-06 11:26:21 +00:00
|
|
|
|
2018-01-24 15:15:18 +00:00
|
|
|
|
2018-02-05 13:48:52 +00:00
|
|
|
editingSessionHeartbeat: () ->
|
2018-01-24 15:15:18 +00:00
|
|
|
return unless nextHeartbeat <= new Date()
|
|
|
|
|
2018-02-05 13:48:52 +00:00
|
|
|
_sendEditingSessionHeartbeat()
|
2018-01-24 15:15:18 +00:00
|
|
|
|
2018-01-24 16:03:43 +00:00
|
|
|
heartbeatsSent++
|
2018-01-24 15:15:18 +00:00
|
|
|
|
2018-01-24 16:03:43 +00:00
|
|
|
# send two first heartbeats at 0 and 30s then increase the backoff time
|
|
|
|
# 1min per call until we reach 5 min
|
|
|
|
backoffSecs = if heartbeatsSent <= 2
|
|
|
|
30
|
|
|
|
else if heartbeatsSent <= 6
|
|
|
|
(heartbeatsSent - 2) * 60
|
|
|
|
else
|
|
|
|
300
|
2018-01-24 15:15:18 +00:00
|
|
|
|
|
|
|
nextHeartbeat = moment().add(backoffSecs, 'seconds').toDate()
|
|
|
|
|
2016-08-10 16:28:13 +00:00
|
|
|
sendMB: (key, segmentation = {}) ->
|
|
|
|
$http {
|
|
|
|
url: "/event/#{key}",
|
2016-08-10 15:42:56 +00:00
|
|
|
method: "POST",
|
2016-08-10 16:28:13 +00:00
|
|
|
data: segmentation
|
2016-08-10 15:42:56 +00:00
|
|
|
headers: {
|
|
|
|
"X-CSRF-Token": window.csrfToken
|
|
|
|
}
|
|
|
|
}
|
2016-08-10 16:28:13 +00:00
|
|
|
|
|
|
|
sendMBSampled: (key, segmentation) ->
|
2018-01-22 15:00:56 +00:00
|
|
|
@sendMB key, segmentation if Math.random() < .01
|
2016-08-10 16:28:13 +00:00
|
|
|
|
|
|
|
sendMBOnce: (key, segmentation) ->
|
|
|
|
if ! _eventInCache(key)
|
|
|
|
_addEventToCache(key)
|
|
|
|
@sendMB key, segmentation
|
2014-07-21 10:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#header
|
|
|
|
$('.navbar a').on "click", (e)->
|
|
|
|
href = $(e.target).attr("href")
|
|
|
|
if href?
|
|
|
|
ga('send', 'event', 'navigation', 'top menu bar', href)
|