Permalink
Browse files

Improved customoization possibilities of the plugin.

- The names for the data attributes are changeable by global setup function $.analyticsEventTracking().
- Fixed $.analyticsEventTracking() that it returns the wrapped set.
- Introduced $.analyticsEventTracking.defaultOptions with default options fortracking.
- Introduced $.fn.analyticsTrackEvent() to track elements without any binding (for own custom bindings other than clik.
- Introduced jQuery style of setting options with $.extend().
  • Loading branch information...
1 parent e771389 commit b8d5a6b8708e56736152010803ca73409c51d0aa @Weltraumschaf Weltraumschaf committed Jul 1, 2011
Showing with 69 additions and 38 deletions.
  1. +69 −38 jquery.analyticseventtracking.js
@@ -1,6 +1,8 @@
/**
* @author Joakim Westerlund
+ * @author Sven Strittmatter <weltraumschaf@gmail.com>
* @version 1.0 (2011-05-21)
+ * @link https://github.com/jorkas/jquery-analyticseventtracking-plugin
*/
(function( $ ){
var globalOptions = {
@@ -9,17 +11,15 @@
label: "jaet-report-label",
value: "jaet-report-value"
};
- var encodeString = function(str){
- return encodeURI(str);
- };
var methods = {
init: function(settings){
this.each(function(){
$(this).click(function(event){
if(settings.delayed === true && $(this).attr("href")){
event.preventDefault();
}
- return methods.trackEvent.call($(this),settings,event);
+
+ methods.trackEvent.call($(this), settings);
});
});
},
@@ -43,49 +43,80 @@
return $(this).data(globalOptions.value) ? parseInt($(this).data(globalOptions.value), 10) : 0;
},
trackEvent: function(){
- var settings = arguments[0];
- var elm = $(this);
- var 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)
- };
- _gaq.push([settings.trackerName, tracking.eventCategory, tracking.eventAction, tracking.eventLabel, tracking.eventValue]);
+ var settings = arguments[0],
+ elm = $(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 && $(elm).attr("href")){
setTimeout(function(){
document.location = $(elm).attr("href");
},50);
}
}
};
-
+
$.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 = {
- 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
- }, errorMsg;
- if (options){
- settings = $.extend(settings, options);
- }
- if(typeof(_gaq) !== "undefined"){
- methods.init.call( this, settings );
- }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;
- }
- }
+ var settings = $.extend($.analyticsEventTracking.defaultOptions, options || {});
+
+ methods.init.call(this, settings);
+
+ return this;
};
-})( jQuery );
+
+ /**
+ * 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 );

0 comments on commit b8d5a6b

Please sign in to comment.