Last active
February 19, 2025 08:08
-
-
Save koddr/efba3d88019ed090f21d9e430472feb5 to your computer and use it in GitHub Desktop.
Буткемпы: скрипты для страниц Tilda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
// массив, в котором лежат нужные нам названия UTM | |
var UTMS = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term']; | |
// функция, которая вытаскивает все GET-параметры из адресной строки | |
function findGETParameterByName(name) { | |
var result = null, tmp = []; | |
location.search.substr(1).split('&').forEach(function (item) { | |
tmp = item.split('='); | |
if (tmp[0] === name) result = decodeURIComponent(tmp[1]); | |
}); | |
return result; | |
} | |
// бежим по массиву с аттрибутами и пытаемся вытащить данные | |
// и положить в переменную, которую затем будем использовать | |
var utmParams = ''; | |
for (u of UTMS) { | |
utmParams += `${u}=${findGETParameterByName(u)}`; | |
if (u !== 'utm_term') utmParams += '|||'; | |
} | |
// формируем новую куку с необходимыми UTM метками | |
document.cookie = `bootcamp_utm_aggregated_data=${utmParams}; path=/; secure;`; | |
// callback-функция для прокидывания данных | |
// из формы в куки пользователя | |
function t396_onSuccess(form) { | |
// проверка на существование формы | |
if (!form) return; | |
if (form instanceof jQuery) form = form.get(0); | |
// обработка всех полей формы в Zero блоке | |
var obj = {}, inputs = form.elements; | |
Array.prototype.forEach.call(inputs, function (input) { | |
if (input.type === 'radio') { | |
// если это radio-кнопка, то забираем только отмеченное значение | |
if (input.checked) obj[input.name] = input.value; | |
} else { | |
obj[input.name] = input.value; | |
} | |
}); | |
// если в форме есть поле с номером телефона, то добавляем его | |
if (obj['phone'] !== undefined) { | |
// формируем куку с полями email и phone | |
document.cookie = `bootcamp_form_aggregated_data=email=${obj['email']}|||phone=${obj['phone'].replaceAll(/\s|\(|\)|\-/gi, '')}; path=/; secure;`; | |
} else { | |
// формируем куку только с полем email | |
document.cookie = `bootcamp_form_aggregated_data=email=${obj['email']}; path=/; secure;`; | |
} | |
// переадресация на страницу успеха | |
var successUrl = form.getAttribute('data-success-url'); | |
if (successUrl) window.location.href = successUrl; | |
} | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
// массив, в котором айдишники всех кнопок | |
var BUTTON_CLASS = 'uid'; | |
// массив, в котором лежат нужные нам названия UTM | |
var UTMS = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term']; | |
// массив, в котором лежат нужные названия полей из формы | |
var FIELDS = ['email', 'phone']; | |
// функция, которая вытаскивает значение cookie с указанным названием | |
function findCookieByName(cookie, name) { | |
var result = null, tmp = []; | |
var match = document.cookie.match(new RegExp(`(^| )${cookie}=([^;]+)`)); | |
if (match) { | |
match[2].split('|||').forEach(function (item) { | |
tmp = item.split('='); | |
if (tmp[0] === name) result = decodeURIComponent(tmp[1]); | |
}); | |
} | |
return result; | |
} | |
// создаем пустую строку | |
var utmParams = ''; | |
// бежим по массиву с UTM метками, пытаемся вытащить данные | |
// и положить в переменную, которую затем будем использовать | |
for (u of UTMS) { | |
utmParams += `&${u}=${findCookieByName('bootcamp_utm_aggregated_data', u)}`; | |
} | |
// бежим по массиву с полями из формы, пытаемся вытащить данные | |
// и положить в переменную, которую затем будем использовать | |
for (f of FIELDS) { | |
utmParams += `&${f}=${findCookieByName('bootcamp_form_aggregated_data', f)}`; | |
} | |
// очистка ссылки под кнопкой | |
var buttons = document.querySelectorAll(`.${BUTTON_CLASS} a`); | |
// формируем новый URL у всех кнопок с необходимыми UTM метками | |
for (b of buttons) { | |
var baseUrl = b.href.replaceAll('&', '&'); | |
b.href = `${baseUrl}${utmParams}`; | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment