D
D
Dunno20182018-10-02 21:03:04
JavaScript
Dunno2018, 2018-10-02 21:03:04

Yandex Metrica - how to set up a goal for submitting a form?

Hello!
There is a form:

<form method="post" action="./handlers/contact-square.php" id="first-form" onsubmit="yaCounter50578219.reachGoal('first-form'); return true;">

The target fires when the submit button is clicked, even if the form is not filled out. And it is necessary that it worked only when submitting the form. Therefore, I need to wrap this code
yaCounter50578219.reachGoal('first-form'); return true;
into the form handler at the moment when the actual submission occurs. The question is how to do it? Handler code:
$('#first-form').validate({
        debug: false,
        onfocusout: true,
        onKeyUp: true,
        rules: {
            chosen_brand: {
                required: true
            },
            number: {
                required: true,
                regexp: /^\+\d{1}\(\d{3}\)\d{3}-\d{2}-\d{2}$/
            }
        },
        messages: {
            chosen_brand: {
                required: "Введите сообщение"
            },
            number: {
                required: "Введите телефон",
                regexp: "Некорректно"
            }
        },
        wrapper: "div",
        errorPlacement: function(error, element) {
            if(element.siblings('.errors_block').length>0){
                element.siblings('.errors_block').html(error[0].textContent);
            }else{
                element.after('<span class="errors_block">'+error[0].textContent+'</span>');
            }
        },
        success: function(element) {                       
            if(element.siblings('.errors_block').length>0){
                element.siblings('.errors_block').remove();               
            }
        }
    });

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2018-10-02
@apisklov

use ajax . for successful sending, hang up the goal.

$.ajax({
        url: url,
        type: 'POST',
        data: data,
        success: function (data) {
          yaCounter50578219.reachGoal('first-form');
        },
        error: function (data) {
          console.log('Ошибка: ' + data);
        }
});

A
Alexey, 2018-10-03
@skygliderus

It is possible in another way.
The submit button's state is :disable by default. Change of state when moving the focus to the last field and entering a certain number of characters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question