Use GTM To Capture And Send, Google Ads Click ID (gclid) with Form Submissions in Hidden Field

Most businesses rely on a form submission for initial contact of a lead into the sales team but this rarely happens from a user landing on the “contact us” page, filling in the form and leaving.

When a user clicks through from Paid Search on Google, to preserve the original click (gclid) tracking across pages, we use the Conversion Linker Tag in GTM. As this is a 1st Party cookie we can store and send this data as needed (after the user provides marketing consent).

The Conversion Linker Tag can also be part of a robust solution to send the gclid value not just to Google Ads but to any analytics platform, CRM or spreadsheet. Google’s OCI (Offline Conversion Import) process requires the gclid to be passed back into Google Ads with its business value i.e. final sales revenue (or an earlier stage in the lifecycle process) for tROAS bidding allowing B2B customers to bid for prospects not on the fact someone filled in a form but on the potential business value that submission may yield.

Steps to Send Additional Information in Hidden Form Fields using GTM

  1. Use the Conversion Linker Tag in GTM to access the gclid

    1. As this is provided by Google we can assume it is robust and very reliable!

  2. Store the gclid value in a new First Party Cookie Variable from within GTM and re-format with a custom JavaScript variable

  3. Create a hidden form field to write the value of the gclid into (used for this specific purpose)

  4. Create a GTM tag to populate the value of the hidden form field with that of the JavaScript variable on page load

    1. When the User submits the form, the gclid value will be sent along with all the other details to the target destinations e.g a CRM or spreadsheet

Capture and send any ad click ID using the same principles

The same process can be applied to any ad platform including Meta, LinkedIn and Microsoft Ads by creating a 1st Party Cookie Variable in GTM, a corresponding hidden form field for each and a querySelector Tag.

1. Make gclid Available in GTM From Conversion Linker Tag

The Conversion Linker Tag sets the value and handles the storage of the gclid through a 1st Party Cookie that we will also make use of.

 
 

The Conversion Linker Tag passes the gclid across all pages and sends the conversion data to google. It does this by storing the value in a cookie called “_gcl_aw” for Google Ads or “_gcl_dc” for Campaign Manager (floodlight).

Create a First-Party Cookie variable in GTM and get it to store its value.

 
 

As a demonstration, if I generate a fake gclid value of e.g. test4, preview GTM, then open dev console and look at the list of cookies, I can see the _gcl_aw has a value of GCL.17099etc.test4

2. Store the gclid value in a new variable

Next we need to parse the value of this variable so we are left with the part we need. We only want to keep and store the last bit after the last full stop. So we need another GTM variable to grab that. Copy/Paste the code below. It separates out the value above by delimiting (like in excel) on a dot and then “pop” returns the last value i.e. “test4”.

function () {
  return {{gclid - Store from Conversion Linker Cookie}}.split(".").pop();
 }

3. Create a hidden field on the form to store the gclid value

How and where you do this is CMS dependent and also which app for Forms your company uses and if the paid or free version is used. This website uses Squarespace which has its own form widget, WordPress CMS and there are a lot of form plugins for this e.g. Gravity Forms, Contact Form 7, WPForms, Ninja Forms (all of which I have experience with). Other website builders include Wix, Webflow and Hubspot (which is also a CRM).

Squarespace Hidden Form Field

Adding a hidden field to a form in Squarespace and all other Form Widgets is straightforward. The screenshot below shows what this would like for the field that I labelled “gclid”. Squarespace taxonomy auto-populates the name of this variable and prefixes “SQF_” to it. It also sets the name to be in ALL CAPS.

 
 

View the Form in the Console and write a CSS Selector that uniquely identifies the hidden form field by its name “SQF_GCLID”.

When I right-click on the Message (required) field of the Form on my contact-us page and select “Inspect” option, the Elements tab shows the image below.

Within the class “field-list” and highlighted in green is the section that describes the attributes that we are interested in: name, type, value

<input id="hidden-1f0e2035-b70a-4786-99fe-80df426fa023-field" name="SQF_GCLID" type="hidden" value="">

