-
Notifications
You must be signed in to change notification settings - Fork 12
/
jquery.building-blocks.googleanalytics.js
204 lines (176 loc) · 7.66 KB
/
jquery.building-blocks.googleanalytics.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*!
Google Analytics Event Tracking jQuery Plugin
Copyright (C) 2011 by Building Blocks UK
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Author: Robert Stevenson-Leggett
Version: 0.92
Handles event tracking through the use of data attributes
Version history:
0.1 - Initial version
0.2 - Support for direct calling
0.3 - Bug fixes
0.4 - Added comments and push to github
0.5 - Add gaTrackEvent
0.6 - Bug fixes
0.7 - Added non interactive option
0.8 - Bug fix
0.9 - Added more tests
0.91 - Bug fix
0.92 - Bug fix for useLabel
*/
(function ($) {
// Extend the jQuery object with the ga trackEvent function. Other functions can call this method, or
// it can be called directly from existing JS code to add tracking
// Can be used anywhere in your Javascript like so:
// $.ga.trackEvent({ category : 'Category', action : 'Action', label : 'Label', value : 0.0});
// $.ga.trackEvent({ category : 'Category', action: 'Action' });
//
// This also enables unit tests to overide this function for testing purposes.
$.extend({
ga: {
trackEvent: function(args) {
var defaultArgs = {
category : 'Unspecified',
action: 'Unspecified',
nonInteractive:false
};
args = $.extend(defaultArgs,args);
if (typeof ga !== 'undefined') {
ga('send', 'event', args.category, args.action, args.label, args.value, {'nonInteraction': args.nonInteractive});
} else if (typeof _gaq !== 'undefined') {
_gaq.push(['_trackEvent', args.category, args.action, args.label, args.value, args.nonInteractive]);
} else {
throw new ReferenceError("ga and _gaq are both undefined.");
}
}
}
});
var defaultOptions = {
//The category attribute
categoryAttribute: 'data-ga-category',
//The action attribute
actionAttribute: 'data-ga-action',
//The label attribute (could be changed to href when tracking file downloads)
labelAttribute: 'data-ga-label',
//The value attribute (must be integer)
valueAttribute: 'data-ga-value',
//An attribute to indicate whether the event is non-interactive (defaults to false)
noninteractiveAttribute: 'data-ga-noninteractive',
//Whether to look for the label
useLabel: false,
//Whether to look for a value
useValue: false,
//false = track as soon as the plugin loads, true = bind to an event
useEvent: false,
//The event to bind to if useEvent is true
event: 'click',
//A method to call to check whether or not we should call the tracking when the event is clicked
valid: function (elem, e) { return true; },
//Tracking complete
complete: function (elem, e) { },
//Category should always be set if using gaTrackEvent
category: 'Unspecified',
//Action should always be set if using gaTrackEvent
action: 'Unspecified',
//Label can be specified if using gaTrackEvent and useLabel == true
label: '',
//value can be specified if using gaTrackEvent and useValue == true
value: '',
//non-interactive - only used if using gaTrackEvent
nonInteractive: false
};
//
// gaTrackEvent adds unobtrusive tracking attributes itself
// So you can add tracking to links etc.
//
// This allows to do sitewide event tracking e.g. Document Downloads just
// by selecting the elements e.g.
//
//
// $('.track-download').gaTrackEvent({
// category:'Downloads', action:'PDF', useEvent:true, event:'click'
// });
//
$.fn.gaTrackEvent = function (options) {
options = $.extend({}, defaultOptions, options);
return this.each(function () {
var element = $(this);
element.attr(options.categoryAttribute, options.category);
element.attr(options.actionAttribute, options.action);
if (options.useLabel === true && options.label !== '') {
element.attr(options.labelAttribute, options.label);
}
if (options.useValue === true && options.value !== '') {
element.attr(options.valueAttribute, options.value);
}
if (options.nonInteractive === true) {
element.attr(options.noninteractiveAttribute, "true");
}
element.gaTrackEventUnobtrusive(options);
});
};
//Create the plugin
// gaTrackEventUnobtrusive expects you to add the data attributes either via server side code
// or direct into the mark up.
$.fn.gaTrackEventUnobtrusive = function (options) {
//Merge options
options = $.extend({}, defaultOptions, options);
//Keep the chain going
return this.each(function () {
var _this = $(this);
//Wrap the tracking so we can reuse it.
var callTrackEvent = function () {
//Retreive the info
var category = _this.attr(options.categoryAttribute);
var action = _this.attr(options.actionAttribute);
var label = _this.attr(options.labelAttribute);
var value = _this.attr(options.valueAttribute);
var nonInteractive = (_this.attr(options.noninteractiveAttribute) === 'true');
var args = {
category : category,
action : action,
nonInteractive : nonInteractive
};
if (options.useLabel && options.useValue) {
args.label = label;
args.value = value;
}
else if (options.useLabel) {
args.label = label;
}
$.ga.trackEvent(args);
};
//If we want to bind to an event, do it.
if (options.useEvent == true) {
//This is what happens when you actually click a button
var constructedFunction = function (e) {
//Check the callback function
if (options.valid(_this, e) === true) {
callTrackEvent();
options.complete(_this, e);
}
};
//E.g. if we are going to click on a link
_this.bind(options.event, constructedFunction);
}
else {
//Otherwise just track immediately (e.g. if we just came from a post-back)
callTrackEvent();
}
});
};
})(jQuery);