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