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