googleanalytics.js 6.1 KB

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