googleanalytics.debug.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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);
  9. // Catch the closest surrounding link of a clicked element.
  10. $(event.target).closest("a,area").each(function() {
  11. console.info("Element '%o' has been detected. Link '%s' found.", 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')) {
  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", "event", "Downloads", Drupal.googleanalytics.getDownloadExtension(this.href).toUpperCase(), Drupal.googleanalytics.getPageUrl(this.href));
  24. }
  25. else if (Drupal.googleanalytics.isInternalSpecial(this.href)) {
  26. // Keep the internal URL for Google Analytics website overlay intact.
  27. console.info("Click on internal special link '%s' has been tracked.", Drupal.googleanalytics.getPageUrl(this.href));
  28. ga("send", "pageview", { "page": Drupal.googleanalytics.getPageUrl(this.href) });
  29. }
  30. else {
  31. // e.g. anchor in same page or other internal page link
  32. console.info("Click on internal link '%s' detected, but not tracked by click.", this.href);
  33. }
  34. }
  35. else {
  36. if (Drupal.settings.googleanalytics.trackMailto && $(this).is("a[href^='mailto:'],area[href^='mailto:']")) {
  37. // Mailto link clicked.
  38. console.info("Click on e-mail '%s' has been tracked.", this.href.substring(7));
  39. ga("send", "event", "Mails", "Click", this.href.substring(7));
  40. }
  41. else if (Drupal.settings.googleanalytics.trackOutbound && this.href.match(/^\w+:\/\//i)) {
  42. if (Drupal.settings.googleanalytics.trackDomainMode != 2 || (Drupal.settings.googleanalytics.trackDomainMode == 2 && !Drupal.googleanalytics.isCrossDomain(this.hostname, Drupal.settings.googleanalytics.trackCrossDomains))) {
  43. // External link clicked / No top-level cross domain clicked.
  44. console.info("Outbound link '%s' has been tracked.", this.href);
  45. ga("send", "event", "Outbound links", "Click", this.href);
  46. }
  47. else {
  48. console.info("Internal link '%s' clicked, not tracked.", this.href);
  49. }
  50. }
  51. }
  52. });
  53. console.groupEnd();
  54. });
  55. // Track hash changes as unique pageviews, if this option has been enabled.
  56. if (Drupal.settings.googleanalytics.trackUrlFragments) {
  57. window.onhashchange = function() {
  58. console.info("Track URL '%s' as pageview. Hash '%s' has changed.", location.pathname + location.search + location.hash, location.hash);
  59. ga('send', 'pageview', location.pathname + location.search + location.hash);
  60. }
  61. }
  62. // Colorbox: This event triggers when the transition has completed and the
  63. // newly loaded content has been revealed.
  64. $(document).bind("cbox_complete", function () {
  65. var href = $.colorbox.element().attr("href");
  66. if (href) {
  67. console.info("Colorbox transition to url '%s' has been tracked.", Drupal.googleanalytics.getPageUrl(href));
  68. ga("send", "pageview", { "page": Drupal.googleanalytics.getPageUrl(href) });
  69. }
  70. });
  71. });
  72. /**
  73. * Check whether the hostname is part of the cross domains or not.
  74. *
  75. * @param string hostname
  76. * The hostname of the clicked URL.
  77. * @param array crossDomains
  78. * All cross domain hostnames as JS array.
  79. *
  80. * @return boolean
  81. */
  82. Drupal.googleanalytics.isCrossDomain = function (hostname, crossDomains) {
  83. /**
  84. * jQuery < 1.6.3 bug: $.inArray crushes IE6 and Chrome if second argument is
  85. * `null` or `undefined`, http://bugs.jquery.com/ticket/10076,
  86. * https://github.com/jquery/jquery/commit/a839af034db2bd934e4d4fa6758a3fed8de74174
  87. *
  88. * @todo: Remove/Refactor in D8
  89. */
  90. if (!crossDomains) {
  91. return false;
  92. }
  93. else {
  94. return $.inArray(hostname, crossDomains) > -1 ? true : false;
  95. }
  96. };
  97. /**
  98. * Check whether this is a download URL or not.
  99. *
  100. * @param string url
  101. * The web url to check.
  102. *
  103. * @return boolean
  104. */
  105. Drupal.googleanalytics.isDownload = function (url) {
  106. var isDownload = new RegExp("\\.(" + Drupal.settings.googleanalytics.trackDownloadExtensions + ")([\?#].*)?$", "i");
  107. return isDownload.test(url);
  108. };
  109. /**
  110. * Check whether this is an absolute internal URL or not.
  111. *
  112. * @param string url
  113. * The web url to check.
  114. *
  115. * @return boolean
  116. */
  117. Drupal.googleanalytics.isInternal = function (url) {
  118. var isInternal = new RegExp("^(https?):\/\/" + window.location.host, "i");
  119. return isInternal.test(url);
  120. };
  121. /**
  122. * Check whether this is a special URL or not.
  123. *
  124. * URL types:
  125. * - gotwo.module /go/* links.
  126. *
  127. * @param string url
  128. * The web url to check.
  129. *
  130. * @return boolean
  131. */
  132. Drupal.googleanalytics.isInternalSpecial = function (url) {
  133. var isInternalSpecial = new RegExp("(\/go\/.*)$", "i");
  134. return isInternalSpecial.test(url);
  135. };
  136. /**
  137. * Extract the relative internal URL from an absolute internal URL.
  138. *
  139. * Examples:
  140. * - http://mydomain.com/node/1 -> /node/1
  141. * - http://example.com/foo/bar -> http://example.com/foo/bar
  142. *
  143. * @param string url
  144. * The web url to check.
  145. *
  146. * @return string
  147. * Internal website URL
  148. */
  149. Drupal.googleanalytics.getPageUrl = function (url) {
  150. var extractInternalUrl = new RegExp("^(https?):\/\/" + window.location.host, "i");
  151. return url.replace(extractInternalUrl, '');
  152. };
  153. /**
  154. * Extract the download file extension from the URL.
  155. *
  156. * @param string url
  157. * The web url to check.
  158. *
  159. * @return string
  160. * The file extension of the passed url. e.g. "zip", "txt"
  161. */
  162. Drupal.googleanalytics.getDownloadExtension = function (url) {
  163. var extractDownloadextension = new RegExp("\\.(" + Drupal.settings.googleanalytics.trackDownloadExtensions + ")([\?#].*)?$", "i");
  164. var extension = extractDownloadextension.exec(url);
  165. return (extension === null) ? '' : extension[1];
  166. };
  167. })(jQuery);