1(function ($, Drupal) { 2 'use strict'; 3 4 const none = '(none)'; 5 6 const lsName = 'ts_traffic_source'; 7 8 const baseParam = 'gclid'; 9 10 const utmParams = [ 11 'utm_source', 12 'utm_medium', 13 'utm_campaign', 14 'utm_term', 15 'utm_content', 16 ]; 17 18 const otherParams = [ 19 'gbraid', 20 'wbraid', 21 ]; 22 23 const searchEngines = { 24 '360.cn': '360.cn', 25 'alice.com': 'alice', 26 'aliceadsl.fr': 'alice', 27 'alltheweb.com': 'alltheweb', 28 'altavista.com': 'altavista', 29 'aol.com': 'aol', 30 'ask.com': 'ask', 31 'search.aol.': 'ask', 32 'alicesuche.aol.': 'ask', 33 'search.auone.jp': 'auone', 34 'isearch.avg.com': 'avg', 35 'search.babylon.com': 'babylon', 36 'baidu.com': 'baidu', 37 'biglobe.ne.jp': 'biglobe', 38 'bing.com': 'bing', 39 'search.centrum.cz': 'centrum.cz', 40 'search.comcast.net': 'comcast', 41 'search.conduit.com': 'Conduit', 42 'cnn.com': 'cnn', 43 'daum.net': 'daum', 44 'duckduckgo.com': 'duckduckgo', 45 'ecosia.org': 'ecosia', 46 'ekolay.net': 'ekolay', 47 'eniro.se': 'eniro', 48 'globo.com': 'globo', 49 'go.mail.ru': 'go.mail.ru', 50 'google.': 'google', 51 'goo.ne.jp': 'goo.ne', 52 'haosou.com': 'haosou.com', 53 'search.incredimail.com': 'incredimail', 54 'kvasir.no': 'kvasir', 55 'lycos.com': 'lycos', 56 'search.lycos.': 'lycos', 57 'mamma.com': 'mamma', 58 'msn.com': 'msn', 59 'mynet.com': 'mynet', 60 'najdi.si': 'najdi', 61 'naver.com': 'naver', 62 'netscape.com': 'netscape', 63 'szukaj.onet.pl': 'onet', 64 'ozu.es': 'ozu', 65 'qwant.com': 'qwant', 66 'rakuten.co.jp': 'rakuten', 67 'rambler.ru': 'rambler', 68 'search-results.com': 'search-results', 69 'search.smt.docomo.ne.jp': 'search.smt.docomo', 70 'sesam.no': 'sesam', 71 'seznam.cz': 'seznam', 72 'so.com': 'so.com', 73 'sogou.com': 'sogou', 74 'startsiden.no': 'startsiden', 75 'szukacz.pl': 'szukacz', 76 'buscador.terra.com.br': 'terra', 77 'search.tut.by': 'tut.by', 78 'search.ukr.net': 'ukr', 79 'search.virgilio.it': 'virgilio', 80 'voila.fr': 'voila', 81 'wp.pl': 'wirtualna polska', 82 'yahoo.': 'yahoo', 83 'yandex.': 'yandex', 84 'yam.com': 'yam', 85 }; 86 87 const setTempTraffic = (traffic) => { 88 traffic['tmp_source'] = traffic.utm_source; 89 traffic['tmp_medium'] = traffic.utm_medium ?? none; 90 91 return traffic; 92 }; 93 94 const getTraffic = (searchParams) => { 95 let traffic = {}; 96 if(searchParams.has(baseParam)) { 97 traffic = { 98 utm_source: 'google', 99 utm_medium: 'cpc', 100 }; 101
102 utmParams.forEach((key) => { 103 if(traffic[key] === undefined) { 104 traffic[key] = searchParams.has(key) ? searchParams.get(key) : none; 105 } 106 }); 107 108 Object.entries(traffic).forEach(([key, value]) => { 109 traffic[`first_${key}`] = value; 110 }); 111 112 setTempTraffic(traffic); 113 114 traffic[baseParam] = searchParams.get(baseParam); 115 } else { 116 let count = 0; 117 utmParams.forEach((key) => { 118 if(searchParams.has(key)) { 119 count++; 120 } 121 }); 122 123 if(count) { 124 utmParams.forEach((key) => { 125 traffic[key] = searchParams.has(key) ? searchParams.get(key) : none; 126 }); 127 128 setTempTraffic(traffic); 129 } else { 130 const referrer = document.referrer; 131 const domain = referrer.length ? new URL(referrer).hostname : ''; 132 if(domain.indexOf('.creatio.com') === -1) { 133 if(domain) { 134 Object.entries(searchEngines).forEach(([key, value]) => { 135 if(domain.includes(key)) { 136 traffic = { 137 utm_source: value, 138 utm_medium: 'organic', 139 }; 140 141 return; 142 } 143 }); 144 145 if(!Object.keys(traffic).length) { 146 traffic = { 147 utm_source: domain, 148 utm_medium: 'referral', 149 }; 150 } 151 152 setTempTraffic(traffic); 153 } else { 154 traffic = { 155 utm_source: '(direct)', 156 }; 157 } 158 } else { 159 const lsTraffic = localStorage.getItem(lsName) === null ? {} : JSON.parse(localStorage.getItem(lsName)); 160 if(Object.keys(lsTraffic).length && lsTraffic.tmp_source !== undefined) { 161 traffic = { 162 utm_source: lsTraffic.tmp_source, 163 utm_medium: lsTraffic.tmp_medium 164 }; 165 } else { 166 traffic = { 167 utm_source: '(direct)', 168 }; 169 } 170 } 171
172 utmParams.forEach((key) => { 173 if(traffic[key] === undefined) { 174 traffic[key] = none; 175 } 176 }); 177 } 178 179 Object.entries(traffic).forEach(([key, value]) => { 180 traffic[`first_${key}`] = value; 181 }); 182 183 traffic[baseParam] = null; 184 } 185 186 return traffic; 187 }; 188 189 const setTraffic = () => { 190 let traffic = {}; 191 const lsTraffic = localStorage.getItem(lsName) === null ? {} : JSON.parse(localStorage.getItem(lsName)); 192 const searchParams = new URLSearchParams(window?.location?.search || ''); 193 if(Object.keys(lsTraffic).length) { 194 traffic = JSON.parse(localStorage.getItem(lsName)); 195 const currentTraffic = getTraffic(searchParams); 196 if(currentTraffic.tmp_source !== undefined) { 197 utmParams.forEach((key) => { 198 traffic[key] = currentTraffic[key]; 199 }); 200 } 201 202 [baseParam, 'tmp_source', 'tmp_medium'].forEach((key) => { 203 if(currentTraffic[key] !== undefined) { 204 traffic[key] = currentTraffic[key]; 205 } 206 }); 207 } else { 208 traffic = getTraffic(searchParams); 209 } 210 211 otherParams.forEach((key) => { 212 traffic[key] = searchParams.has(key) ? searchParams.get(key) : null; 213 }); 214 215 if(JSON.stringify(traffic) !== JSON.stringify(lsTraffic)) { 216 localStorage.setItem(lsName, JSON.stringify(traffic)); 217 } 218 }; 219 220 $(document).ready(() => { 221 setTraffic(); 222 }); 223 224 window.addEventListener('CookiebotOnAccept', (e) => { 225 setTraffic(); 226 }); 227 228 window.addEventListener('CookiebotOnDecline', (e) => { 229 setTraffic(); 230 }); 231 232 $(document).on('blur', 'input[name="field_lead_firstname"]', (e) => { 233 if(typeof Cookiebot !== 'undefined' && Cookiebot?.consent?.statistics) { 234 const form = $(e.target).closest('form'); 235 const lsTraffic = localStorage.getItem(lsName) === null ? {} : JSON.parse(localStorage.getItem(lsName)); 236 Object.entries(lsTraffic).forEach(([key, value]) => { 237 const name = key.startsWith('utm_') ? `field_lead_${key.replace('utm_', 'utm')}` : `field_lead_${key}`; 238 const field = form.find(`input[name="${name}"]`); 239 if(field.length && value !== null) { 240 field.val(value); 241 } 242 }); 243 244 if(form.find('input[name="field_lead_ga_client_id"]').length && $.cookie('_ga') !== undefined) { 245 const parts = $.cookie('_ga').split('.'); 246 parts.shift(); 247 parts.shift(); 248 form.find('input[name="field_lead_ga_client_id"]').val(parts.join('.')); 249 } 250 251 if(form.find('input[name="field_lead_user_agent"]').length) { 252 form.find('input[name="field_lead_user_agent"]').val(navigator.userAgent); 253 } 254 255 const cookies = { 256 '_ga_BGNNESGLC6' : 'field_lead_ga_session_id', 257 'li_fat_id': 'field_lead_li_fat_id', 258 'CookieConsent': 'field_lead_cookie_consent', 259 '_fbp': 'field_fbp', 260 '_fbc': 'field_fbc' 261 }; 262 263 Object.entries(cookies).forEach(([cookie, name]) => { 264 if(form.find(`input[name="${name}"]`).length && $.cookie(cookie) !== undefined) { 265 form.find(`input[name="${name}"]`).val($.cookie(cookie)); 266 } 267 }); 268 269 if(window._creatio_vid && form.find('input[name="field_creatio_vid"]').length) { 270 form.find('input[name="field_creatio_vid"]').val(window._creatio_vid); 271 } 272 273 if(form.find('input[name="field_lead_commentary"]').length && drupalSettings.path.currentPath.indexOf('landings/') === 0) { 274 const searchParams = new URLSearchParams(window?.location?.search || ''); 275 if(searchParams.has('visitor_id')) { 276 const visitor_id = searchParams.get('visitor_id'); 277 if(visitor_id.length) { 278 form.find('input[name="field_lead_commentary"]').val(`visitor_id: ${visitor_id}`); 279 } 280 } 281 } 282 } 283 }); 284 285} (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.