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 gtag('event', Drupal.google_analytics.getDownloadExtension(this.href).toUpperCase(), { 35 event_category: 'Downloads', 36 event_label: Drupal.google_analytics.getPageUrl(this.href), 37 transport_type: 'beacon' 38 }); 39 } 40 else if (Drupal.google_analytics.isInternalSpecial(this.href)) { 41 // Keep the internal URL for Google Analytics website overlay intact. 42 // @todo: May require tracking ID 43 gtag('config', drupalSettings.google_analytics.account, { 44 page_path: Drupal.google_analytics.getPageUrl(this.href), 45 transport_type: 'beacon' 46 }); 47 } 48 } 49 else { 50 if (drupalSettings.google_analytics.trackMailto && $(this).is("a[href^='mailto:'],area[href^='mailto:']")) { 51 // Mailto link clicked. 52 gtag('event', 'Click', { 53 event_category: 'Mails', 54 event_label: this.href.substring(7), 55 transport_type: 'beacon' 56 }); 57 } 58 else if (drupalSettings.google_analytics.trackTel && $(this).is("a[href^='tel:'],area[href^='tel:']")) { 59 // Tel link clicked. 60 gtag('event', 'Click', { 61 event_category: 'Telephone calls', 62 event_label: this.href.substring(4), 63 transport_type: 'beacon' 64 }); 65 } 66 else if (drupalSettings.google_analytics.trackOutbound && this.href.match(/^\w+:\/\//i)) { 67 if (drupalSettings.google_analytics.trackDomainMode !== 2 || (drupalSettings.google_analytics.trackDomainMode === 2 && !Drupal.google_analytics.isCrossDomain(this.hostname, drupalSettings.google_analytics.trackCrossDomains))) { 68 // External link clicked / No top-level cross domain clicked. 69 gtag('event', 'Click', { 70 event_category: 'Outbound links', 71 event_label: this.href, 72 transport_type: 'beacon' 73 }); 74 } 75 } 76 } 77 }); 78 }); 79 80 // Track hash changes as unique pageviews, if this option has been enabled. 81 if (drupalSettings.google_analytics.trackUrlFragments) { 82 window.onhashchange = function () { 83 gtag('config', drupalSettings.google_analytics.account, { 84 page_path: location.pathname + location.search + location.hash 85 }); 86 }; 87 } 88 89 // Colorbox: This event triggers when the transition has completed and the 90 // newly loaded content has been revealed. 91 if (drupalSettings.google_analytics.trackColorbox) { 92 $(document).on('cbox_complete', function () { 93 var href = $.colorbox.element().attr('href'); 94 if (href) { 95 gtag('config', drupalSettings.google_analytics.account, { 96 page_path: 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.