Ripwinder
Ripwinder

Reputation: 210

AJAX form submission and Google Analytics

I am trying to track an AJAX form submission in Google Analytics. I have tried using the following code to create a page view in GA to track goals, however the page view is not tracking.

<script type="text/JavaScript">

$("#template-contactform").validate({
    submitHandler: function(form) {
        $('.form-process').fadeIn();
        $(form).ajaxSubmit({
            target: '#contact-form-result',
            success: function() {
                $('.form-process').fadeOut();
                $('#template-contactform').find('.sm-form-control').val('');
                $('#contact-form-result').attr('data-notify-msg', $('#contact-form-result').html()).html('');
                SEMICOLON.widget.notifications($('#contact-form-result'));
                ga('send', {
                    'hitType' : 'pageview',
                    'page' : '/contact-us-success' // Virtual page (aka, does not actually exist) that you can now track in GA Goals as a destination page.
                });
            }
        });
    }
});
</script>

UPDATE:

I have resolved this by using Event Tracking. I used the code below in the submit button and created a new goal tracking the event in GA.

onClick="ga('send', 'event', { eventCategory: 'Contact', eventAction: 'ContactRequest'});

Upvotes: 0

Views: 4322

Answers (2)

Ripwinder
Ripwinder

Reputation: 210

I found the easiest solution for me was to include the below code in the submit button of my form:

onClick="ga('send', 'event', { eventCategory: 'Contact', eventAction: 'ContactRequest'});

In Google Analytics do the following:

Click on Admin. Under the View list, click on Goals Click on the New Goal button, click on the Custom radio button and then click on the Next step button. Name the goal and select the Event radio button. Populate all of the relevant goal details:

Category: Contact

Action: ContactRequest

Label: Optional

Value: Optional

Click the Create Goal button. You can now track form submissions in conversions.

Keep in mind this is tracking every click, so if you have mandatory fields and a user forgets and is prompted to complete the field, this will still register. For my use case its fine, but another solution would have to be sought if this is not the case for your forms.

Upvotes: 2

natchkebiailia
natchkebiailia

Reputation: 631

ga('send', 'pageview', {
  'page': '/contact-us-success',
  'hitCallback': function() {
     console.log('ga success')
  }
});

Upvotes: -1

Related Questions