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