1(function ($, window) { 2 "use strict"; 3 4 $.extend($.expr[":"], { 5 ga: function (a) { 6 var attr = a.attributes, 7 len = attr.length; 8 9 while (len--) { 10 if (attr[len].name.indexOf("data-ga-") !== -1) { 11 return true; 12 } 13 } 14 15 return false; 16 } 17 }); 18 19 $.googleAnalyticsApi = { 20 trackEvent: { 21 event: { 22 value: "_trackEvent" 23 }, 24 category: { 25 value: null 26 }, 27 action: { 28 value: null 29 }, 30 label: { 31 value: null 32 }, 33 value: { 34 value: null 35 } 36 } 37 }; 38 39 var methods = { 40 createArgs: function () { 41 var self = this, 42 event = this.event, 43 params = []; 44 45 $.each(event, function (key, val) { 46 var value; 47 if (key === "event") { 48 value = val.value; 49 } else { 50 value = self.$element.attr("data-ga-" + key.toLowerCase()) 51 } 52 params.push(value); 53 }); 54 55 while (params[params.length - 1] == null) { 56 params.pop(); 57 } 58 59 return params; 60 } 61 }, 62 63 GoogleAnalytics = function (element, options) { 64 this.$element = $(element); 65 this.options = $.extend({}, $.fn.googleAnalytics.defaults, options); 66 }; 67 68 GoogleAnalytics.prototype = { 69 constructor: GoogleAnalytics, 70 trackEvent: function () { 71 var trackedEvent = $.Event("tracked.ga"); 72 this.args = methods.createArgs.call(this); 73 74 if (this.options.debug) { 75 if (window.console && window.console.log) { 76 console.log("Pushing Google analytics data.", this.args); 77 this.$element.trigger(trackedEvent); 78 } 79 } 80 else { 81 var gaq = window._gaq || window.gaq; 82 if (gaq) { 83 var self = this; 84 gaq.push(this.args) 85 //console.log(this.args) 86 self.$element.trigger(trackedEvent); 87 // Redirect the location - delayed so that any other page functionality has time to run. 88 setTimeout(function () { 89 var href = self.$element.attr("href"), 90 target = self.$element.attr("target"); 91 92 if (href && href.indexOf("#") !== 0) { 93 94 // IE8 doesn't pass the referrer to the next page 95 // using window.location. We create a hidden link to 96 // counter that. Fixes issue #2 97 // http://stackoverflow.com/a/7917528/427899 98 var a = document.createElement("a"); 99 100 if (!a.click) { 101 // Handle external window requests. 102 if (target && target === "_blank") { 103 window.open(href, target); 104 } 105 else { 106 // Old FF and Secure links. 107 window.location = href; 108 } 109 return; 110 } 111 112 a.setAttribute("href", href); 113 if (target && target === "_blank") { 114 a.setAttribute("target", target); 115 } 116 a.style.display = "none"; 117 document.body.appendChild(a); 118 a.click(); 119 } 120 121 }, 100); 122 } else { 123 throw new ReferenceError("Google Analytics _gaq/gaq object is not defined."); 124 } 125 } 126 } 127 }; 128 129 $.fn.googleAnalytics = function (options) { 130 return this.each(function () { 131 var $this = $(this), 132 data = $this.data("ga"), 133 opts = typeof options === "object" ? options : null; 134 135 if (!$(this).hasClass('ga-processed')) { 136 $this.addClass('ga-processed'); 137 138 if (!data) { 139 $this.data("ga", (data = new GoogleAnalytics(this, opts))); 140 } 141 142 var handler = data.options.handler.toLowerCase(), 143 event = data.$element.attr("data-ga-event"); 144 145 $.extend(data.options, { event: event }); 146 data.event = $.googleAnalyticsApi[data.options.event]; 147 data.$element.bind(handler + ".ga", function (e) { 148 if ($(this).attr('href') != undefined) { 149 e.preventDefault(); 150 } 151 data.trackEvent(); 152 }); 153 } 154 }); 155 }; 156 157 $.fn.googleAnalytics.defaults = { 158 event: "trackEvent", 159 handler: "click", 160 debug: false 161 }; 162 163 $.fn.googleAnalytics.Constructor = GoogleAnalytics; 164 165 $(document).ready(function () { 166 $(":ga").each(function () { 167 $(this).googleAnalytics(); 168 }); 169 170 $(document).ajaxComplete(function() { 171 $(":ga").each(function () { 172 $(this).googleAnalytics(); 173 }); 174 }); 175 }); 176 177}(jQuery, window));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.