Test & confirm the CSS and JavaScript in the Console that will eventually be used by a Tag in GTM. The general steps are shown below:

  1. Create a test variable for the gclid value

  2. Write a querySelectorAll that only returns one result i.e. the NodeList length is 1

  3. Print this again using querySelector (to be used in the GTM Tag)

  4. Assign the value of the hidden form field selected to that of the gclid variable

  5. Print it out again to confirm it worked

The querySelector I wrote “document.querySelector("input[name$='GCLID']")” would be interpreted as “find the first “input” that has an [attribute] name that ends ($) in GCLID”

4. Replicate in GTM with A Custom HTML Tag

Go to Google Tag Manager and set up a new Custom HTML Tag that reflects the steps above but with some changes to make it compatible with GTM and the GTM gclid variable we defined earlier. Set the Tag to fire later than usual so that the form will have had time to load e.g. on DOM Ready or Window Loaded

<script>
(function(){
document.querySelector("input[name$='GCLID']").value = {{gclid - Get Value}};
})();

</script>
 
 

Preview and Test this in GTM before publishing

To test that the conversion linker and _gcl_aw cookie is a robust method I Preview GTM on non-form page that has a “fake” gclid value in the URL and then navigate to the form page before checking that the form field is populated as desired. This is shown in the screenshot in Step 1 above.


Cookie Names of Other Ad Platform Click IDs (and GA4 Client ID)

 
PlatformCookie NamePurposeExample Cookie ValueQuery Parameter
Google Ads_gcl_awStores the Google Ads Click ID (GCLID) for tracking conversions tied to the specific adGCL.123.Cj0KCQiAn4SeBhCwARIsANeF9DGye?gclid=Cj0Ketc
Google Ads_gcl_auA broader Google Ads cookie used for attribution beyond specific Ad clicks1.1.123.Cj0KCQiAn4SeBhCwARIsANeF9DGyen/a
Google Ads (Floodlight)_gcl_dcStores the GCLID for Google Campaign Manager (Floodlight)Cj0KCQiAn4SeBhCwARIsANeF9DGye?gclid=
Google Analytics_gaStores the Google Analytics Client IDGA1.1.123456789.987654321n/a
Meta (Facebook)_fbcStores the Facebook Click ID (FBCLID)fb.1.1699999999.ABCDEF123456?fbclid=ABCDetc
Meta (Facebook)_fbpStores a first-party cookie for Meta pixel trackingfb.1.1699999999.ABCDEF123456n/a
Microsoft Ads (Bing)_uetmsclkidStores the Microsoft Ads Click ID (MSCLKID) for attribution_uetabcdef1234567890abcdef1234567890?msclkid=abcetc
Microsoft Ads (Bing)_uetsidStores the Bing Ads Click ID (MSCLKID) for sessionsabcdef1234567890abcdef1234567890n/a
Microsoft Ads (Bing)_uetvidStores the Bing Ads Click ID (MSCLKID) for visitorsabcdef1234567890abcdef1234567890n/a
LinkedIn_li_msStores LinkedIn click tracking dataC1234ABCD1234?li_fat_id=
Twitter Ads_twq_click_idStores the Twitter Click ID for campaign attribution123456abcdef?twclid=
Pinterest Ads_pin_unauthStores Pinterest click tracking data for unauthenticated usersC1234ABC?pinclid=

Use an appropriate custom JavaScript method, depending on the location of the clickID within the cookie.

