Skip to content

Instantly share code, notes, and snippets.

@arnklint
Last active December 18, 2017 18:57
Show Gist options
  • Save arnklint/4247137 to your computer and use it in GitHub Desktop.
Save arnklint/4247137 to your computer and use it in GitHub Desktop.
Track Form Abandonment in Google Analytics in five rows of javascript

Put this code on the page where the form you want to track resides. Some other examples are currently passed around the web with varying quality. This is one that will work as long as your form tag has an id= or name attribute.

You don´t have to change this code to be able to track form abandonment in your shopping cart, order form or whatever form you want.

This sends events to Google Analytics when a user focuses somewhere not in a field after having focused on a input field. You won´t know for how long users focused on respective fields, or the actual conversion rate in the form using this, but it might be a start.

A tool that provides more insight both over time and per field, is Form Analytics wich helps you optimize your online forms. For instance, it measures dropout rate, average field input length, conversion rate and average time per field. All which provides great insights in the most overlooked, yet important part of you site.

Anyway, here´s the code:

$(function(){
  $('form input').blur(function( e ){
    var me = $(this), form = me.parents('form');
    _gaq.push(['_trackEvent', (me.attr('name') || me.attr('id')), (me.val().length > 0 ? 'filled' : 'empty'), $(this).attr('name')]);
  });
});
@koenpunt
Copy link

I suggest using

$('form :input')

This will capture selects and textareas as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment