vdesignu.

Tools menu

View all categories

Free tool

dataLayer Builder (Forms)

Generate dataLayer code for form tracking including submissions, field interactions, and errors.

#formtracking #datalayerforms #leadtracking #formanalytics

The submit event fires when the browser submits the form, not when your server accepts it. For AJAX forms, push form_submit from your success callback instead.

dataLayer snippetjavascript
// Form tracking for #contact-form. Paste before </body> or in a GTM Custom HTML tag.

var formStarted = false;
document.addEventListener('focusin', function (e) {
  var form = e.target.form;
  if (formStarted || !form || !form.matches('#contact-form')) return;
  formStarted = true;
  window.dataLayer = window.dataLayer || [];
  window.dataLayer.push({ event: 'form_start', form_id: form.id, form_name: 'Contact form' });
});

document.addEventListener('submit', function (e) {
  var form = e.target;
  if (!form.matches('#contact-form')) return;
  window.dataLayer = window.dataLayer || [];
  window.dataLayer.push({ event: 'form_submit', form_id: form.id, form_name: 'Contact form', form_destination: form.action });
});

document.addEventListener('invalid', function (e) {
  var form = e.target.form;
  if (!form || !form.matches('#contact-form')) return;
  window.dataLayer = window.dataLayer || [];
  window.dataLayer.push({ event: 'form_error', form_id: form.id, form_name: 'Contact form', form_field: e.target.name, error_message: e.target.validationMessage });
}, true);

How to use this tool

Follow these steps to get the most out of the dataLayer Builder (Forms).

01

Enter your accurate data in the provided form fields.

02

Review all configuration options and settings.

03

Click the generate or proceed button to execute.

04

Copy, download, or implement the professional output.

In depth

Precision Form Tracking

Our dataLayer builder writes listeners for form start (first field focused), submit and browser validation errors. The submit event fires when the browser submits the form; for AJAX forms, push it from your success callback so failed submissions aren't counted.

GTM Integration

Use the events as Google Tag Manager triggers for GA4 key events and Google Ads conversions.

Want it done for you?

Tracking is what makes ad spend accountable. We run Google Ads with conversion tracking you can check yourself.

Google Ads & PPC Management

More in Tracking & Analytics

Related tools