为什么我的ajax表单提交目标没有被跟踪?

时间:2014-07-14 16:36:56

标签: javascript jquery google-analytics

我正在尝试在有效的ajax表单提交后提交目标事件。

我正在使用analytics.js并且我已经创建了目标,但这是我第一次尝试目标,所以我可能会遗漏一些关键的东西。 表单会正确验证和提交,而不是跟踪Google Analytics中的目标。

我的整个表单功能如下,但问题区域必须是:

// custom event for google analyics
$(document).on('formSuccess', function() {
    ga('send','event','form','submission');
})

我的目标,因为它在GA中的设置是:

Category:'form'    
Action: 'submission'    
Label: blank    
Value: blank
Use the Event value as the Goal Value for the conversion: Yes

当我验证时,我得到0%的转换。甚至可以验证ajax表格吗?

我早期有'formSuccess'的eventListener,如果response.success为true,我在ajax.success中触发事件。其他一切工作正常,顺便说一句。


REA.mailForm = function() {
    var formId = '#tour-form';

    // custom event for google analyics
    $(document).on('formSuccess', function() {
        ga('send','event','form','submission');
    })

    $(formId).submit(function(e) {

        e.preventDefault();

        var firstName = $(formId+' #first-name').val();
        var lastName = $(formId+' #last-name').val();
        var phone = $(formId+' #phone').val();
        var email = $(formId+' #email').val();
        var datePicker = $(formId+' #date-picker').val();
        var timePicker = $(formId+' #time-picker').val();
        var referral = $(formId+' #referral option:selected' ).val();
        var formData = {};
        var alertElement = $(formId+' .alert');

        alertElement.removeClass('alert-success alert-danger');

        var alertClass = '';
        var alertText = '';
        var showAlert = function(alertClass, alertText) {
            alertElement
            .addClass(alertClass)
            .show()
            .find('.alert-text').text(alertText);

        };

        // super basic validation
        // if((firstName && lastName && phone && email) !== '') {
            // prepare each form field for php form processing
            formData.firstName = firstName;
            formData.lastName = lastName;
            formData.phone = phone;
            formData.email = email;
            formData.datePicker = datePicker;
            formData.timePicker = timePicker;
            formData.referral = referral;
            formData.website = window.location.pathname;

            // send data and examine response
            $.ajax({
                type: 'POST',
                url: 'process.php',
                data: formData,
                success: function(data) {
                    var response = JSON.parse(data);
                    if(response.failure){
                        alertClass = 'alert-danger';

                        for (var key in response) {
                            alertText += response[key]+'\n';
                        }

                    } else if(response.success) {
                        alertClass = 'alert-success';
                        alertText = response.success;
                        REA.clearForm(formId);
                        // custom event for analytics
                        $(document).trigger('formSuccess');
                    }

                    showAlert(alertClass, alertText);
                },
                error: function() {
                    showAlert('alert-danger', 'Sorry. Something went wrong. Your message was not received.');
                }
            });

            var timer = setTimeout(function() {
                clearTimeout(timer);
                alertElement
                .removeClass('alert-success alert-danger')
                .hide()
                .find('.alert-text').text('');
            }, 10000);

            return false;
        // }
    });
}

1 个答案:

答案 0 :(得分:2)

事实证明,只需要24小时就可以收集数据并将其填充到分析中。从来没有任何错误。