2014-07-21 06:33:10 -04:00
|
|
|
define [
|
|
|
|
"base"
|
2016-07-27 11:53:44 -04:00
|
|
|
"modules/localStorage"
|
2014-07-21 06:33:10 -04:00
|
|
|
], (App) ->
|
2016-08-10 12:28:13 -04:00
|
|
|
CACHE_KEY = "mbEvents"
|
2018-01-22 10:10:52 -05:00
|
|
|
EDIT_SESSION_HEARTBEAT_INTERVAL = 5 * 60 * 1000 # 5min
|
2016-08-10 12:28:13 -04:00
|
|
|
|
2016-08-10 11:42:56 -04:00
|
|
|
send = (category, action, attributes = {})->
|
|
|
|
ga('send', 'event', category, action)
|
|
|
|
event_name = "#{action}-#{category}"
|
|
|
|
Intercom?("trackEvent", event_name, attributes)
|
2018-01-22 10:00:56 -05:00
|
|
|
|
2016-08-10 12:28:13 -04:00
|
|
|
App.factory "event_tracking", ($http, localStorage) ->
|
2018-01-22 10:00:56 -05:00
|
|
|
_getEventCache = () ->
|
2016-07-27 11:53:44 -04:00
|
|
|
eventCache = localStorage CACHE_KEY
|
2016-07-27 10:53:04 -04:00
|
|
|
|
2016-07-27 11:53:44 -04:00
|
|
|
# Initialize as an empy object if the event cache is still empty.
|
|
|
|
if !eventCache?
|
|
|
|
eventCache = {}
|
2018-01-22 10:00:56 -05:00
|
|
|
localStorage CACHE_KEY, eventCache
|
2016-07-27 10:53:04 -04:00
|
|
|
|
2016-07-27 11:53:44 -04:00
|
|
|
return eventCache
|
2016-07-27 11:17:16 -04:00
|
|
|
|
2016-07-27 11:53:44 -04:00
|
|
|
_eventInCache = (key) ->
|
|
|
|
curCache = _getEventCache()
|
2016-07-27 12:08:15 -04:00
|
|
|
curCache[key] || false
|
2016-07-27 10:53:04 -04:00
|
|
|
|
2016-07-27 11:53:44 -04:00
|
|
|
_addEventToCache = (key) ->
|
|
|
|
curCache = _getEventCache()
|
|
|
|
curCache[key] = true
|
2016-07-27 10:53:04 -04:00
|
|
|
|
2016-07-27 11:53:44 -04:00
|
|
|
localStorage CACHE_KEY, curCache
|
2016-07-27 11:17:16 -04:00
|
|
|
|
2014-07-21 06:33:10 -04:00
|
|
|
return {
|
|
|
|
send: (category, action, label, value)->
|
|
|
|
ga('send', 'event', category, action, label, value)
|
2016-07-06 07:26:21 -04:00
|
|
|
|
2018-01-23 10:58:59 -05:00
|
|
|
editingSessionHeartbeat: _.throttle( (segmentation = {}) ->
|
2018-01-22 10:10:52 -05:00
|
|
|
$http({
|
2018-01-23 10:58:59 -05:00
|
|
|
url: "/editingSession/#{window.project_id}",
|
2018-01-22 10:10:52 -05:00
|
|
|
method: "PUT",
|
|
|
|
data: segmentation,
|
|
|
|
headers: {
|
|
|
|
"X-CSRF-Token": window.csrfToken
|
|
|
|
}
|
|
|
|
})
|
2018-01-23 05:14:24 -05:00
|
|
|
, EDIT_SESSION_HEARTBEAT_INTERVAL, trailing: false)
|
2018-01-22 10:10:52 -05:00
|
|
|
|
|
|
|
|
2016-08-10 12:28:13 -04:00
|
|
|
sendMB: (key, segmentation = {}) ->
|
|
|
|
$http {
|
|
|
|
url: "/event/#{key}",
|
2016-08-10 11:42:56 -04:00
|
|
|
method: "POST",
|
2016-08-10 12:28:13 -04:00
|
|
|
data: segmentation
|
2016-08-10 11:42:56 -04:00
|
|
|
headers: {
|
|
|
|
"X-CSRF-Token": window.csrfToken
|
|
|
|
}
|
|
|
|
}
|
2016-08-10 12:28:13 -04:00
|
|
|
|
|
|
|
sendMBSampled: (key, segmentation) ->
|
2018-01-22 10:00:56 -05:00
|
|
|
@sendMB key, segmentation if Math.random() < .01
|
2016-08-10 12:28:13 -04:00
|
|
|
|
|
|
|
sendMBOnce: (key, segmentation) ->
|
|
|
|
if ! _eventInCache(key)
|
|
|
|
_addEventToCache(key)
|
|
|
|
@sendMB key, segmentation
|
2014-07-21 06:33:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#header
|
|
|
|
$('.navbar a').on "click", (e)->
|
|
|
|
href = $(e.target).attr("href")
|
|
|
|
if href?
|
|
|
|
ga('send', 'event', 'navigation', 'top menu bar', href)
|