1/** 2 * This script sets a custom cookie named "custom_ga" in the document with UTM parameters extracted from the URL query string. 3 * The cookie is set with an expiration of 30 days and is scoped to the domain of the current webpage. 4 * 5 * @author Iddar Olivares <[email protected]> 6 * @copyright Magma Partners, 2024 7 * 8 * @param {Document} document - The global document object of the webpage. 9 * @param {Window} window - The global window object of the webpage. 10 */ 11(function (document, window) { 12 // The name of the cookie to be set. 13 const cookieName = "custom_ga"; 14 // The number of days after which the cookie will expire. 15 const cookieExpire = 30; 16 //The domain of the current webpage, used for scoping the cookie. 17 const cookieDomain = location.hostname; 18 // The query string part of the URL, excluding the '?'. 19 const queryString = window.location.search.substring(1); 20 // An object to parse the query string. 21 const params = new URLSearchParams(queryString); 22 // An object to store the UTM parameters extracted from the query string. 23 const utmParams = {}; 24 // Loop through each parameter in the URL query string 25 for (const [key, value] of params) { 26 // Check if the parameter key includes "utm_" 27 if (key.includes("utm_")) { 28 // Add the UTM parameter to the utmParams object
29 utmParams[key] = value; 30 } 31 } 32 // Encode the UTM parameters object into a URI safe string 33 const safetyUTMs = encodeURI(JSON.stringify(utmParams)); 34 // Construct the cookie value string 35 const cookieValue = `${cookieName}=${safetyUTMs};expires=${cookieExpire};domain=${cookieDomain};path=/`; 36 // Set the cookie in the document 37 document.cookie = cookieValue; 38 })(document, window);
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.