jQuery plug-in to provide custom analytics. For those of us who can not use Google Analytics at work or just want to dork with something else.

jquery-analytics.min.js 1.9KB

    String.prototype.startsWith=function(a){return this.indexOf(a)==0};String.prototype.endsWith=function(a){return this.lastIndexOf(a)==this.length-a.length};(function(e){var g=0;var c={attributes:[],assignTo:["a","input[type='submit']"],captureOnce:false,client:null,id:"id",exclude:".analytics-exclude",url:null};function a(i){var h=e(i).is(c.exclude);var j=e(i).parent();if(j&&j.prop("tagName")!==undefined){h=h||j.is(c.excluded);if(!h){h=h||a(j)}}return h}function b(j){var h=[];var i=e(j).prop("tagName");if(i!=undefined){var l=e(j).parent();if(l&&l.prop("tagName")!==undefined){e.each(b(e(j).parent()),function(m,n){h.push(n)})}if(i=="HTML"||i=="BODY"){h.push(i)}else{var k=e(j).analyticsUniqueId().attr("id");h.push(i+'[id="'+k+'"]')}}return h}function d(h){b(h)}function f(n){$this=e(this);if(c.url&&!$this.is(".analytics-captured")&&!a($this)){var h=b($this);if(h&&h.length>0){h=h.join(" ");var m=null;if($this.attr("href")){n.preventDefault();m=$this.attr("href")}var l={};if(c.id){l[c.id]=h}else{l.id=h}if(c.client){l.client=c.client}var k=$this.data();for(var j in k){if(j.startsWith("analytics")){var i=j.replace(/analytics/g,"").toLowerCase();l[i]=k[j]}}e.each(e(c.attributes),function(o,p){l[p]=$this.attr(p)});e.ajax({type:"POST",url:c.url,contentType:"application/x-www-form-urlencoded",data:l}).always(function(){if(c.captureOnce){$this.addClass("analytics-captured")}if(m){window.location=m}})}}}e.fn.analyticsUniqueId=function(){if(this.length==0){return}return this.each(function(){if(!e(this).attr("id")){e(this).attr("id","analytics-id-"+ ++g)}})};e.fn.analytics=function(i){if(e(this).length==0){return}c=e.extend({},c,i);var h=c.assignTo.join(",");return this.each(function(){e(this).find(h).andSelf().filter(h).each(function(){d(e(this));e(this).on("click",f)})}).on("DOMNodeInserted",function(j){e(j.target).find(h).andSelf().filter(h).each(function(){d(e(this));e(this).on("click",f)})})}})(jQuery);