forked from abitgone/jQuery-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabitgone-gahelper.js
129 lines (109 loc) · 5.37 KB
/
abitgone-gahelper.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
/*
abitgone-gahelper.js
=======================
A suite of Google Analytics triggers to help non-developers use page- and event-tracking beyond the default
tracking offered by Google Analytics.
For information on how to use this plugin and for code examples, visit
http://abitgone.github.com/jQuery-Plugins/GA-Helper
License
-------
Author: Anthony Williams
Web: http://abitgone.github.com/jQuery-Plugins/Carousel
Copyright (c) 2012 Anthony Williams
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.
*/
!function (jQuery) {
//GaHelper Public Class Definition
var GAHelper = function(element, options) {
this.$element = $(element);
this.options = $.extend({}, $.fn.gaHelper.defaults, options);
}
GAHelper.prototype = {
triggerEvent: function(e) {
var pushData = ['_trackEvent', this.options.gahelperEventCategory, this.options.gahelperEvent];
var hasLabel = !(typeof this.options.gahelperEventLabel === "undefined"),
hasValue = !(typeof this.$element.attr("data-gahelper-event-value") === "undefined") &&
typeof(parseInt(this.$element.attr("data-gahelper-event-value")) === "number"),
hasNoninteraction = !(typeof this.options.gahelperEventNoninteraction === "undefined");
// Variables must be passed in a specific order - push a null if a value doesn hasn't been passed
if (hasLabel || hasValue || hasNoninteraction) {
if (hasLabel) {
pushData.push(this.options.gahelperEventLabel);
} else {
pushData.push(null); // Placeholder null for Event label, if hasValue or hasNoninteraction
}
if (hasValue || hasNoninteraction) {
if (hasValue) {
pushData.push(parseInt(this.$element.attr("data-gahelper-event-value")));
} else {
pushData.push(null); // Placeholder null for Event Value, if hasNoninteraction
}
if (hasNoninteraction) {
pushData.push(this.options.gahelperEventNoninteraction);
}
}
}
_gaq.push(pushData);
if (!typeof this.options.gahelperPreventdefault === "undefined") {
if (this.options.gahelperPreventdefault) {
e.preventDefault();
}
}
},
triggerUrl: function(e) {
var pushData = ['_trackPageview', this.options.gahelperUrl];
_gaq.push(pushData);
}
}
// GAHelper Plugin Definition
$.fn.gaHelper = function(option) {
return this.each(function () {
var $this = $(this),
data = $this.data('gaHelper'),
options = typeof option == 'object' && option;
if (!data) $this.data('gaHelper', (data = new GAHelper(this, options)));
if (typeof option === 'string' && typeof data[option] === 'function') data[option]();
});
}
$.fn.gaHelper.defaults = {
gaHelper: true
}
$.fn.gaHelper.Constructor = GAHelper;
// GAHelper Data-Api
$(function () {
$(document).on('click.gahelper.data-api', '[data-gahelper-event][data-gahelper-event-category], [data-gahelper-url]', function (e) {
var $this = $(this),
option = $this.data('gaHelper') ? 'gaHelper' : $this.data();
if (!(typeof _gaq === "array" || typeof _gaq === "object")) {
if (!!console) {
var errMsg = "Google Analytics tracking script does not appear to be loaded (_gaq is not an object nor an array).";
if (!!console.warn) {
console.warn(errMsg)
} else {
console.log("Warning: " + errMsg);
}
}
}
$this.gaHelper(option);
if ($this.is("[data-gahelper-event]")) {
$this.gaHelper("triggerEvent");
}
if ($this.is("[data-gahelper-url]")) {
$this.gaHelper("triggerUrl");
}
if ($this.is("[data-gahelper-preventdefault]")) {
e.preventDefault();
}
});
});
}(window.jQuery);