googleanalytics.debug.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. (function ($) {
  2. Drupal.googleanalytics = {};
  3. $(document).ready(function() {
  4. // Attach mousedown, keyup, touchstart events to document only and catch
  5. // clicks on all elements.
  6. $(document.body).bind("mousedown keyup touchstart", function(event) {
  7. console.group("Running Google Analytics for Drupal.");
  8. console.info("Event '%s' has been detected.", event.type);
  9. // Catch the closest surrounding link of a clicked element.
  10. $(event.target).closest("a,area").each(function() {
  11. console.info("Closest element '%o' has been found. URL '%s' extracted.", this, this.href);
  12. // Is the clicked URL internal?
  13. if (Drupal.googleanalytics.isInternal(this.href)) {
  14. // Skip 'click' tracking, if custom tracking events are bound.
  15. if ($(this).is('.colorbox') && (Drupal.settings.googleanalytics.trackColorbox)) {
  16. // Do nothing here. The custom event will handle all tracking.
  17. console.info("Click on .colorbox item has been detected.");
  18. }
  19. // Is download tracking activated and the file extension configured for download tracking?
  20. else if (Drupal.settings.googleanalytics.trackDownload && Drupal.googleanalytics.isDownload(this.href)) {
  21. // Download link clicked.
  22. console.info("Download url '%s' has been found. Tracked download as extension '%s'.", Drupal.googleanalytics.getPageUrl(this.href), Drupal.googleanalytics.getDownloadExtension(this.href).toUpperCase());
  23. ga("send", {
  24. "hitType": "event",
  25. "eventCategory": "Downloads",
  26. "eventAction": Drupal.googleanalytics.getDownloadExtension(this.href).toUpperCase(),
  27. "eventLabel": Drupal.googleanalytics.getPageUrl(this.href),
  28. "transport": "beacon"
  29. });
  30. }
  31. else if (Drupal.googleanalytics.isInternalSpecial(this.href)) {
  32. // Keep the internal URL for Google Analytics website overlay intact.
  33. console.info("Click on internal special link '%s' has been tracked.", Drupal.googleanalytics.getPageUrl(this.href));
  34. ga("send", {
  35. "hitType": "pageview",
  36. "page": Drupal.googleanalytics.getPageUrl(this.href),
  37. "transport": "beacon"
  38. });
  39. }
  40. else {
  41. // e.g. anchor in same page or other internal page link
  42. console.info("Click on internal link '%s' detected, but not tracked by click.", this.href);
  43. }
  44. }
  45. else {
  46. if (Drupal.settings.googleanalytics.trackMailto && $(this).is("a[href^='mailto:'],area[href^='mailto:']")) {
  47. // Mailto link clicked.
  48. console.info("Click on e-mail '%s' has been tracked.", this.href.substring(7));
  49. ga("send", {
  50. "hitType": "event",
  51. "eventCategory": "Mails",
  52. "eventAction": "Click",
  53. "eventLabel": this.href.substring(7),
  54. "transport": "beacon"
  55. });
  56. }
  57. else if (Drupal.settings.googleanalytics.trackOutbound && this.href.match(/^\w+:\/\//i)) {
  58. if (Drupal.settings.googleanalytics.trackDomainMode !== 2 || (Drupal.settings.googleanalytics.trackDomainMode === 2 && !Drupal.googleanalytics.isCrossDomain(this.hostname, Drupal.settings.googleanalytics.trackCrossDomains))) {
  59. // External link clicked / No top-level cross domain clicked.
  60. console.info("Outbound link '%s' has been tracked.", this.href);
  61. ga("send", {
  62. "hitType": "event",
  63. "eventCategory": "Outbound links",
  64. "eventAction": "Click",
  65. "eventLabel": this.href,
  66. "transport": "beacon"
  67. });
  68. }
  69. else {
  70. console.info("Internal link '%s' clicked, not tracked.", this.href);
  71. }
  72. }
  73. }
  74. });
  75. console.groupEnd();
  76. });
  77. // Track hash changes as unique pageviews, if this option has been enabled.
  78. if (Drupal.settings.googleanalytics.trackUrlFragments) {
  79. window.onhashchange = function() {
  80. console.info("Track URL '%s' as pageview. Hash '%s' has changed.", location.pathname + location.search + location.hash, location.hash);
  81. ga("send", {
  82. "hitType": "pageview",
  83. "page": location.pathname + location.search + location.hash
  84. });
  85. };
  86. }
  87. // Colorbox: This event triggers when the transition has completed and the
  88. // newly loaded content has been revealed.
  89. if (Drupal.settings.googleanalytics.trackColorbox) {
  90. $(document).bind("cbox_complete", function () {
  91. var href = $.colorbox.element().attr("href");
  92. if (href) {
  93. console.info("Colorbox transition to url '%s' has been tracked.", Drupal.googleanalytics.getPageUrl(href));
  94. ga("send", {
  95. "hitType": "pageview",
  96. "page": Drupal.googleanalytics.getPageUrl(href)
  97. });
  98. }
  99. });
  100. }
  101. });
  102. /**
  103. * Check whether the hostname is part of the cross domains or not.
  104. *
  105. * @param string hostname
  106. * The hostname of the clicked URL.
  107. * @param array crossDomains
  108. * All cross domain hostnames as JS array.
  109. *
  110. * @return boolean
  111. */
  112. Drupal.googleanalytics.isCrossDomain = function (hostname, crossDomains) {
  113. /**
  114. * jQuery < 1.6.3 bug: $.inArray crushes IE6 and Chrome if second argument is
  115. * `null` or `undefined`, https://bugs.jquery.com/ticket/10076,
  116. * https://github.com/jquery/jquery/commit/a839af034db2bd934e4d4fa6758a3fed8de74174
  117. *
  118. * @todo: Remove/Refactor in D8
  119. */
  120. if (!crossDomains) {
  121. return false;
  122. }
  123. else {
  124. return $.inArray(hostname, crossDomains) > -1 ? true : false;
  125. }
  126. };
  127. /**
  128. * Check whether this is a download URL or not.
  129. *
  130. * @param string url
  131. * The web url to check.
  132. *
  133. * @return boolean
  134. */
  135. Drupal.googleanalytics.isDownload = function (url) {
  136. var isDownload = new RegExp("\\.(" + Drupal.settings.googleanalytics.trackDownloadExtensions + ")([\?#].*)?$", "i");
  137. return isDownload.test(url);
  138. };
  139. /**
  140. * Check whether this is an absolute internal URL or not.
  141. *
  142. * @param string url
  143. * The web url to check.
  144. *
  145. * @return boolean
  146. */
  147. Drupal.googleanalytics.isInternal = function (url) {
  148. var isInternal = new RegExp("^(https?):\/\/" + window.location.host, "i");
  149. return isInternal.test(url);
  150. };
  151. /**
  152. * Check whether this is a special URL or not.
  153. *
  154. * URL types:
  155. * - gotwo.module /go/* links.
  156. *
  157. * @param string url
  158. * The web url to check.
  159. *
  160. * @return boolean
  161. */
  162. Drupal.googleanalytics.isInternalSpecial = function (url) {
  163. var isInternalSpecial = new RegExp("(\/go\/.*)$", "i");
  164. return isInternalSpecial.test(url);
  165. };
  166. /**
  167. * Extract the relative internal URL from an absolute internal URL.
  168. *
  169. * Examples:
  170. * - https://mydomain.com/node/1 -> /node/1
  171. * - https://example.com/foo/bar -> https://example.com/foo/bar
  172. *
  173. * @param string url
  174. * The web url to check.
  175. *
  176. * @return string
  177. * Internal website URL
  178. */
  179. Drupal.googleanalytics.getPageUrl = function (url) {
  180. var extractInternalUrl = new RegExp("^(https?):\/\/" + window.location.host, "i");
  181. return url.replace(extractInternalUrl, '');
  182. };
  183. /**
  184. * Extract the download file extension from the URL.
  185. *
  186. * @param string url
  187. * The web url to check.
  188. *
  189. * @return string
  190. * The file extension of the passed url. e.g. "zip", "txt"
  191. */
  192. Drupal.googleanalytics.getDownloadExtension = function (url) {
  193. var extractDownloadextension = new RegExp("\\.(" + Drupal.settings.googleanalytics.trackDownloadExtensions + ")([\?#].*)?$", "i");
  194. var extension = extractDownloadextension.exec(url);
  195. return (extension === null) ? '' : extension[1];
  196. };
  197. })(jQuery);