Google Analytics event tracking

I am trying to track when a user clicks on the submit form button and they send contact information. I have this code on the contact page:

<input type="submit" value="Send" class="btn big btn_submit_form" onClick="ga('send', 'event', { eventCategory: 'Contact', eventAction: 'Information Request', eventLabel: 'Contact Form'});">

I am using Universal Analytics.

In my Google Analytics account, I have the goal description Name as Contact Form Submit, and the Goal Type is Event. The event conditions are as follows:

  • Category -Equals to- Contact
  • Action -Equals to- Information Request
  • Label -Equals to- Contact Form
  • with the Value field being left empty.

    I have "Use the Event value as the Goal value for the conversion" set to yes.

    However, Google Analytics doesn't seem to be tracking the event. Any idea how to fix this?

    Thanks.


    The most likely problem here is that your page is being unloaded before the event hit has time to send. When you submit a form on a web page, most browsers will stop executing JavaScript and start loading whatever new page the form's action attribute is pointing to.

    The general workaround for this is to call preventDefault on the form's submit event, wait for the hit to successfully send to Google Analytics, and then manually re-trigger the form submit.

    Here's how that might look (note: I'm using jQuery for simplicity):

    $('#my-form').on('submit', function(event) {
      // Prevent the browser's default form submission action.
      event.preventDefault();
    
      ga('send', 'event', {
        eventCategory: 'Contact',
        eventAction: 'Information Request',
        eventLabel: 'Contact Form',
        hitCallback: function() {
          $('my-form').trigger('submit');
        }
      });
    });
    

    The key part in the above code is the hitCallback function, which gets invoked when GA returns success from the event hit beacon.


    Maybe try changing the event to the form submit, instead of the button click:

    <form onSubmit="ga('send', 'event', { eventCategory: 'Contact', eventAction: 'Information Request', eventLabel: 'Contact Form'});">
    

    The form can be submitted in ways other than the clicking the submit button (for instance pressing enter in an input field)

    Also, in my experience, the tracking will post correctly almost every time, even when you do not call preventDefault on the event.

    链接地址: http://www.djcxy.com/p/31186.html

    上一篇: 如果没有互联网,我打印时中止Google Analytics

    下一篇: Google Analytics事件跟踪