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.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // The MIT License (MIT)
  2. //
  3. // Copyright (c) 2013 Bryan Allred <bryan.allred@gmail.com>
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. // Check to see if a string starts with the given search criteria.
  23. // @param {String} search
  24. // @return {Boolean} a value indicating whether the string starts with the search criteria
  25. String.prototype.startsWith = function (search) {
  26. return this.indexOf(search) == 0;
  27. };
  28. // Check to see if a string ends with the given search criteria.
  29. // @param {String} search
  30. // @return {Boolean} a value indicating whether the string ends with the search criteria
  31. String.prototype.endsWith = function (search) {
  32. return original.lastIndexOf(search) == original.length - search.length;
  33. };
  34. (function ($) {
  35. // Declared outside of scope to maintain an accurate count.
  36. var uniqueId = 0;
  37. // Provide a unique identifier to an element if one has not already been assigned.
  38. // @return {Object} modified jQuery objects
  39. $.fn.analyticsUniqueId = function () {
  40. if (this.length == 0) {
  41. return;
  42. }
  43. return this.each(function () {
  44. if (!$(this).attr("id")) {
  45. $(this).attr("id", "analytics-id-" + ++uniqueId);
  46. }
  47. });
  48. };
  49. })(jQuery);
  50. (function ($) {
  51. // Default settings which may be extended upon.
  52. var settings = {
  53. attributes: [],
  54. assignTo: ["a", "input[type='submit']"],
  55. captureOnce: false,
  56. client: null,
  57. exclude: ".analytics-exclude",
  58. url: null
  59. };
  60. // Walk the tree of a given node.
  61. // @param {Object} element
  62. // @return {Array} path
  63. function walkTree(element) {
  64. var tree = [];
  65. var tagName = $(element).prop("tagName");
  66. if (tagName != undefined) {
  67. var parent = $(element).parent();
  68. if (parent != undefined) {
  69. $.each(walkTree($(element).parent()), function (i, node) {
  70. tree.push(node);
  71. });
  72. }
  73. if (tagName == "HTML" || tagName == "BODY") {
  74. tree.push(tagName);
  75. }
  76. else {
  77. var tagId = $(element).analyticsUniqueId().attr("id");
  78. tree.push(tagName + '[id="' + tagId + '"]');
  79. }
  80. }
  81. return tree;
  82. };
  83. // Identify the path to the node.
  84. // @param {Object} node
  85. function identifyPath(node) {
  86. // Assign identification to all relevant elements.
  87. walkTree(node);
  88. };
  89. // Initiate a trace on click.
  90. // @param {Object} e
  91. function initiateTrace(e) {
  92. // Locally scope this variable.
  93. $this = $(this);
  94. if (settings.url && !$this.is(".analytics-captured") && !$this.is(settings.exclude)) {
  95. // // We prevent the default action to allow the background call to succeed.
  96. // e.preventDefault();
  97. // Initialize the data to be collected.
  98. var data = {
  99. id: walkTree($this).join(' ')
  100. };
  101. // Attach the client identifier if found.
  102. if (settings.client) {
  103. data["client"] = settings.client
  104. }
  105. // Assign any "data-analytics-" attributes.
  106. var dataAttributes = $this.data();
  107. for (var attribute in dataAttributes) {
  108. if (attribute.startsWith("analytics")) {
  109. var cleanName = attribute.replace(/analytics/g, '').toLowerCase();
  110. data[cleanName] = dataAttributes[attribute];
  111. }
  112. }
  113. // Assign the custom attributes requested to be collected.
  114. $.each($(settings.attributes), function (i, attribute) {
  115. data[attribute] = $this.attr(attribute);
  116. });
  117. // Send the analytics.
  118. $.ajax({
  119. type: "POST",
  120. url: settings.url,
  121. contentType: "application/x-www-form-urlencoded",
  122. data: data
  123. })
  124. .always(function () {
  125. if (settings.captureOnce) {
  126. $this.addClass("analytics-captured");
  127. }
  128. });
  129. }
  130. };
  131. // Plug-in function providing easy access to analytics.
  132. // @param {Object} options
  133. // @returns {Object} modified jQuery objects
  134. $.fn.analytics = function (options) {
  135. if ($(this).length == 0) {
  136. return;
  137. }
  138. // Configure the default settings.
  139. settings = $.extend({}, settings, options);
  140. // Declare the selector to be used.
  141. var selector = settings.assignTo.join(",");
  142. return this.each(function () {
  143. // Itereate through all elements given on initiation.
  144. $(this).find(selector).andSelf().filter(selector)
  145. .each(function () {
  146. identifyPath($(this));
  147. $(this).on("click", initiateTrace);
  148. });
  149. })
  150. .on("DOMNodeInserted", function (e) {
  151. // This will capture any dynamically generated content.
  152. $(e.target).find(selector).andSelf().filter(selector)
  153. .each(function () {
  154. identifyPath($(this));
  155. $(this).on("click", initiateTrace);
  156. });
  157. });
  158. };
  159. })(jQuery);