function getAllUtmParams() { const urlParams = new URLSearchParams(window.location.search); const utmParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content']; const otherParams = ['gclid', 'fbclid', 'yclid', 'roistat', 'click_id']; const allParams = [...utmParams, ...otherParams]; const labels = {}; allParams.forEach(param => { if (urlParams.has(param)) { labels[param] = urlParams.get(param); } }); return labels; } const utmLabels = getAllUtmParams(); // Если есть метки — сохраняем их в sessionStorage и cookie if (Object.keys(utmLabels).length > 0) { sessionStorage.setItem('saved_utm_labels', JSON.stringify(utmLabels)); document.cookie = `saved_utm_labels=${encodeURIComponent(JSON.stringify(utmLabels))}; path=/; max-age=3600`; console.log('✅ UTM-метки сохранены:', utmLabels); } // 2. Отслеживаем успешную отправку формы GetCourse // GetCourse добавляет класс .getcourseform_success при успешной отправке const observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { if (mutation.target.classList && mutation.target.classList.contains('getcourseform_success')) { // Форма успешно отправлена — добавляем метки в URL редиректа setTimeout(function() { const storedLabels = sessionStorage.getItem('saved_utm_labels'); if (storedLabels) { const labels = JSON.parse(storedLabels); const currentUrl = window.location.href; let newUrl = currentUrl; Object.keys(labels).forEach(key => { if (!newUrl.includes(`${key}=`)) { const separator = newUrl.includes('?') ? '&' : '?'; newUrl += `${separator}${key}=${encodeURIComponent(labels[key])}`; } }); if (newUrl !== currentUrl) { console.log('🔄 Добавляем метки в URL редиректа:', newUrl); window.location.href = newUrl; } } }, 500); } }); }); // Начинаем следить за формой const formContainer = document.querySelector('.getcourse-form, [class*="getcourse"]'); if (formContainer) { observer.observe(formContainer, { attributes: true, subtree: true, attributeFilter: ['class'] }); } // 3. Дополнительно: перехватываем клик по кнопке отправки (для надёжности) document.addEventListener('click', function(e) { const button = e.target.closest('button[type="submit"], input[type="submit"], .getcourse_button'); if (button) { const storedLabels = sessionStorage.getItem('saved_utm_labels'); if (storedLabels) { const labels = JSON.parse(storedLabels); const forms = document.querySelectorAll('form'); forms.forEach(form => { const action = form.getAttribute('action') || ''; let newAction = action; Object.keys(labels).forEach(key => { if (!newAction.includes(`${key}=`)) { const separator = newAction.includes('?') ? '&' : '?'; newAction += `${separator}${key}=${encodeURIComponent(labels[key])}`; } }); if (newAction !== action) { form.setAttribute('action', newAction); } }); } } }, true); })();