Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 4 commits
  • 1 file changed
  • 0 commit comments
  • 3 contributors
Commits on Jun 30, 2011
Sven Strittmatter Added setup function to set global custom data attribute names. e771389
Commits on Jul 01, 2011
@Weltraumschaf Weltraumschaf 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().
b8d5a6b
@Weltraumschaf Weltraumschaf Added fallback with attr() to access data attribute id .data() does n…
…ot work.
d73933d
Commits on Sep 22, 2011
@jorkas Merge git://github.com/Weltraumschaf/jquery-analyticseventtracking-pl…
…ugin

* git://github.com/Weltraumschaf/jquery-analyticseventtracking-plugin:
  Added fallback with attr() to access data attribute id .data() does not work.
  Improved customoization possibilities of the plugin.
  Added setup function to set global custom data attribute names.

Conflicts:
	jquery.analyticseventtracking.js
11a8890
Showing with 114 additions and 63 deletions.
  1. +114 −63 jquery.analyticseventtracking.js
View
177 jquery.analyticseventtracking.js
@@ -1,81 +1,132 @@
+/*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 <weltraumschaf@gmail.com>
* @version 1.0 (2011-05-21)
+ * @link https://github.com/jorkas/jquery-analyticseventtracking-plugin
*/
-(function( $ ){
- 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")){
+(function($) {
+ var globalOptions = {
+ category: "jaet-report-category",
+ action: "jaet-report-action",
+ label: "jaet-report-label",
+ value: "jaet-report-value"
+ },
+ 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);
});
});
},
- getEventValue: function(value,elm){
- if($.isFunction(value)){
- return encodeString(value.call(elm));
- }else{
- return encodeString(value);
+ 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 $(this).data("jaet-report-category") ? $(this).data("jaet-report-category") : "DefaultCategory";
+ reportCategory: function() {
+ return methods.getData(globalOptions.category, this, "DefaultCategory");
},
- reportAction: function(){
- return $(this).data("jaet-report-action") ? $(this).data("jaet-report-action") : "DefaultAction";
+ reportAction: function() {
+ return methods.getData(globalOptions.action, this, "DefaultAction");
},
- reportLabel: function(){
- return $(this).data("jaet-report-label") ? $(this).data("jaet-report-label") : "";
+ reportLabel: function() {
+ return methods.getData(globalOptions.label, this, "DefaultLabel");
},
- reportValue: function(){
- return $(this).data("jaet-report-value") ? $(this).data("jaet-report-value") : 0;
+ reportValue: function() {
+ var value = methods.getData(globalOptions.value, this, 0);
+
+ return parseInt(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]);
- if(settings.delayed === true && $(elm).attr("href")){
- setTimeout(function(){
- document.location = $(elm).attr("href");
- },50);
+ 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");
+ }, 50);
+ }
}
- }
- };
+ };
- $.fn.analyticsEventTracking = function(options){
- var settings = {
- category: methods.reportCategory,
- action: methods.reportAction,
- label: methods.reportLabel,
- value: methods.reportValue,
+ $.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
- }, 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;
- }
- }
- };
-})( jQuery );
+ 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);

No commit comments for this range

Something went wrong with that request. Please try again.