{"id":4603,"date":"2020-08-19T09:00:00","date_gmt":"2020-08-19T06:00:00","guid":{"rendered":"https:\/\/trackingchef.com\/?p=4603"},"modified":"2023-07-02T11:50:05","modified_gmt":"2023-07-02T08:50:05","slug":"form-abandonment-tracking","status":"publish","type":"post","link":"https:\/\/trackingchef.com\/google-analytics\/form-abandonment-tracking\/","title":{"rendered":"Form abandonment tracking with (and w\/o) Google Tag Manager"},"content":{"rendered":"\n

A common requirement in measurement plans set by Marketing or Product teams is the ability to track form abandonment. With many of the teams mentioned above stressing over bringing in more and more leads, the ability to analyze and address where and why users drop off is important.<\/p>\n\n\n\n

While there some tools that can measure a form’s conversion rate (e.g. Hubspot’s built feature<\/a>) and even analyze specific fields (e.g. Hotjar Form Analysis tool<\/a>), measuring this in Google Analytics can come in handy for in-depth analysis.<\/p>\n\n\n\n

The output of measurement described below is this table of events in Google Analytics that can be used to analyze the drop-off points of users.<\/p>\n\n\n

\n
\"\"
Form abandonment tracking in Google Analytics<\/figcaption><\/figure><\/div>\n\n\n

Another classic use case of this data is creating Remarketing audience from these users that had expressed a clear interest in your product or service and only need that extra nudge. These can also serve as a great seed for and Lookalike audiences too.<\/p>\n\n\n\n

Since this triggers a generic event, it can be used to create these audiences in any ad platform aside from Google Analytics\/Ads, for example on Facebook Ads or Taboola.<\/p>\n\n\n\n

Form abandonment tracking with Google Tag Manager<\/h2>\n\n\n\n

The simplest way I usually go about this is with Google Tag Manager (GTM). If you’re using GTM4WP, this is already baked in as an event (will appear in the Data Layer as gtm4wp.formElementEnter<\/em> & gtm4wp.formElementLeave<\/em>).<\/p>\n\n\n\n

The method described below is based off of the Generic Event Tracking<\/a> mechanism I’ve previously posted about. Using this generic tracking, adding additional event such as the form abandonment is a breeze.<\/p>\n\n\n\n

Adding the Custom HTML tag<\/h3>\n\n\n\n

The first step to handling form abandonment is being able to identify to which point has a user gotten within a specific form. To achieve this we will use a simple jQuery script. <\/p>\n\n\n\n

\n

Important:
<\/strong>This assumes you already have jQuery active on your site (WordPress sites ship with it by default). If you don’t use jQuery, see the Vanilla JS solution below.<\/p>\n<\/blockquote>\n\n\n\n

The purpose of this script is to capture a change in the value of a form field, and send the label of that field to GTM.<\/p>\n\n\n\n

We will add this script as a Custom HTML tag in GTM. You can set the tag to trigger on any page or only on the pages where there are forms (e.g. only the Contact Us page).<\/p>\n\n\n\n

<script>\njQuery(\"form input, form select, form textarea\").change(function() {\n    dataLayer.push({\n        'event': 'form interaction',\n        'form_field': jQuery(this).attr('placeholder')\n    })\n});\n<\/script><\/code><\/pre>\n\n\n\n

The output of this script will be an event sent to Google Analytics (using the Generic Event tag in GTM). The event’s name is ‘form interaction’ and the event’s parameter ‘form_field’ will record the field interacted with.<\/p>\n\n\n

\n
\"\"
The Custom HTML Tag in GTM<\/figcaption><\/figure><\/div>\n\n\n

In the example above, the field will be identified by its placeholder text. You can extract the field’s label attribute by replacing the placeholder<\/em> value with label<\/em>.<\/p>\n\n\n\n

jQuery(this).attr('label')<\/code><\/pre>\n\n\n\n

Elementor forms <\/h4>\n\n\n\n

In Elementor forms, this value is also referenced as name<\/em> or id<\/em>. You can also grab the field’s label or the form’s name with a minor alteration:<\/p>\n\n\n\n

'form_field_label': jQuery(this).parents('div').children('label').text(),\n'form_field_id': jQuery(this).parents('form').attr('name')<\/code><\/pre>\n\n\n\n

You can also check out my guide on Bulletproof Elementor form tracking with GTM<\/a>.<\/p>\n\n\n\n

Contact Form 7 forms <\/h4>\n\n\n\n

In CF7 forms, this value is also referenced as id<\/em>. You can also grab the field’s label or the form’s ID with a minor alteration:<\/p>\n\n\n\n

'form_field_label': jQuery(this).parents('label').text(),\n'form_field_id': jQuery(this).parents('.wpcf7').attr('id')<\/code><\/pre>\n\n\n\n

You can also check out my guide on Bulletproof Contact Form 7 tracking with GTM<\/a>.<\/p>\n\n\n\n

Gravity forms<\/h4>\n\n\n\n

In Gravity forms, this value is also referenced as name<\/em> or id<\/em>. You can also grab the field’s label with a minor alteration:<\/p>\n\n\n\n

'form_field_label': jQuery(this).parents('li').children('label').text(),\n'form_field_id': jQuery(this).parents('.gform_wrapper').attr('id')<\/code><\/pre>\n\n\n\n

Hubspot forms <\/h4>\n\n\n\n

In Hubspot forms, this value is also referenced as name<\/em>. ou can also grab the field’s label with a minor alteration:<\/p>\n\n\n\n

jQuery(this).parents('div').eq(1).children('label').text()<\/code><\/pre>\n\n\n\n

You can also check out my guide on Hubspot form tracking with (and w\/o) Google Tag Manager<\/a>.<\/p>\n\n\n\n

Form abandonment tracking w\/o Google Tag Manager<\/h2>\n\n\n\n

For those of you who don’t use GTM (shame on you!), there’s also a solution available. It utilizes the same jQuery and logic (see above for specific platforms) but triggers the event directly to Google Analytics via the gtag <\/em>method.<\/p>\n\n\n\n

Add this snippet to any page that has a form you want to track.<\/p>\n\n\n\n

<script>\njQuery(\"form input, form select, form textarea\").change(function() {\n        gtag('event', 'form interaction', {\n        'form_field': jQuery(this).attr('placeholder')\n        });\n});\n<\/script><\/code><\/pre>\n\n\n\n

You can also create a similar tracker for Facebook pixel events by altering the script (both GA and FB events can sit under a single script):<\/p>\n\n\n\n

<script>\njQuery(\"form input, form select, form textarea\").change(function() {\nfbq('track', 'ViewContent', {\n  content_name: jQuery(this).attr('placeholder')\n });\n});\n<\/script><\/code><\/pre>\n\n\n\n

Vanilla JavaScript (w\/o jQuery)<\/h2>\n\n\n\n

in case your site doesn’t use jQuery, you can still use a simpler version of this solution. Since JS lacks some of the methods that are trivial in jQuery (without complex setup), we can only grab the immediate value of the field’s name\/label.<\/p>\n\n\n\n

If you’re using Google Tag Manager, simply use this snippet. You can change the value of form<\/em> to capture a specific form by class or id (e.g. .class or #id). You can also change the value of the attribute you’re <\/p>\n\n\n\n

<script>\ndocument.querySelector('form').addEventListener('change', function(e) {\n    dataLayer.push({\n        'event': 'form interaction',\n        'form_field': e['target'].getAttribute('name')\n    });\n});\n<\/script><\/code><\/pre>\n\n\n\n

If you’re not using Google Tag Manager, you can use this snippet to trigger the event into Google Analytics.<\/p>\n\n\n\n

<script>\ndocument.querySelector('form').addEventListener('change', function(e) {\n        gtag('event', 'form interaction', {\n        'form_field': e['target'].getAttribute('name')\n        });\n});\n<\/script><\/code><\/pre>\n\n\n\n

Testing your selectors<\/h2>\n\n\n\n

If you want to test that you have the correct selectors, you can use this script. Simply paste it in your browser’s Console (click F-12 and navigate to the Console tab) and then interacting with any form on that page (the function resets on page navigation).<\/p>\n\n\n\n

jQuery<\/h3>\n\n\n\n
jQuery(\"form input, form select, form textarea\").change(function() {\n        alert('The field value is '+jQuery(this).attr('placeholder'))\n});<\/code><\/pre>\n\n\n\n

Vanilla JavaScript<\/h3>\n\n\n\n
document.querySelector('form').addEventListener('change', function(e) {\n        alert('The field value is ' + e['target'].getAttribute('name'))\n});<\/code><\/pre>\n\n\n\n

Caveats<\/h2>\n\n\n\n

While this approach is pretty simple, it does bring some challenges.<\/p>\n\n\n\n

    \n
  1. This method doesn’t take into account the form’s ID. If you have one for per page, that’s no big deal (as GA events are tied to the page the user triggered them on). If you have multiple forms, e.g. a contact us form and newsletter footer , these can be difficult to analyze separately.
    In that case my suggestion is simply to add the form’s ID to the Event Label field (requires additional code, see above).<\/li>\n\n\n\n
  2. Fields can be tracked multiple times – if a user edits a field multiple times, this will be recorded (not necessarily a bad thing)<\/li>\n\n\n\n
  3. Browser autocomplete will trigger the fields impacted (again, not a bad thing)<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"

    A simple guide to tracking from abandonment events for improving conversion rates and campaign targeting.<\/p>\n","protected":false},"author":1,"featured_media":4629,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2,39,42],"tags":[49,43,48],"_links":{"self":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/4603"}],"collection":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/comments?post=4603"}],"version-history":[{"count":18,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/4603\/revisions"}],"predecessor-version":[{"id":5665,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/4603\/revisions\/5665"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/media\/4629"}],"wp:attachment":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/media?parent=4603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/categories?post=4603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/tags?post=4603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}