overleaf/services/web/public/coffee/main/event.coffee

72 lines
1.6 KiB
CoffeeScript
Raw Normal View History

define [
"base"
2016-07-27 15:53:44 +00:00
"modules/localStorage"
], (App) ->
2016-08-10 16:28:13 +00:00
CACHE_KEY = "mbEvents"
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)
2016-08-10 16:28:13 +00:00
App.factory "event_tracking", ($http, localStorage) ->
2016-07-27 15:53:44 +00:00
_getEventCache = () ->
eventCache = localStorage CACHE_KEY
2016-07-27 15:53:44 +00:00
# Initialize as an empy object if the event cache is still empty.
if !eventCache?
eventCache = {}
localStorage CACHE_KEY, eventCache
2016-07-27 15:53:44 +00:00
return eventCache
2016-07-27 15:53:44 +00:00
_eventInCache = (key) ->
curCache = _getEventCache()
curCache[key] || false
2016-07-27 15:53:44 +00:00
_addEventToCache = (key) ->
curCache = _getEventCache()
curCache[key] = true
2016-07-27 15:53:44 +00:00
localStorage CACHE_KEY, curCache
return {
send: (category, action, label, value)->
ga('send', 'event', category, action, label, value)
2016-07-06 11:26:21 +00:00
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) ->
@sendMB key, segmentation if Math.random() < .01
sendMBOnce: (key, segmentation) ->
if ! _eventInCache(key)
_addEventToCache(key)
@sendMB key, segmentation
}
2016-07-06 11:26:21 +00:00
# App.directive "countlyTrack", () ->
# return {
# restrict: "A"
# scope: false,
# link: (scope, el, attrs) ->
# eventKey = attrs.countlyTrack
# if (eventKey?)
# el.on "click", () ->
# console.log eventKey
# }
#header
$('.navbar a').on "click", (e)->
href = $(e.target).attr("href")
if href?
ga('send', 'event', 'navigation', 'top menu bar', href)