How Do You Get Form Tracking Code?

You do not need to write a form listener from scratch. Here is where to get tested code for almost any form platform, and how to wire it into GTM.

Short answer: you almost never need to write it yourself. Someone has already written and tested a listener for your form platform, and you can copy it.

This post shows you where to get that code, how to install it in Google Tag Manager, and how to turn the event it produces into conversions in GA4, Google Ads and Meta.

What is form tracking?

Form tracking means knowing when someone successfully submitted a form on your site, and being able to act on it. Fire a conversion, build an audience, add the person to a report.

It sounds trivial. It is not, because of one thing: most modern forms never load a new page.

Why it is harder than it looks

Old forms posted to a server and loaded a thank you page. You tracked a page view on that URL and you were done.

Modern forms submit over AJAX. The page never changes. The form is swapped for a success message, the URL stays the same, and every page based tracking method sees nothing at all.

Worse, a lot of form tools render the form inside an iframe on a different domain. Your JavaScript cannot see inside it, so even listening for a click on the submit button does not work.

What an event listener actually does

An event listener is a small piece of JavaScript that waits for the form tool to announce that something happened, then pushes a message into the GTM data layer.

Every serious form platform announces its own events. They just do it in their own way:

  • A DOM event. Contact Form 7 fires wpcf7mailsent on the document.

  • A jQuery event. Gravity Forms, Ninja Forms and Elementor all use this.

  • A postMessage. HubSpot, Typeform, Tally and Calendly send a message from the iframe to the parent page.

The listener’s job is to catch that announcement and translate it into something GTM understands. That is all it does.

Where to get the code

The best public library of these listeners is DumbData. They maintain tested listeners for around 90 tools, including almost every form platform you are likely to use.

Use theirs rather than writing your own. Not because writing one is hard, but because theirs have been used on thousands of sites and already handle the awkward cases you will not think of until they break.

The process is:

  1. Open the DumbData listeners page.

  2. Find your form platform in the list.

  3. Copy the code exactly as it is.

  4. Paste it into a Custom HTML tag in GTM.

That is genuinely it. The rest of this post is what to do with the event once it exists.

How to install the listener in GTM

  1. In Google Tag Manager, create a new tag and choose Custom HTML.

  2. Paste the listener code, including the script tags.

  3. Set the trigger. All Pages works, and if the form only exists on a few pages, limit it to those.

  4. Tick Support document.write only if the listener needs it. Almost none do.

  5. Save, then Preview.

One detail that catches people: several listeners use jQuery. On WordPress that is usually already loaded. On a custom build it might not be. If the listener starts with jQuery and nothing happens, that is the first thing to check.

How to create the Custom Event trigger

The listener pushes an event with a name. You need a trigger that listens for that exact name.

  1. In GTM, create a new trigger and choose Custom Event.

  2. Type the event name exactly as it appears in the listener code. It is case sensitive.

  3. Leave it on All Custom Events unless you want to filter by form ID.

  4. Save.

If you want to track one specific form rather than all of them, add a condition on the form ID variable you are about to create.

How to create Data Layer Variables

Most listeners push more than just the event name. They also push a form ID, sometimes the submitted values, sometimes a step reference.

To use any of that in a tag, you need a Data Layer Variable:

  1. In GTM, create a new variable and choose Data Layer Variable.

  2. Set the name to exactly the key in the data layer push, for example formID or hs-form-guid.

  3. Leave version 2 selected.

  4. Save with a clear name like DLV Form ID.

Now you can send the form ID to GA4 as an event parameter, or use it to decide which conversion to fire.

How to send the event to GA4

  1. Create a GA4 Event tag.

  2. Set the event name to something clean and consistent, such as generate_lead.

  3. Add event parameters: form_id from your Data Layer Variable, and form_name if you have it.

  4. Set the trigger to your Custom Event trigger.

  5. Save.

Use one event name across every form and separate them with parameters. One generate_lead event with a form_id parameter is far more useful than six differently named events.

How to send conversions to Google Ads

  1. In Google Ads, create a conversion action with source Website, and note the conversion ID and label.

  2. In GTM, create a Google Ads Conversion Tracking tag with those values.

  3. Set the trigger to your Custom Event trigger.

  4. Add a conversion value if you have one worth sending.

  5. Turn on Enhanced Conversions and supply the hashed email if the form collects one.

