Tutorials

How to capture UTM parameters in form submissions

November 19, 20267 min read
Thin white lines forming a flowing ribbon on a black background

You send a newsletter, post a link on LinkedIn and run a small ad, all pointing at the same landing page. A week later three enquiries arrive, and you can't tell which of those three channels sent them.

To capture UTM parameters in a form, add one hidden input for each parameter you care about, then use a few lines of JavaScript to copy the values from the page URL into those inputs when the page loads. The values travel with the form like any other field, so each submission records the campaign that brought that person in. UTM parameters are the utm_source, utm_medium and utm_campaign tags you add to the end of a link so you can tell where a visit came from.

This post covers attributing individual leads. If you want to count conversions in Google Analytics instead, that's a separate setup with its own steps, and we've kept it out of this one.

The example we'll use

The business below is made up. Imagine a freelancer who sells accessibility audits and runs one landing page at https://example.com/audit. She shares it three ways, and each link carries tags:

https://example.com/audit?utm_source=newsletter&utm_medium=email&utm_campaign=nov_audit_offer

Here is what each part means:

ParameterAnswersValue in the example
utm_sourceWhere did the visitor come from?newsletter
utm_mediumWhat kind of channel was it?email
utm_campaignWhich campaign or offer was it for?nov_audit_offer

Those three are enough for most small sites. utm_term (a paid search keyword) and utm_content (which link or ad variant) exist too, and you can add them the same way, but only capture the ones you actually fill in when you tag links. An input that is always empty only adds clutter.

The form and the script

You need an endpoint first, which is the URL a form posts to. If you haven't set one up, our guide to sending an HTML contact form to email walks through creating a form and pasting in its URL. Then add the three hidden inputs.

<form action="https://formtorch.com/f/YOUR_FORM_ID" method="POST">
  <label for="email">Email</label>
  <input id="email" name="email" type="email" required />

  <label for="message">What should we look at?</label>
  <textarea id="message" name="message" required></textarea>

  <input type="hidden" name="utm_source" />
  <input type="hidden" name="utm_medium" />
  <input type="hidden" name="utm_campaign" />

  <button type="submit">Request an audit</button>
</form>

<script>
  const params = new URLSearchParams(window.location.search);

  ["utm_source", "utm_medium", "utm_campaign"].forEach((name) => {
    const input = document.querySelector(`input[name="${name}"]`);
    if (input) input.value = params.get(name) || "";
  });
</script>

Replace YOUR_FORM_ID with your own. Three details matter here:

  • The name attributes are the field names you'll see later. They must match what the script looks up, so utm_source in the HTML and utm_source in the array.
  • The script sits after the form. It runs while the page loads, and the inputs have to exist by then. If you move it to the <head>, wrap it in a DOMContentLoaded listener.
  • params.get(name) || "" handles a missing tag. URLSearchParams returns null when a parameter isn't in the URL, and the fallback leaves the field empty instead of writing the text "null" into it.

Don't start the field names with an underscore. Names beginning with _ are reserved for Formtorch's own settings, such as _redirect, and aren't saved as regular submission fields. The forms docs list them, and utm_source is an ordinary field name that gets saved like email does.

What a tagged visit looks like

When a visitor opens the tagged link and sends the form, the browser posts every field in the form, including the hidden ones. The submission arrives with utm_source, utm_medium and utm_campaign stored next to email and message. Here is what a tagged submission looks like:

FieldValue
emailsam@example.com
messageCan you audit our checkout flow?
utm_sourcenewsletter
utm_mediumemail
utm_campaignnov_audit_offer

To read them, open the form in the Formtorch dashboard, go to Submissions, and open the entry. If email notifications are on, the fields appear in the notification too.

What happens without UTM parameters

Plenty of visitors arrive with no tags, because they typed the address, followed an old link or came from search. In that case params.get() finds nothing, and the script sets each hidden input to an empty string.

Our submission code keeps empty values from a standard HTML form post, so an untagged submission still carries utm_source, utm_medium and utm_campaign as fields with empty values, rather than leaving them out. In a spreadsheet export, that means the three columns exist and the cells for that row are blank. A blank cell is useful information, because it tells you the lead didn't arrive through a tagged link.

If you want to tell "organic or direct" apart from "tag was lost", set a default in the script instead, for example params.get(name) || "none". That puts a visible value in every row, and it makes filtering in a spreadsheet easier.

What the script can and can't see

This approach reads the address of the page that contains the form, and nothing else. Two limits follow from that.

Another page first. If a visitor lands on /audit?utm_source=newsletter, clicks through to /contact and fills in the form there, the tags are no longer in the address bar, so the script finds nothing. The simplest fix is to put the form on the landing page itself. Carrying values between pages needs storage such as sessionStorage or cookies, and cookies bring consent rules that depend on where your visitors are. We've left that out on purpose, and if you go that way, check what applies to your site first.

Editable values. Anyone can edit a hidden input in their browser's developer tools, or type any text into the URL. Treat these values as hints about where a lead came from, not as proof, and don't use them to decide anything important such as a discount or access.

Check the data in a CSV

Once a few tagged and untagged submissions are in, export them. The utm_* fields show up as columns after the metadata columns, in alphabetical order with your other fields. Our guide to exporting form submissions to CSV covers the steps and the file layout.

One detail from that guide applies here. A value that begins with =, +, - or @ gets a leading apostrophe in the export, so a spreadsheet can't run it as a formula. Because anyone can put anything in a URL, that protection is worth having for these fields.

Frequently asked questions

Can I use the same snippet with a framework like React?

The idea is the same: read window.location.search on the page that renders the form and put the values into hidden inputs or into the data you send. The snippet above is plain HTML and JavaScript, so adapt it to your framework and check what arrives.

Start with utm_source, utm_medium and utm_campaign, and use the same spelling every time. Newsletter and newsletter become two different values in a spreadsheet, so pick lowercase and stick to it.

Next step

Add the three hidden inputs and the script to your landing page, then open your own tagged link and send a dummy submission. Open it in the Submissions tab and confirm all three values are there. If you need a starting point, our sales lead form template is a good base.

To get the captured values into a spreadsheet as submissions arrive, see how to send an HTML form to Google Sheets. To pass them to a CRM or another tool, forward each submission with webhooks. The complete guide to contact forms covers the rest of the form.

Want to know which campaign sent each lead?

Create a Formtorch form, add three hidden fields to your landing page, and see the campaign next to every submission in your dashboard.

Related posts
Abstract white wireframe ribbons on a black background
GitHub Pages Contact Form Without a Backend
TutorialsDecember 3, 20268 min read
Thin white lines forming a looping ribbon on a black background
How to export website form submissions to CSV
TutorialsNovember 12, 20267 min read
Thin white lines forming a smooth, flowing ribbon on a black background
How to hand off a client's contact form after launch
TutorialsNovember 5, 20267 min read
Form submission webhooks: payloads, signatures, and retries
Form submission webhooks: payloads, signatures, and retries
TutorialsOctober 29, 202610 min read