1/** 2 * @file 3 * Links need to be tagged with unique IDs and interaction-type events 4 * (download versus video start), then pushed to the dataLayer 5 */ 6 7(function ($, Drupal) { 8 'use strict'; 9 10 Drupal.trade_analytics = {}; 11 12 $(document).ready(function () { 13 // Add a new click event listener for marketdiversification redirect link 14 $('a[href="https://beta.trade.gov/marketdiversification"]').click(function (e) { 15 e.preventDefault(); // Prevent the default behavior of the link 16 17 // Construct the new URL without any query parameters 18 let url = new URL('https://beta.trade.gov/marketdiversification'); 19 20 // Open the new URL in a new window without any query parameters 21 window.open(url.href); 22 }); 23 24 // Add a new click event listener for the specific link that needs to be redirected 25 // this is to fix a # that is being added to the link when redirected 26 $('a[href="https://otexaprod.trade.gov/madeinusa/regapp/"]').click(function (e) { 27 e.preventDefault(); // Prevent the default behavior of the link 28 // Construct the new URL, preserving the GA parameters while removing the URL fragment 29 let url = new URL('https://otexaprod.trade.gov/madeinusa/regapp/'); 30 url.search = window.location.search; // Preserve GA parameters 31 32 // Redirect to the new URL without the URL fragment 33 //window.location.href = url.href; 34 window.open(url.href); 35 }); 36 // header/footer navigation id=siteNavigation-[header/footer] 37 $('.ita-header__menu a').click(function () { 38 window.dataLayer.push({'siteNavigation-header': $(this).text()}); 39 40 window.dataLayer.push({ 41 'event': 'HeaderLinkClick', 42 'eventCategory': 'HeaderLink', 43 'eventAction': 'Click', 44 'eventLabel': $(this).text() 45 }); 46 }); 47 $('.footer__menu a').click(function () { 48 window.dataLayer.push({'siteNavigation-footer': $(this).text()}); 49 50 window.dataLayer.push({ 51 'event': 'FooterLinkClick', 52 'eventCategory': 'FooterLink', 53 'eventAction': 'Click', 54 'eventLabel': $(this).text() 55 }); 56 }); 57 58 // internal / external links 59 $('a').click(function () { 60 // ita-specific id=itaInternalLink (trade.gov plus list of ita-owned properties) 61 if (Drupal.trade_analytics.isInternal(this.href) || Drupal.trade_analytics.isTradeDomain(this.hostname, tradeDomains)) { 62 window.dataLayer.push({'itaInternalLink': $(this).text()}); 63 } else { 64 // id=externalLink (trade.gov plus list of ita-owned properties) 65 window.dataLayer.push({'externalLink': $(this).text()}); 66 window.dataLayer.push({ 67 'event': 'gaEvent', 68 'eventCategory': 'ExternalLink', 69 'eventAction': 'Click', 70 'eventLabel': $(this).text() 71 }); 72 } 73 }); 74 }); 75 76 /** 77 * set tracked download extensions and internally tracked domains 78 */ 79 var trackDownloadExtensions = [ 80 'zip', 81 'pdf', 82 ]; 83 84 var tradeDomains = [ 85 'www.trade.gov', // keeping this here for ita-dev.mobomo.net and local testing 86 'www.export.gov', 87 '2016.export.gov', 88 'enforcement.trade.gov', 89 'www.stopfakes.gov', 90 'www.selectusa.gov', 91 ]; 92 93 // url functions pulled from google analytics 94 /** 95 * Check whether the hostname is part of the cross domains or not. 96 * 97 * @param {string} hostname 98 * The hostname of the clicked URL. 99 * @param {array} tradeDomains 100 * All internally tracked domain hostnames as JS array. 101 * 102 * @return {boolean} isTradeDomain 103 */ 104 Drupal.trade_analytics.isTradeDomain = function (hostname, tradeDomains) { 105 return $.inArray(hostname, tradeDomains) > -1 ? true : false; 106 }; 107 108 /** 109 * Check whether this is a download URL or not. 110 * 111 * @param {string} url 112 * The web url to check. 113 * 114 * @return {boolean} isDownload 115 */ 116 Drupal.trade_analytics.isDownload = function (url) { 117 var isDownload = new RegExp('\\.(' + trackDownloadExtensions + ')([\?#].*)?$', 'i'); 118 return isDownload.test(url); 119 }; 120 121 /** 122 * Check whether this is an absolute internal URL or not. 123 * 124 * @param {string} url 125 * The web url to check. 126 * 127 * @return {boolean} isInternal 128 */ 129 Drupal.trade_analytics.isInternal = function (url) { 130 var isInternal = new RegExp('^(https?):\/\/' + window.location.host, 'i'); 131 return isInternal.test(url); 132 }; 133 134 /** 135 * Extract the relative internal URL from an absolute internal URL. 136 * 137 * Examples: 138 * - https://mydomain.com/node/1 -> /node/1 139 * - https://example.com/foo/bar -> https://example.com/foo/bar 140 * 141 * @param {string} url 142 * The web url to check. 143 * 144 * @return {string} getPageUrl 145 * Internal website URL. 146 */ 147 Drupal.trade_analytics.getPageUrl = function (url) { 148 var extractInternalUrl = new RegExp('^(https?):\/\/' + window.location.host, 'i'); 149 return url.replace(extractInternalUrl, ''); 150 }; 151 152 /** 153 * Extract the download file extension from the URL. 154 * 155 * @param {string} url 156 * The web url to check. 157 * 158 * @return {string} getDownloadExtension 159 * The file extension of the passed url. e.g. 'zip', 'txt' 160 */ 161 Drupal.trade_analytics.getDownloadExtension = function (url) { 162 var extractDownloadextension = new RegExp('\\.(' + trackDownloadExtensions + ')([\?#].*)?$', 'i'); 163 var extension = extractDownloadextension.exec(url); 164 return (extension === null) ? '' : extension[1]; 165 }; 166 167})(jQuery, Drupal);
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.