(function() {
// Функция для получения UTM-меток из URL
function getUtmParams() {
const urlParams = new URLSearchParams(window.location.search);
const utmParams = {};
for (const [key, value] of urlParams) {
if (key.startsWith('utm_')) {
utmParams[key] = value;
}
}
return utmParams;
}
// Сохраняем UTM-метки в localStorage
const utmParams = getUtmParams();
if (Object.keys(utmParams).length > 0) {
localStorage.setItem('utm_params', JSON.stringify(utmParams));
}
// Добавляем обработчик для всех кнопок, которые ведут на другие страницы
document.addEventListener('click', function(e) {
const link = e.target.closest('a');
if (link && link.href) {
const currentUtm = localStorage.getItem('utm_params');
if (currentUtm) {
const utmObj = JSON.parse(currentUtm);
const url = new URL(link.href);
for (const [key, value] of Object.entries(utmObj)) {
url.searchParams.set(key, value);
}
link.href = url.toString();
}
}
});
})();