Skip to content

Instantly share code, notes, and snippets.

@dvygolov
Last active December 4, 2020 11:49
Show Gist options
  • Save dvygolov/5dd705e51994b154e2e124ae94e04a40 to your computer and use it in GitHub Desktop.
Save dvygolov/5dd705e51994b154e2e124ae94e04a40 to your computer and use it in GitHub Desktop.
Добавляет событие Lead по нажатию кнопки на форме. При этом проверяет, чтобы были заполнены имя и телефон.
<script>
function addbuttonpixel(){
var buttons=document.querySelectorAll("form button");
buttons.forEach(function(button){
button.addEventListener('click', function() {
firepixel(button);
});
});
var submits=document.querySelectorAll("form input[type='submit']");
submits.forEach(function(submit){
submit.addEventListener('click', function() {
firepixel(submit);
});
});
}
function firepixel(element){
console.log("Started pixel fire..");
var node = element;
while (node.nodeName != "FORM" && node.parentNode) {
node = node.parentNode;
}
var hasName=false;
var hasPhone=false;
var inputs=node.getElementsByTagName('input');
for(i=0;i<inputs.length;i++){
var input=inputs[i];
if (input.name=="name" && input.value!==''){
hasName=true;
}
if (input.name=="phone" && input.value!==''){
hasPhone=true;
}
}
if (hasName&&hasPhone)
fbq('track', 'Lead');
}
window.addEventListener('DOMContentLoaded', addbuttonpixel, false);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment