-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjquery.analyticseventtracking.js
129 lines (123 loc) · 4.71 KB
/
jquery.analyticseventtracking.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
/*jslint browser: true, undef: true, nomen: true, eqeqeq: true, bitwise: false,
newcap: true, immed: true, white: false, plusplus: false */
/*global jQuery, console, _gaq */
/**
* @author Joakim Westerlund
* @author Sven Strittmatter <[email protected]>
* @version 1.0 (2011-05-21)
* @link https://github.com/jorkas/jquery-analyticseventtracking-plugin
*/
(function($) {
var globalOptions = {
category: "jaet-report-category",
action: "jaet-report-action",
label: "jaet-report-label",
value: "jaet-report-value"
},
methods = {
init: function(settings) {
$(document).on('click', this.selector, function(event) {
if (settings.delayed === true && $(this).attr("href")) {
event.preventDefault();
}
methods.trackEvent.call($(this), settings);
});
},
getData: function(name, elm, defaultData) {
var $elm = $(elm), data;
data = $elm.data(name) || $elm.attr("data-" + name);
return data || defaultData;
},
getEventValue: function(value, elm) {
if ($.isFunction(value)) {
return value.call(elm);
} else {
return value;
}
},
reportCategory: function() {
return methods.getData(globalOptions.category, this, "DefaultCategory");
},
reportAction: function() {
return methods.getData(globalOptions.action, this, "DefaultAction");
},
reportLabel: function() {
return methods.getData(globalOptions.label, this, "DefaultLabel");
},
reportValue: function() {
var value = methods.getData(globalOptions.value, this, 0);
return parseInt(value, 10) || 0;
},
trackEvent: function() {
var settings = arguments[0],
$this = $(this),
tracking = {
eventCategory: methods.getEventValue(settings.category, this),
eventAction: methods.getEventValue(settings.action, this),
eventLabel: methods.getEventValue(settings.label, this),
eventValue: methods.getEventValue(settings.value, this)
},
errorMsg;
if (typeof(_gaq) !== "undefined") {
_gaq.push([
settings.trackerName,
tracking.eventCategory,
tracking.eventAction,
tracking.eventLabel,
tracking.eventValue
]);
} else {
errorMsg = "Google Analaytics _gaq varible not found! Please set up Google Analytics properly.";
if (console && console.error && typeof console.error === "function") {
console.error(errorMsg);
} else {
throw errorMsg;
}
}
if (settings.delayed === true && $this.attr("href")) {
setTimeout(function() {
document.location = $this.attr("href");
}, 100);
}
}
};
$.analyticsEventTracking = function(options) {
globalOptions = $.extend(globalOptions, options || {});
};
/**
* Default plugin options.
*
* @var {Object}
*/
$.analyticsEventTracking.defaultOptions = {
category: methods.reportCategory,
action: methods.reportAction,
label: methods.reportLabel,
value: methods.reportValue,
trackerName: '_trackEvent', //Default to Analytics default
delayed: true //Delay link clicks for some miliseconds
};
/**
* Binds the elements of the wrapped set to click events for tracking.
*
* @param {Object} Optional tracking options.
* @return {jQuery}
*/
$.fn.analyticsEventTracking = function(options) {
var settings = $.extend({},$.analyticsEventTracking.defaultOptions, options || {});
methods.init.call(this, settings);
return this;
};
/**
* Triggers event tracking for all elements in the wrapped set
* @param {Object} Optional tracking options.
* @return {jQuery}
*/
$.fn.analyticsTrackEvent = function(options) {
var settings = $.extend({},$.analyticsEventTracking.defaultOptions, options || {});
this.each(function(){
methods.trackEvent.call($(this), settings);
});
return this;
};
})(jQuery);