Fadlan Fadlan - 6 months ago 29
jQuery Question

Google Analytics Event Not Working on Submit

I would like to send event when someone submits form on my website, but it's not working.

This is my form script:

$('.wicmsLeadSubmit').prop( 'disabled', false );

$('.wicmsLeadSubmit').click(function(){
var leadForm = $(this).closest('form');
var leadImie = $('.lead_imie', leadForm);
var leadTelefon = $('.lead_telefon', leadForm);
var leadEmail = $('.lead_email', leadForm);

if(leadImie.length){
if(leadImie.val()==''){
leadImie.attr('placeholder', 'Podaj imię');
return false;
}
}


if(leadEmail.length){
if(leadEmail.val() == ''){
leadEmail.attr('placeholder', 'Podaj email');
return false;
}
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if(reg.test(leadEmail.val()) == false) {
leadEmail.val('');
leadEmail.attr('placeholder', 'Podałeś błędny email');
return false;
}
}
ga('send', 'event', 'Submit', 'Modal Rejestracja', $('.reveal-modal.formularz').id());
ga('set', 'userId', leadEmail.val() );

$.ajax({
type: 'POST',
url: '/lead',
dataType: 'json',
data: {
lead_email: leadEmail.val(),
lead_imie: leadImie.val(),
lead_source: $(':hidden.lead_source', leadForm).val(),
lead_telefon: leadTelefon.val()
},
success: function(data){
if(data.error.exists==true){
$('.powiadomienie', leadForm).hide();
$('.blad', leadForm).show();
$('.blad', leadForm).append(' Tresc błedu: '+data.error.message);
$('.wicmsLeadSubmit', leadForm).prop( 'disabled', true );

}
else{

$('.wicmsLeadSubmit', leadForm).prop( 'disabled', true );
$('.powiadomienie', leadForm).show();
}
},
});

return false;
});


And this is what makes error: ga('send', 'event', 'Submit', 'Modal Rejestracja', $('.reveal-modal.formularz').id());

And when I try to look for errors there is only one:
$(...).id is not a function

Please help.

Answer

.id() is not a jQuery function, if you want the id attribute, do this:

ga('send', 'event', 'Submit', 'Modal Rejestracja', $('.reveal-modal.formularz').attr('id'));