// Função para verificar e adicionar utm_source apenas se não houver nenhum utm na URL
function addUtmSource(url, utmSourceValue) {
try {
const urlObj = new URL(url);
// Verifica se algum parâmetro utm já existe
const hasUtmParams = Array.from(urlObj.searchParams.keys()).some(param => param.startsWith('utm_'));
// Adiciona utm_source apenas se não houver nenhum parâmetro utm na URL
if (!hasUtmParams) {
urlObj.searchParams.set('utm_source', utmSourceValue);
return urlObj.toString();
}
// Retorna a URL original caso já exista utm na URL
return url;
} catch (error) {
console.error('URL inválida:', error.message);
return url; // Retorna a URL original em caso de erro
}
}
// Aplica a função a todos os botões com links do motor de reservas
function updateButtonsUtm(utmSourceValue) {
const buttons = document.querySelectorAll('a');
buttons.forEach((button) => {
const href = button.getAttribute('href');
if (href && href.includes('hbook.hsystem.com.br')) { // Garante que o link seja do motor
const updatedUrl = addUtmSource(href, utmSourceValue);
button.setAttribute('href', updatedUrl);
}
});
}
// Chama a função para processar os botões na página
document.addEventListener('DOMContentLoaded', () => {
updateButtonsUtm('website');
});