googleanalytics.js 5.4 KB

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