Browse Source

Merge pull request #1 from bmallred/master

Added option to change object ID passed. Fixed issue with anchor redirection
Bryan Allred 11 years ago
parent
commit
e7a6743d02
2 changed files with 22 additions and 6 deletions
  1. 21 5
      jquery-analytics.js
  2. 1 1
      jquery-analytics.min.js

+ 21 - 5
jquery-analytics.js

@ -44,6 +44,7 @@ String.prototype.endsWith = function (search) {
44 44
        assignTo: ["a", "input[type='submit']"],
45 45
        captureOnce: false,
46 46
        client: null,
47
        id: "id",
47 48
        exclude: ".analytics-exclude",
48 49
        url: null
49 50
    };
@ -89,13 +90,24 @@ String.prototype.endsWith = function (search) {
89 90
        $this = $(this);
90 91
91 92
        if (settings.url && !$this.is(".analytics-captured") && !$this.is(settings.exclude)) {
92
            // // We prevent the default action to allow the background call to succeed.
93
            // e.preventDefault();
93
            // We prevent the default action to allow the background call to succeed.
94
            var preventedHref = null;
95
            if ($this.attr("href")) {
96
                e.preventDefault();
97
                preventedHref = $this.attr("href");
98
            }
94 99
95 100
            // Initialize the data to be collected.
96
            var data = {
97
                id: walkTree($this).join(' ')
98
            };
101
            var data = {};
102
103
            // Attach the object identifier.
104
            var tree = walkTree($this).join(' ');
105
            if (settings.id) {
106
                data[settings.id] = tree;
107
            }
108
            else {
109
                data["id"] = tree;
110
            }
99 111
100 112
            // Attach the client identifier if found.
101 113
            if (settings.client) {
@ -127,6 +139,10 @@ String.prototype.endsWith = function (search) {
127 139
                if (settings.captureOnce) {
128 140
                    $this.addClass("analytics-captured");
129 141
                }
142
143
                if (preventedHref) {
144
                    window.location = preventedHref;
145
                }
130 146
            });
131 147
        }
132 148
    };

+ 1 - 1
jquery-analytics.min.js

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