1 2(function($) { 3 // wait for document ready 4 $(function() { 5 // Get raw cookie values 6 const cookies = document.cookie; 7 8 // Helper to read a specific cookie 9 function getCookie(name) { 10 const match = cookies.match(new RegExp('(?:^|; )' + name.replace(/([.$?*|{}()\[\]\\\/\+^])/g, '\\$1') + '=([^;]*)')); 11 return match ? decodeURIComponent(match[1]) : ''; 12 } 13 14 // helper function to sanitise cookies 15 function sanitise(str) { 16 if (!str) return ''; 17 return str.toString().replace(/<[^>]*>?/gm, '').trim(); 18 } 19 20 // prepare all variables 21 const cookieValue = sanitise(getCookie('_deco_utmz')); 22 const all_utm = cookieValue.split('§'); 23 const referrer = sanitise(getCookie('_deco_utm_referrer')); 24 const landingpage = sanitise(getCookie('_deco_utmurl')); 25 const submissionurl = window.location.origin + window.location.pathname; 26 const shortname = $('h1').data('shortname'); 27 const quiz_cookie = sanitise(getCookie('moodle_quiz_cookie')); 28 29 // handle false values in UTM fields 30 all_utm.forEach((value, i) => { 31 if (value === 'false') { 32 all_utm[i] = ''; 33 } 34 }); 35 36 // mapping object for vars to HS fields 37 const fieldMap = { 38 utm_source: { 39 value: all_utm[0], 40 hubspotFieldName: 'utm_source__c' 41 }, 42 utm_medium: { 43 value: all_utm[1], 44 hubspotFieldName: 'utm_medium__c' 45 }, 46 utm_term: { 47 value: all_utm[2], 48 hubspotFieldName: 'utm_term' 49 }, 50 utm_campaign: { 51 value: all_utm[3], 52 hubspotFieldName: 'utm_campaign__c' 53 }, 54 utm_content: { 55 value: all_utm[4], 56 hubspotFieldName: 'utm_content' 57 }, 58 shortname: { 59 value: shortname, 60 hubspotFieldName: 'moodle_partner__c' 61 }, 62 submissionurl: { 63 value: submissionurl, 64 hubspotFieldName: 'submission_url__c' 65 }, 66 referrer: { 67 value: referrer, 68 hubspotFieldName: 'referrer' 69 }, 70 landingpage: { 71 value: landingpage, 72 hubspotFieldName: 'landing_page_url' 73 } 74 }; 75 76 // function to loop through fieldMap, populate data into all appropriate Hubspot fields 77 function populateHubspotFields() { 78 for (const key in fieldMap) { 79 const mapping = fieldMap[key]; 80 // select ALL fields with this name 81 const fields = document.querySelectorAll(`[name="${mapping.hubspotFieldName}"]`); 82 83 fields.forEach(field => { 84 field.value = mapping.value || ''; 85 }); 86 } 87 } 88 89 // use MutationObserver to wait until form is loaded on page before populating 90 const observer = new MutationObserver((mutations, obs) => { 91 const formFields = document.querySelectorAll('[name="utm_source__c"]'); 92 if (formFields.length > 0) { 93 populateHubspotFields(); 94 obs.disconnect(); 95 } 96 }); 97 observer.observe(document.body, { 98 childList: true, 99 subtree: true 100 }); 101 102 }); 103})(jQuery);
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.