2014-07-21 06:33:10 -04:00
|
|
|
define [
|
|
|
|
"base"
|
|
|
|
], (App) ->
|
2016-07-27 10:53:04 -04:00
|
|
|
CACHE_KEY = "countlyEvents"
|
|
|
|
|
|
|
|
_getEventCache = () ->
|
|
|
|
eventCache = window.localStorage.getItem CACHE_KEY
|
|
|
|
|
|
|
|
# Initialize as an empy object if the event cache is still empty.
|
|
|
|
if !eventCache?
|
|
|
|
window.localStorage.setItem(CACHE_KEY, "{}")
|
|
|
|
eventCache = window.localStorage.getItem CACHE_KEY
|
|
|
|
|
|
|
|
return JSON.parse eventCache
|
|
|
|
|
|
|
|
_eventInCache = (key) ->
|
|
|
|
curCache = _getEventCache()
|
|
|
|
|
|
|
|
if (curCache.hasOwnProperty key)
|
|
|
|
_getEventCache()[key]
|
|
|
|
else
|
|
|
|
false
|
|
|
|
|
|
|
|
_addEventToCache = (key) ->
|
|
|
|
curCache = _getEventCache()
|
|
|
|
curCache[key] = true
|
|
|
|
curCacheAsStr = JSON.stringify curCache
|
|
|
|
window.localStorage.setItem CACHE_KEY, curCacheAsStr
|
2014-07-21 06:33:10 -04:00
|
|
|
|
|
|
|
App.factory "event_tracking", ->
|
2016-07-27 10:53:04 -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
|
|
|
|
|
|
|
sendCountly: (key, segmentation) ->
|
|
|
|
eventData = { key }
|
2016-07-06 09:24:50 -04:00
|
|
|
eventData.segmentation = segmentation if segmentation?
|
2016-07-27 10:53:04 -04:00
|
|
|
Countly?.q.push([ "add_event", eventData ])
|
2016-07-08 05:06:46 -04:00
|
|
|
|
|
|
|
sendCountlySampled: (key, segmentation) ->
|
|
|
|
@sendCountly key, segmentation if Math.random() < .01
|
2016-07-27 10:53:04 -04:00
|
|
|
|
|
|
|
sendCountlyOnce: (key, segmentation) ->
|
|
|
|
if ! _eventInCache(key)
|
|
|
|
_addEventToCache(key)
|
|
|
|
@sendCountly key, segmentation
|
2014-07-21 06:33:10 -04:00
|
|
|
}
|
|
|
|
|
2016-07-06 07:26:21 -04:00
|
|
|
# App.directive "countlyTrack", () ->
|
|
|
|
# return {
|
|
|
|
# restrict: "A"
|
|
|
|
# scope: false,
|
|
|
|
# link: (scope, el, attrs) ->
|
|
|
|
# eventKey = attrs.countlyTrack
|
|
|
|
# if (eventKey?)
|
|
|
|
# el.on "click", () ->
|
|
|
|
# console.log eventKey
|
|
|
|
# }
|
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)
|