How to Capture the GCLID in a Website Form (Plus gbraid and wbraid)

A copy-paste script that saves Google Ads click IDs and fills your hidden form fields, plus the common failures that leave those fields empty.

Article metadata

Franco Maccarone

Written by

Franco Maccarone

Founder, LeadUp

  • 5 min read

To capture the GCLID in a website form, add a hidden field to the form, read the gclid value from the landing page URL, save it in the browser, and write it into the field when the form loads.

The saving step is the one people skip. If a visitor lands on one page and fills in the form on another, the URL no longer contains the click ID, so a script that only reads the current URL leaves the field empty. Google’s offline conversion guide recommends running the script on every page for this reason.

Google’s sample only handles the gclid. The version below also captures gbraid, wbraid, and the UTM values from the same click.

Step 1: add the hidden fields

Add a hidden field to every form for each value you want to keep:

<input type="hidden" name="gclid" />
<input type="hidden" name="gbraid" />
<input type="hidden" name="wbraid" />
<input type="hidden" name="utm_campaign" />
<input type="hidden" name="utm_term" />
<input type="hidden" name="landing_page_url" />

Keep the three click IDs in separate fields. They’re different identifiers, and each has its own field when you upload a conversion to Google Ads:

IDWhen it appears
gclidMost ad clicks with auto-tagging turned on
gbraidA web ad click that leads to an iOS app
wbraidAn in-app iOS ad click that leads to a web page

Google’s upload documentation has the current definitions. You can’t predict which one a click will carry, so capture all three.

If your form plugin generates its own field names, such as input_5, give the field or its wrapper a CSS class named gclid instead. The script below looks for both.

Step 2: add the script

Add this before the closing </body> tag on every page, or as a Custom HTML tag in Google Tag Manager that fires on all pages:

<script>
(function () {
  var CLICK_IDS = ['gclid', 'gbraid', 'wbraid'];
  var FIELDS = CLICK_IDS.concat(['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content']);
  var KEY = 'google_ads_click';
  var MAX_AGE = 90 * 24 * 60 * 60 * 1000; // 90 days

  function readSaved() {
    try {
      var saved = JSON.parse(localStorage.getItem(KEY));
      return saved && Date.now() - saved.savedAt < MAX_AGE ? saved : null;
    } catch (e) {
      return null;
    }
  }

  function capture() {
    var params = new URLSearchParams(location.search);
    var gclsrc = params.get('gclsrc');
    var hasClickId = CLICK_IDS.some(function (id) { return params.get(id); });
    if (!hasClickId || (gclsrc && gclsrc.indexOf('aw') === -1)) return readSaved();

    var click = { savedAt: Date.now(), landing_page_url: location.origin + location.pathname };
    FIELDS.forEach(function (field) {
      if (params.get(field)) click[field] = params.get(field);
    });
    try {
      localStorage.setItem(KEY, JSON.stringify(click));
    } catch (e) {}
    return click;
  }

  function fillForms(click) {
    Object.keys(click).forEach(function (field) {
      if (field === 'savedAt') return;
      var selector = 'input[name="' + field + '"], input.' + field + ', .' + field + ' input';
      document.querySelectorAll(selector).forEach(function (input) {
        input.value = click[field];
      });
    });
  }

  var click = capture();
  if (!click) return;
  fillForms(click);
  new MutationObserver(function () { fillForms(click); })
    .observe(document.documentElement, { childList: true, subtree: true });
})();
</script>

The script is built around a few decisions:

  • A new ad click replaces the old one. IDs from different clicks never mix on one lead.
  • A visit with only UTMs changes nothing. A later newsletter click won’t overwrite the ad click.
  • Saved clicks expire after 90 days, the same lifetime Google’s sample uses.
  • The gclsrc check comes from Google’s sample, which skips the value when gclsrc is present without aw.
  • Forms added after page load get filled too, which covers most popups and AJAX forms.

This stores data in the visitor’s browser, so run it under the same consent rules as your other marketing tags. Google’s guide also tells you to follow local cookie consent rules.

Step 3: make sure the values leave the form

A hidden field only helps if its value reaches the next system. Check each handoff:

  • Form plugin: confirm the hidden fields are included in the submission data and notifications.
  • Zapier or Make: map each new field in the action step. Existing automations won’t include them automatically.
  • CRM: add custom fields, and make them long enough not to cut off a long click ID. Google notes that the GCLID is case sensitive, so don’t let any step change its case.

Why the GCLID field is empty

If the setup looks right but values are missing, these are the usual causes:

SymptomLikely causeFix
Missing for every paid leadAuto-tagging is offTurn on auto-tagging in the Google Ads account settings
Missing on the landing pageA redirect dropped the parametersFix redirects so the query string survives, including http to https and www rules
Works on the landing page, missing on the form pageThe value isn’t being savedRun the script on every page, not only on landing pages
Field never fills, no errorThe form is in an iframe from another domainYour page script can’t reach it. Use the form vendor’s hidden field feature and pass the values in the embed URL
Lost when the form is on another domainBrowser storage is separate for each domainPass the values in the link to the form domain, and run the script there too
Value is filled but not submittedA React or Vue form ignores direct value changesRead google_ads_click from localStorage when the form submits and add it to the payload
The same ID appears on different leadsThe value was written into cached HTMLFill it in the browser, as this script does, not on the server
Missing for some visitors onlyPrivacy tools, ad blockers, or declined consentExpected. Send email and phone so enhanced conversions can help

Test it before you trust it

  1. Open a landing page in a private window with a test value, such as ?gclid=TEST_click_id_0123456789.
  2. Click through to a different page on the site, then open the form.
  3. Check the hidden field in your browser’s developer tools.
  4. Submit the form and confirm the value arrives at the final destination, with its case intact.
  5. Repeat the test with ?wbraid= in place of ?gclid=.

This proves your form plumbing works. A made-up ID will still fail if you upload it to Google Ads, so test the upload with a real ad click.

Once the script is live, track the share of paid form leads with a click ID each week. It won’t reach 100%, but a sudden drop usually means a site change broke something, such as a new redirect or a rebuilt form.

When the click ID is missing anyway

Some clicks will never give you an ID. Enhanced conversions for leads give Google a second way to match, using the hashed email or phone from the form. Google still recommends including GCLIDs whenever you can, so use the two together. Consider making email or phone a required field if your form allows it.

Sending click IDs to LeadUp

If you use LeadUp’s form lead scoring, include these same fields in your webhook’s JSON body:

{
  "name": "Jordan Reyes",
  "email": "jordan@example.com",
  "phone": "602-555-0142",
  "message": "I was rear-ended in Tempe last week...",
  "gclid": "Cj0KCQjw...",
  "utm_campaign": "Car Accident - Search",
  "utm_term": "car accident lawyer phoenix",
  "landing_page_url": "https://example.com/car-accident-lawyer"
}

LeadUp reads gclid, gbraid, and wbraid for conversion uploads, and uses the UTM and page fields to show lead quality by campaign, landing page, and search term. It treats click IDs shorter than 15 characters as missing, so use a longer value when you test.

Once the IDs are arriving, the next question is what to do with them. Start with how to keep spam out of your Google Ads conversions.

Related articles

Form Lead Scoring

Want Google Ads to optimize for qualified form leads?

See how LeadUp rates each form submission against the client’s own criteria and sends only qualified conversions to Google Ads.

Pick the path that fits your timeline. Both go directly to our team.

Share your goals

Tell us about your stack, priorities, or blockers and we will recommend the next best step.