// source --> https://marvitsrl.net/wp-content/plugins/iimage-cf7-on-sent-ok/js/wpcf7mailsent.js?ver=3f7591bfd85aabec384fef4d4517cd70 

document.addEventListener('wpcf7mailsent', function (event) {
    var url = ajax_data.ajaxurl;
    var http = new XMLHttpRequest();

    var params = "action=check_cf7&id=" + event.detail.contactFormId;
    var id_form = event.detail.contactFormId;
    //console.log(event.detail);

    http.open("POST", url, true);

//Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    http.onreadystatechange = function () {//Call a function when the state changes.
        if (http.readyState == 4 && http.status == 200) {
            var cf7 = JSON.parse(http.responseText);

            if (cf7) {

                try {
                    dataLayer.push({'event': 'invio-form', 'id-form': id_form});
                    console.log('evento inviato GTM');

                } catch (e) {
                    console.log(e.message);
                }

                if (typeof ga == 'function') {
                    console.log('evento inviato GA');
                    try {
                        ga('send', 'event', 'Form', 'invio richiesta', cf7.post_title);
                    } catch (e) {
                        console.log(e.message)
                    }
                }
                if (typeof __gaTracker == 'function') {
                    console.log('evento inviato __ga');
                    try {
                        __gaTracker('send', 'event', 'Form', 'invio richiesta', cf7.post_title);
                    } catch (e) {
                        console.log(e.message)
                    }
                }



            }

            //document.getElementById('contactform').style.display = 'none';
        }
    };
    http.send(params);
}, false);