forked from aterris/backbone.analytics
/
backbone.analytics.coffee
153 lines (135 loc) · 4.5 KB
/
backbone.analytics.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#### Backbone Analytics 0.2
#
# A small object to manage the integration of google analytics into
# a backbone application. It is meant to be simple and implementation
# agnostic. Depends on Underscore.js and Backbone.js
#
# Created by Andrew Terris [@aterris](http://twitter.com/aterris)
#
# Hosted on [github](https://github.com/aterris/backbone.analytics)
#
#### Downloads
#
# * [Development](https://raw.github.com/aterris/backbone.analytics/master/backbone.analytics.js)
# * [Production](https://raw.github.com/aterris/backbone.analytics/master/backbone.analytics.min.js)
#
#### Example
#
# <script src="/js/underscore.js"></script>
# <script src="/js/backbone.js"></script>
# <script src="/js/backbone.analytics.js"></script>
# <script>
# window.App = window.App || {};
# window.App.ga = new Backbone.Analytics({code: 'UA-12345678-9'});
# </script>
#
#### Usage
#
# This object is meant to be simple and flexible so you can use it however
# works best for your needs and implementation. I prefer to extend the object
# and use it to write methods that contain my specific analytics integrations.
# However, you can also use it as is.
#
#### Option Defaults
#
# * setAccount: true
# * initialPageview: true
# * trackNavigate: true
# * loadScript: true
# * debug: false
#
#### Syntactic Sugar
#
# Backbone.Analytics provides a few convience methods that wrap around common
# google analytics objects and interactions including `queue`, `push`,
# `setAccount`, `trackPageview`, `trackEvent`, and `setCustomVar`
#
#### Debug Mode
#
# Enabling debug mode will include the debug version of the google analytics
# script instead of the standard script. This will log debug information
# to the console. Enable debug mode by passing `debug: true`
#
#### Extending Backbone.Analytics
#
# Extending Backbone.Analytics can help you manage and maintain your domain specific
# analytics interactions in a single location.
#
class window.Backbone.Analytics
constructor: (options={}) ->
@code = options.code
@debug = options.debug
@setAccount() unless options.setAccount == false
@loadScript() unless options.loadScript == false
@initialize.apply(this, arguments)
@trackPageview() unless options.initialPageview == false
@trackNavigate() unless options.trackNavigate == false
##### Initialize method to be overridden
initialize: =>
##### Load Script
#
# Load the google analytics script via the standard asynchronous pattern
#
# By default creating a new analytics object will automatically load the
# google analytics script via the asynchronous pattern. Passing `loadScript: false`
# will stop this behavior if you already loaded it yourself.
#
loadScript: =>
ga = document.createElement('script')
ga.type = 'text/javascript'
ga.async = true
ga.src = @protocol() + @script()
s = document.getElementsByTagName('script')[0]
s.parentNode.insertBefore(ga, s)
##### Script source protocol
protocol: =>
if document.location.protocol == 'https:'
'https://ssl'
else
'http://www'
##### Script source url
script: =>
if @debug == true
'.google-analytics.com/u/ga_debug.js'
else
'.google-analytics.com/ga.js'
##### Global _gaq object
queue: =>
window._gaq ||= []
##### Push queue
push: (args) =>
@queue().push(args)
##### Set Account
#
# Trigger setAccount or throw an error if we do not have a tracking code
#
# By default creating a new analytics object will automatically trigger a set
# account event using the `code` passed to the create method. Passing
# `setAccount: false` will stop this behavior.
#
setAccount: =>
if @code?
@push(['_setAccount', @code])
else
throw new Error("Cannot Set Google Analytics Account: No Tracking Code Provided")
##### Track Pageview
trackPageview: (fragment) =>
command = ['_trackPageview']
command.push(fragment) if fragment?
@push(command)
##### Track Event
trackEvent: (args...) =>
@push(['_trackEvent'].concat(args))
##### Track Social
trackSocial: (args...) =>
@push(['_trackSocial'].concat(args))
##### Set Custom Variable
setCustomVar: (args...) =>
@push(['_setCustomVar'].concat(args))
##### Track Backbone Navigation
trackNavigate: =>
trackPageview = @trackPageview
navigate = window.Backbone.History.prototype.navigate
window.Backbone.History.prototype.navigate = (fragment, options) ->
trackPageview(fragment)
navigate.apply(this, arguments)