For clarity, it is the ad platform that appends its own clickID query parameter to the landing Page URL within the Ad Account. This its own unique “key” to monitor and measure performance on and off the platform and connect the data together such as impressions, cost to conversions, revenue, etc. In order to make this useful to your business and the whole reason for passing this data is up to what you want to do with it and why within the analytics/CMS system that is receiving the data.

 
PlatformCookie NameExample Cookie ValueQuery ParameterLogic to Extract Relevent Portion of cookie value
Google Ads_gcl_awGCL.123.Cj0KCQiAn4SeBhCwARIsANeF9DGye?gclid=Cj0KetcLast Part
Google Analytics_gaGA1.1.123456789.987654321n/aLast 2 Parts
Meta (Facebook)_fbcfb.1.1699999999.ABCDEF123456?fbclid=ABCDetcLast Part
Microsoft Ads (Bing)_uetmsclkid_uetabcdef1234567890abcdef1234567890?msclkid=abcetcRemove 1st 4 characters from string
LinkedInli_fat_idC1234ABCD1234?li_fat_id=C123etcn/a (can use the cookie variable directly from GTM)
Twitter Ads_twclid123456abcdef?twclid=123etcn/a (can use the cookie variable directly from GTM)
Pinterest Ads_epikC1234ABC?epik=C123etcn/a (can use the cookie variable directly from GTM)

Example to Get GA4 Client ID

To extract the GA4 Client ID which is the last 2 parts of the string GA1.1.1234.5678 would look something like the code below.

function() {
  var cookie = {{GA4 Cookie}};
  if (cookie && typeof cookie === 'string' && cookie.startsWith('GA')) {
    return cookie.split('.').slice(-2).join('.'); //go to the end and get the last 2 parts
  }
  return ''; // Default fallback
}

Some Things To Consider

  1. Hubspot Forms do not need to use this method as they have their own connectors for Google Ads that removes the need to do this

    1. With Hubspot it is also not necessary for Facebook or LinkedIn, for the same reason.

  2. Hubspot Forms can also be integrated to auto update customer match Contact Lists, Send Enhanced Conversions and Import Offline Conversions on Lifecycle Stage changes back to Google Ads

  3. Do not test how forms populate with visible form fields as they have different safety mechanisms that often mean they will not allow form fields to be populated like this

    1. As I found out after a lot of time-wasted as to why I could not get this to work initially!

  4. If your querySelector works in the Console but the value cannot be written to then skip this step and test the set-up in GTM as the console or the form may be blocking access which may not be the case in a “real” situation

Pass Any Ad Click Id To Hidden Form Fields With GTM

Repeat the above process for any other Paid Ad Click coming from Microsoft Ads (MSA - Bing Ads), LinkedIn, Meta, etc.

Preview of Variables in GTM Preview Mode

GTM Variable
Variable TypeValue
Cookie - GA4 ClientID1st Party Cookie"GA1.1.169232950.1730734883"
Cookie Value - GA4 ClientIDCustom JavaScript"169232950.1730734883"
Cookie - Google Ads ClickID - gclid1st Party Cookie"GCL.1736337107.xyzgoogle-ads"
Cookie Value - Google Ads ClickID - gclidCustom JavaScript"xyzgoogle-ads"
Cookie - MSFT Ads ClickID - msclkid1st Party Cookie"_uetabc123"
Cookie Value - MSFT Ads ClickID - msclkidCustom JavaScript"abc123"
Cookie Value - LinkedIn ClickID - li_fat_id1st Party Cookie"123linkedin"
https://domain.com/contact-us?gclid=xyzgoogle-ads&msclkid=abc123&li_fat_id=123linkedin

The Landing Page URL is for testing & demonstrating only as the URL (in the table above) would never have all of these query parameters as any given ad click would come from a single ad platform at a time.

On your website, create hidden form fields and label them however you like:

 
 

2. In GTM, create 1st Party cookie variables for each ad platform by naming the cookie in the variable exactly as defined by the Ad Platform

3. In GTM, create corresponding JavaScript variables, to retrieve the relevant part of the cookie value that is typically appended by the platform in the query parameter of the URL:

3.1 This step is not needed for cookies that only store the exact value used e.g. the LinkedIn cookie value and that of the query parameter are the same so the 1P Cookie Variable can be used directly as it already holds the value needed

 
 

4. In GTM, bring all of this together with a custom HTML that uses a querySelector to identify the appropriate hidden form field and then populate its value from the custom variable

Previous
Previous

How To Stop Display Ads from Appearing On Childrens Content on YouTube

Next
Next

How To Remove Usercentrics Cookiebot Branding From Consent Banner with GTM