Skip to main content

Forms, Leads and GA4 Reporting · Part 1: Form and lead tracking

Chapter 8.2 · 2 of 36 · 4 min

WordPress form plugins: Contact Form 7, WPForms, Elementor, Gravity Forms

If the client's site runs on WordPress, the form is almost certainly built with one of these four.

Free to read

If the client's site runs on WordPress, the form is almost certainly built with one of these four. Each has its own success signal, and they differ.

Contact Form 7

CF7 fires its own browser event, called wpcf7mailsent. It fires on the document, and only when the mail was sent successfully.

This is CF7's most reliable signal, better even than Element Visibility. In GTM, create a Custom HTML tag on All Pages with this code.

html
<script>
  document.addEventListener(
    'wpcf7mailsent',
    function (event) {
      window.dataLayer = window.dataLayer || [];
      window.dataLayer.push({
        event: 'form_submit_success',
        form_id: 'cf7-' + event.detail.contactFormId,
        form_name: 'contact',
      });
    },
    false
  );
</script>

Then in GTM you create a Custom Event trigger named form_submit_success, and attach your GA4 tag to it.

event.detail.contactFormId tells you which form was submitted. If the site has three forms, this one piece of code handles all three.

WPForms

When WPForms runs in AJAX mode, the success message container has the class .wpforms-confirmation-container-full. That is what I use in Element Visibility.

If AJAX is turned off in WPForms, the page reloads and #wpforms-confirmation is added to the URL. A Page View trigger will not work then, because GTM does not count a new Page View when only the hash changes. You need a History Change trigger instead.

Elementor Pro Forms

Elementor fires a submit_success jQuery event. If jQuery is on the site, you can capture it.

html
<script>
  if (window.jQuery) {
    jQuery(document).on('submit_success', function (event, response) {
      window.dataLayer = window.dataLayer || [];
      window.dataLayer.push({
        event: 'form_submit_success',
        form_id: 'elementor',
        form_name: 'contact',
      });
    });
  }
</script>

Elementor has another route, which I prefer. You can add a Webhook under the form's Actions After Submit, and by giving it a URL you get confirmation from the server. That is the work of Part 3.

Gravity Forms

Gravity Forms fires a gform_confirmation_loaded event in AJAX mode.

html
<script>
  if (window.jQuery) {
    jQuery(document).on('gform_confirmation_loaded', function (event, formId) {
      window.dataLayer = window.dataLayer || [];
      window.dataLayer.push({
        event: 'form_submit_success',
        form_id: 'gf-' + formId,
        form_name: 'contact',
      });
    });
  }
</script>

One name, four plugins

Notice that I kept the same event name in all four snippets: form_submit_success. If the plugin changes, or the client moves to a new form plugin, nothing in your GTM triggers, tags or GA4 reports needs to change. Only this listener code changes.

This follows on directly from the naming convention in book 1. Name the event after what happened, not after the site's technology.

Steps

  1. Find out which form plugin the client's site is running. Search the page source for wpcf7, wpforms, elementor or gform.
  2. Put the matching code from above into a Custom HTML tag in GTM, triggered on All Pages.
  3. Create a Custom Event trigger named form_submit_success.
  4. Turn on Preview mode and submit the form. Check whether form_submit_success appears in the list on the left.
  5. Go to the Data Layer tab and read both values, form_id and form_name.

What not to do

  • Do not leave all four listeners in place if the site has only one plugin. The ones you do not need will not break anything on screen, but unnecessary code builds up in the container, and six months later nobody will know which one is doing the work.
  • Do not deploy jQuery-dependent code without if (window.jQuery). If jQuery is missing, the Console will show jQuery is not defined, and all JavaScript after that point in the tag will stop.

Proof

In Preview mode, after you submit the form, the form_submit_success event appears, form_id and form_name are visible in the Data Layer tab, and submitting the form empty produces no event.

Want the full chapter list, or to jump to another chapter?

All chapters in the book