If the real outcome happens later, in a CRM, this form submission is the start of the story rather than the end of it. That is a separate job: tracking qualified leads, not form fills.

How to use the event with Meta and other platforms

The same Custom Event trigger fires any tag you like. That is the point of routing everything through the data layer.

  • Meta. A Meta Pixel tag with the Lead standard event.

  • Mixpanel, Piwik PRO, Amplitude. Their GTM templates all accept a custom event trigger.

  • LinkedIn, Microsoft, TikTok. Same pattern with their own conversion tags.

You write the listener once and every platform reads from it. Adding a new platform later is one tag, not another listener.

How to test it

  1. Open GTM Preview and load the page with the form.

  2. Submit the form for real. Do not simulate it.

  3. Check the event appears in the left hand event stream with the exact name you expect.

  4. Click the event and open the Data Layer tab to confirm the form ID and any other values are there.

  5. Check your tags fired, and only once.

  6. Check GA4 realtime and, the next day, Google Ads.

Submit at least twice. A listener that fires once and then stops is a real failure mode and you will only see it on the second attempt.

Common issues

  • Nothing fires at all. Usually jQuery is missing, or the listener loaded after the form did. Move the trigger earlier.

  • It fires twice. The listener is installed in two places, often once in GTM and once hardcoded in the theme.

  • It fires on failed submissions. You are listening for a click or a submit rather than a success. Check which event the listener actually uses.

  • The form ID is undefined. The Data Layer Variable name does not match the key in the push. They are case sensitive.

  • It works in preview and not live. The container was not published, or a consent setting is blocking the tag.

Privacy considerations

Some listeners can capture the values people typed into the form. That is genuinely useful and it is also the part to be careful with.

Three rules that keep you out of trouble:

  • Never send names, emails, phone numbers or free text into GA4. It is against Google’s terms and it is the most common way sites end up with personal data where it should not be.

  • If you send customer data to an ad platform for matching, hash it first and only send what that platform expects.

  • Gate the tags behind marketing consent in your consent tool, not behind a hand rolled check: Google Consent Mode v2 in GTM

Capturing which form was submitted is low risk. Capturing what was typed into it is a decision to make deliberately.

Final thoughts

Form tracking has a reputation for being fiddly, and most of that reputation comes from people writing their own listeners badly.

Take the tested code, install it once, trigger everything from the data layer event, and it stops being a recurring problem.

Platform specific guides, each using the DumbData listener for that tool:

Need this built properly?

Form tracking is simple once the events are real events. I set up and repair tracking for ecommerce stores and lead generation businesses, so the numbers in your ad accounts match the money in your bank account.

Have a look at what I do, or book a call and we can go through your setup together.

Need accurate tracking?

Its Free

500+ Happy Clients ⭐️

Keep Reading

See exactly where you’re wasting ad spend and how better tracking can double your sales.

See exactly where you’re wasting ad spend and how better tracking can double your sales.

Tracking in 3 Hours

Tracking in 3 Hours

I Manage Everything

I Manage Everything

24/7 Expert Support

24/7 Expert Support

TESTIMONIALS

Trusted by 500+ satisfied clients

Discover how we’ve driven growth and innovation.

500+ Tracking

500+ Tracking

Adélaïde De Vos

3,922 followers

I had the pleasure to work with Abdul and I can say that he exceeded my expectations. He was professional, patient, and clear in his explanations and committed to client satisfaction. If you're looking for someone who is not only highly skilled but also reliable and committed to client satisfaction, I highly recommend Kayium!

Dean Watson

1,030 followers

Kayium setup our tracking for Google Ads and Meta Ads, which helped us get 20%+ more conversions on the same ad spend. He didn’t just handle the technical setup,he also took the time to explain why it works and how it improves results. If you’re looking for someone skilled, reliable, and committed to delivering real results, I highly recommend him!

David Friend

CEO: Profitable Sites

Kayium set up accurate tracking that helped us scale ads fast and boost conversions by over 25%. He gave us clear insight into which ads drive revenue.

Lalita Sundari

7,924 followers

I've worked with Kayium; his strategic mindset and expertise in GTM, GA4, Pixel & Server Side Tracking, are invaluable! I highly recommend Kayium!