(function (global) { const COOKIE_EXPIRATION_DAYS = 30; // Expiração padrão dos cookies // Expiração personalizada para cada parâmetro UTM const UTM_EXPIRATIONS = { utm_source: 7, utm_medium: 7, utm_campaign: 30, utm_term: 30, utm_content: 15, }; const CLEAN_UTM_FROM_URL = false; // Configuração para limpar UTMs da URL após captura // Função para definir um cookie function setCookie(name, value, days) { const date = new Date(); date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); // Dias para milissegundos const expires = 'expires=' + date.toUTCString(); document.cookie = name + '=' + encodeURIComponent(value) + ';' + expires + '; path=/'; } // Função para obter o valor de um cookie function getCookie(name) { const cookies = document.cookie.split("; "); for (let i = 0; i < cookies.length; i++) { const [key, value] = cookies[i].split("="); if (key === name) return decodeURIComponent(value); } return null; } // Função para validar valores UTM function isValidUTMValue(value) { const validPattern = /^[a-zA-Z0-9-_ ]+$/; // Permite letras, números, -, _ e espaços return validPattern.test(value); } // Função para capturar parâmetros UTM function captureUTM() { const params = new URLSearchParams(window.location.search); const utmParams = { utm_source: params.get("utm_source") || getCookie("utm_source"), utm_medium: params.get("utm_medium") || getCookie("utm_medium"), utm_campaign: params.get("utm_campaign") || getCookie("utm_campaign"), utm_term: params.get("utm_term") || getCookie("utm_term"), utm_content: params.get("utm_content") || getCookie("utm_content"), utm_source_platform: params.get("utm_source_platform") || getCookie("utm_source_platform"), utm_creative_format: params.get("utm_creative_format") || getCookie("utm_creative_format"), utm_marketing_tactic: params.get("utm_marketing_tactic") || getCookie("utm_marketing_tactic"), }; console.log("UTM Parameters Captured:", utmParams); // Salvar os parâmetros UTM em cookies com validação e expiração personalizada for (const [key, value] of Object.entries(utmParams)) { if (value && isValidUTMValue(value)) { const days = UTM_EXPIRATIONS[key] || COOKIE_EXPIRATION_DAYS; // Usa expiração personalizada setCookie(key, value, days); } } // Adicionar ao data layer global.habllaTagManager = global.habllaTagManager || []; global.habllaTagManager.push({ event: "utmCaptured", data: utmParams }); return utmParams; } // Função para carregar ou gerar o UUID function manageUUID() { let uuid = getCookie("uuid"); if (!uuid) { uuid = crypto.randomUUID(); // Gera um UUID único setCookie("uuid", uuid, COOKIE_EXPIRATION_DAYS); } console.log("UUID Captured:", uuid); // Adicionar ao data layer global.habllaTagManager = global.habllaTagManager || []; global.habllaTagManager.push({ event: "uuidCaptured", data: { uuid } }); return uuid; } // Função para adicionar UTMs e UUID aos formulários automaticamente function addUTMAndUUIDToForms(uuid) { const htmCookie = getCookie("HTMCookie"); if (htmCookie === "false") { return; } const forms = document.querySelectorAll("form"); // Seleciona todos os formulários na página forms.forEach((form) => { const utmKeys = [ "utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content", "utm_source_platform", "utm_creative_format", "utm_marketing_tactic", ]; // Adicionar UTMs aos formulários utmKeys.forEach((utmKey) => { const utmValue = getCookie(utmKey); if (utmValue) { let hiddenField = form.querySelector('input[name="' + utmKey + '}"]'); if (!hiddenField) { hiddenField = document.createElement("input"); hiddenField.type = "hidden"; hiddenField.name = utmKey; form.appendChild(hiddenField); } hiddenField.value = utmValue; } }); // Adicionar UUID ao formulário if (uuid) { let uuidField = form.querySelector('input[name="uuid"]'); if (!uuidField) { uuidField = document.createElement("input"); uuidField.type = "hidden"; uuidField.name = "uuid"; form.appendChild(uuidField); } uuidField.value = uuid; } }); } // Função para persistir UTMs na URL function persistUTMParams(utmParams) { const params = new URLSearchParams(window.location.search); let updated = false; for (const [key, value] of Object.entries(utmParams)) { if (value && !params.has(key)) { params.set(key, value); updated = true; } } if (updated) { const newUrl = window.location.pathname + '?' + params.toString(); window.history.replaceState(null, "", newUrl); } } // Função para limpar UTMs da URL após captura function cleanUTMFromURL() { if (!CLEAN_UTM_FROM_URL) return; const url = new URL(window.location.href); const params = url.searchParams; ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"].forEach((key) => { if (params.has(key)) params.delete(key); }); window.history.replaceState(null, "", url.pathname + (params.toString() ? "?" + params.toString() : "")); } // Capturar UTMs e UUID ao carregar o script const utmParams = captureUTM(); const uuid = manageUUID(); // Persistir os parâmetros UTM entre páginas persistUTMParams(utmParams); // Adicionar UTMs e UUID aos formulários addUTMAndUUIDToForms(uuid); // Limpar UTMs da URL após captura (se configurado) cleanUTMFromURL(); })(window);