googleanalytics.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. (function ($) {
  2. $(document).ready(function() {
  3. // Expression to check for absolute internal links.
  4. var isInternal = new RegExp("^(https?):\/\/" + window.location.host, "i");
  5. // Attach onclick event to document only and catch clicks on all elements.
  6. $(document.body).click(function(event) {
  7. // Catch the closest surrounding link of a clicked element.
  8. $(event.target).closest("a,area").each(function() {
  9. var ga = Drupal.settings.googleanalytics;
  10. // Expression to check for special links like gotwo.module /go/* links.
  11. var isInternalSpecial = new RegExp("(\/go\/.*)$", "i");
  12. // Expression to check for download links.
  13. var isDownload = new RegExp("\\.(" + ga.trackDownloadExtensions + ")$", "i");
  14. // Is the clicked URL internal?
  15. if (isInternal.test(this.href)) {
  16. // Skip 'click' tracking, if custom tracking events are bound.
  17. if ($(this).is('.colorbox')) {
  18. // Do nothing here. The custom event will handle all tracking.
  19. }
  20. // Is download tracking activated and the file extension configured for download tracking?
  21. else if (ga.trackDownload && isDownload.test(this.href)) {
  22. // Download link clicked.
  23. var extension = isDownload.exec(this.href);
  24. _gaq.push(["_trackEvent", "Downloads", extension[1].toUpperCase(), this.href.replace(isInternal, '')]);
  25. }
  26. else if (isInternalSpecial.test(this.href)) {
  27. // Keep the internal URL for Google Analytics website overlay intact.
  28. _gaq.push(["_trackPageview", this.href.replace(isInternal, '')]);
  29. }
  30. }
  31. else {
  32. if (ga.trackMailto && $(this).is("a[href^='mailto:'],area[href^='mailto:']")) {
  33. // Mailto link clicked.
  34. _gaq.push(["_trackEvent", "Mails", "Click", this.href.substring(7)]);
  35. }
  36. else if (ga.trackOutbound && this.href.match(/^\w+:\/\//i)) {
  37. if (ga.trackDomainMode == 2 && isCrossDomain($(this).attr('hostname'), ga.trackCrossDomains)) {
  38. // Top-level cross domain clicked. document.location is handled by _link internally.
  39. event.preventDefault();
  40. _gaq.push(["_link", this.href]);
  41. }
  42. else {
  43. // External link clicked.
  44. _gaq.push(["_trackEvent", "Outbound links", "Click", this.href]);
  45. }
  46. }
  47. }
  48. });
  49. });
  50. // Colorbox: This event triggers when the transition has completed and the
  51. // newly loaded content has been revealed.
  52. $(document).bind("cbox_complete", function() {
  53. var href = $.colorbox.element().attr("href");
  54. if (href) {
  55. _gaq.push(["_trackPageview", href.replace(isInternal, '')]);
  56. }
  57. });
  58. });
  59. /**
  60. * Check whether the hostname is part of the cross domains or not.
  61. *
  62. * @param string hostname
  63. * The hostname of the clicked URL.
  64. * @param array crossDomains
  65. * All cross domain hostnames as JS array.
  66. *
  67. * @return boolean
  68. */
  69. function isCrossDomain(hostname, crossDomains) {
  70. /**
  71. * jQuery < 1.6.3 bug: $.inArray crushes IE6 and Chrome if second argument is
  72. * `null` or `undefined`, http://bugs.jquery.com/ticket/10076,
  73. * https://github.com/jquery/jquery/commit/a839af034db2bd934e4d4fa6758a3fed8de74174
  74. *
  75. * @todo: Remove/Refactor in D8
  76. */
  77. if (!crossDomains) {
  78. return false;
  79. }
  80. else {
  81. return $.inArray(hostname, crossDomains) > -1 ? true : false;
  82. }
  83. }
  84. })(jQuery);