-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone-tracker.coffee
132 lines (103 loc) · 4.01 KB
/
backbone-tracker.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#### Mixes declarative mixpanel tracking into
#### Backbone.View
####
#### If events hash is:
####
#### events:
#### 'click a' : 'click link'
####
#### ---- Track it with a string ----
####
#### track:
#### 'click' : 'Clicked Link'
####
#### ---- An object ----
####
#### track:
#### 'click' :
#### name: 'Clicked Link'
#### data: {linkName: 'I Like Turtles'}
####
#### ---- A function ----
####
#### track:
#### 'click' :
#### name: 'Clicked Link'
#### data: (event) -> {
#### linkHref: event.target.attr('href')
#### time: new Date()
#### }
####
#### (note, function's `this` is the view, not event
#### target element)
BackboneTracker = ((Backbone, config) ->
throw 'No config parameter' unless config?
throw 'Config needs trackEvent function' unless config.trackEvent?
oldDelegateEvents = Backbone.View.prototype.delegateEvents
delegateEventSplitter = /^(\S+)\s*(.*)$/
toReturn = _.extend Backbone.View.prototype,
delegateEvents: (events) ->
if (!(events || (events = _.result(this, 'events'))))
return this
@undelegateEvents()
_.each events, (val, key) =>
method = events[key]
if (!_.isFunction(method))
method = this[events[key]]
if (!method) then return
match = key.match(delegateEventSplitter)
eventName = match[1]
selector = match[2]
method = _.bind(method, this)
eventName += '.delegateEvents' + @cid
if (selector == '')
if @track? and _.has(@track, key)
@$el.on eventName, (e) =>
result = method.apply(this, arguments)
_trackOnViewEvent.call(this, key, e)
return result
else
@$el.on(eventName, method)
else
if @track? and _.has(@track, key)
@$el.on eventName, selector, (e) =>
result = method.apply(this, arguments)
_trackOnViewEvent.call(this, key, e)
return result
else
@$el.on(eventName, selector, method)
@bindKeyboardEvents() if @bindKeyboardEvents?
return this
## Public wrapper for helper, attached to
## Backbone.View. Required an eventName. Optionally
## takes eventData
trackEvent: (eventName, eventData) ->
_sendTrackEvent(eventName, eventData)
#### Helpers ####
## When a view's event is triggered, and there is a
## corresponding entry in track, 'track' the event with
## the given configuration. Value can be a string,
## or an object with name and data property. Data can be
## an object, or function returning an object.
_trackOnViewEvent = (key, e) ->
# make sure defined
val = @track[key]
if _.isString(val)
_sendTrackEvent(@track[key])
else if _.isObject(val)
return if !(val.name? and val.data?)
if _.isFunction(val.data)
_sendTrackEvent(val.name, val.data.call(this,e))
else if _.isObject(val.data)
_sendTrackEvent(val.name, val.data)
## Broadcast the track event through chrome message
## passing, where background-script publishes it to
## mixpanel. Requires an event `name`. Optionally takes an
## event `data` object or a function that makes one.
_sendTrackEvent = (eventName, eventData={}) ->
config.trackEvent(eventName, eventData)
)
if ( typeof define == "function" && define.amd )
define "backbone-tracker", [], -> BackboneTracker
if module?.exports?
module.exports = BackboneTracker